<?php
// Temporary - no auth check
require_once '../partials/admin-sidebar.php';

// Simple database connection
$host = 'localhost';
$dbname = 'herbal_wellness';
$username = 'root';
$password = '';

try {
    $db = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    switch ($_POST['action']) {
        case 'update_service':
            $stmt = $db->prepare("UPDATE toa_wheel_services SET service_name = ?, color_primary = ?, color_secondary = ?, icon = ?, description = ? WHERE id = ?");
            $stmt->execute([$_POST['service_name'], $_POST['color_primary'], $_POST['color_secondary'], $_POST['icon'], $_POST['description'], $_POST['service_id']]);
            break;
            
        case 'add_personnel':
            $stmt = $db->prepare("INSERT INTO toa_wheel_personnel (service_id, name, website) VALUES (?, ?, ?)");
            $stmt->execute([$_POST['service_id'], $_POST['name'], $_POST['website']]);
            break;
            
        case 'update_personnel':
            $stmt = $db->prepare("UPDATE toa_wheel_personnel SET name = ?, website = ? WHERE id = ?");
            $stmt->execute([$_POST['name'], $_POST['website'], $_POST['personnel_id']]);
            break;
            
        case 'delete_personnel':
            $stmt = $db->prepare("DELETE FROM toa_wheel_personnel WHERE id = ?");
            $stmt->execute([$_POST['personnel_id']]);
            break;
    }
    header('Location: toa-wheel-services.php?success=1');
    exit;
}

// Get data
$services = $db->query("SELECT * FROM toa_wheel_services ORDER BY display_order")->fetchAll(PDO::FETCH_ASSOC);
$personnel = $db->query("
    SELECT p.*, s.service_name 
    FROM toa_wheel_personnel p 
    JOIN toa_wheel_services s ON p.service_id = s.id 
    ORDER BY s.display_order, p.name
")->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TOA Wheel Management | Herbal Wellness</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body class="bg-gray-100">
    <div class="min-h-screen flex">
        <!-- Sidebar -->
        <?php renderAdminSidebar('toa-wheel-services'); ?>
        
        <!-- Main Content -->
        <div class="flex-1 p-8">
            <div class="mb-8">
                <h2 class="text-3xl font-bold text-gray-900">TOA Wellness Wheel Management</h2>
                <p class="text-gray-600 mt-2">Manage services and personnel for the TOA Wellness Wheel section</p>
            </div>
            
            <?php if (isset($_GET['success'])): ?>
                <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-6">
                    Changes saved successfully!
                </div>
            <?php endif; ?>

            <!-- Add Personnel Form -->
            <div class="bg-white p-6 rounded-lg shadow mb-8">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">Add New Personnel</h3>
                <form method="POST" class="space-y-4">
                    <input type="hidden" name="action" value="add_personnel">
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Service</label>
                        <select name="service_id" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500">
                            <?php foreach ($services as $service): ?>
                                <option value="<?= $service['id'] ?>"><?= htmlspecialchars($service['service_name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Name</label>
                        <input type="text" name="name" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Website URL</label>
                        <input type="url" name="website" placeholder="https://" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500">
                    </div>
                    
                    <button type="submit" class="bg-emerald-600 text-white px-4 py-2 rounded hover:bg-emerald-700">
                        Add Personnel
                    </button>
                </form>
            </div>

            <!-- Services Management -->
            <div class="bg-white p-6 rounded-lg shadow mb-8">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">Edit Services</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <?php foreach ($services as $service): ?>
                    <form method="POST" class="border border-gray-200 rounded-lg p-4">
                        <input type="hidden" name="action" value="update_service">
                        <input type="hidden" name="service_id" value="<?= $service['id'] ?>">
                        
                        <div class="space-y-3">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Service Name</label>
                                <input type="text" name="service_name" value="<?= htmlspecialchars($service['service_name']) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-md">
                            </div>
                            
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Description</label>
                                <input type="text" name="description" value="<?= htmlspecialchars($service['description'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-md">
                            </div>
                            
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Icon (Font Awesome)</label>
                                <input type="text" name="icon" value="<?= htmlspecialchars($service['icon'] ?? '') ?>" placeholder="fas fa-building" class="w-full px-3 py-2 border border-gray-300 rounded-md">
                            </div>
                            
                            <div class="grid grid-cols-2 gap-2">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-1">Primary Color</label>
                                    <input type="color" name="color_primary" value="<?= $service['color_primary'] ?>" class="w-full h-10 border border-gray-300 rounded-md">
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-1">Secondary Color</label>
                                    <input type="color" name="color_secondary" value="<?= $service['color_secondary'] ?>" class="w-full h-10 border border-gray-300 rounded-md">
                                </div>
                            </div>
                            
                            <button type="submit" class="w-full bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">
                                Update Service
                            </button>
                        </div>
                    </form>
                    <?php endforeach; ?>
                </div>
            </div>

            <!-- Personnel Management -->
            <div class="bg-white p-6 rounded-lg shadow mb-8">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">Current Personnel</h3>
                <?php 
                $current_service = '';
                foreach ($personnel as $person): 
                    if ($current_service !== $person['service_name']):
                        $current_service = $person['service_name'];
                        echo "<h4 class='font-medium text-gray-900 mt-6 mb-3 first:mt-0'>{$current_service}</h4>";
                    endif;
                ?>
                <div class="flex items-center space-x-4 mb-3 p-3 bg-gray-50 rounded">
                    <form method="POST" class="flex items-center space-x-3 flex-1">
                        <input type="hidden" name="action" value="update_personnel">
                        <input type="hidden" name="personnel_id" value="<?= $person['id'] ?>">
                        <input type="text" name="name" value="<?= htmlspecialchars($person['name']) ?>" class="px-3 py-1 border border-gray-300 rounded" required>
                        <input type="url" name="website" value="<?= htmlspecialchars($person['website']) ?>" class="flex-1 px-3 py-1 border border-gray-300 rounded" required>
                        <button type="submit" class="bg-green-600 text-white px-3 py-1 rounded text-sm hover:bg-green-700">Update</button>
                    </form>
                    <form method="POST" onsubmit="return confirm('Delete this personnel?')">
                        <input type="hidden" name="action" value="delete_personnel">
                        <input type="hidden" name="personnel_id" value="<?= $person['id'] ?>">
                        <button type="submit" class="bg-red-600 text-white px-3 py-1 rounded text-sm hover:bg-red-700">Delete</button>
                    </form>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</body>
</html>