<?php
session_start();
require_once '../../api/lib.php';

// Simple admin check
if (!isset($_SESSION['user_id'])) {
    header('Location: /files/atasteofafricanmarketplace/superadmin-login.php');
    exit;
}

$db = hw_db();

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'update_section':
                $stmt = $db->prepare("UPDATE site_content SET content = ? WHERE section = 'toa_wheel_title'");
                $stmt->execute([$_POST['section_title']]);
                $stmt = $db->prepare("UPDATE site_content SET content = ? WHERE section = 'toa_wheel_description'");
                $stmt->execute([$_POST['section_description']]);
                break;
                
            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 '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 '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 'delete_personnel':
                $stmt = $db->prepare("DELETE FROM toa_wheel_personnel WHERE id = ?");
                $stmt->execute([$_POST['personnel_id']]);
                break;
        }
        header('Location: toa-wheel-management.php?success=1');
        exit;
    }
}

// Get section content
$section_title = $db->query("SELECT content FROM site_content WHERE section = 'toa_wheel_title'")->fetchColumn() ?: 'TOA Wellness Wheel – Business & Financial Empowerment';
$section_description = $db->query("SELECT content FROM site_content WHERE section = 'toa_wheel_description'")->fetchColumn() ?: 'Your membership goes beyond wellness—we empower your business growth, financial stability, and long-term prosperity through comprehensive professional services and strategic guidance.';

// Get services and personnel
$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 Wellness Wheel Management</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * { box-sizing: border-box; }
        body { font-family: system-ui, -apple-system, sans-serif; margin: 0; background: #f5f5f5; }
        .container { max-width: 1200px; margin: 0 auto; padding: 20px; }
        .header { background: white; padding: 20px; border-radius: 8px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .section { background: white; padding: 20px; border-radius: 8px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; font-weight: 600; }
        input, select { width: 100%; padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px; }
        button { background: #007cba; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; }
        button:hover { background: #005a87; }
        .color-preview { width: 30px; height: 30px; border-radius: 4px; display: inline-block; margin-left: 10px; }
        .personnel-list { display: grid; gap: 10px; }
        .personnel-item { padding: 10px; background: #f8f9fa; border-radius: 4px; display: flex; justify-content: space-between; align-items: center; }
        .success { background: #d4edda; color: #155724; padding: 10px; border-radius: 4px; margin-bottom: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>TOA Wellness Wheel – Business & Financial Empowerment</h1>
            <p>Complete management of the TOA Wellness Wheel section including title, description, services, and personnel</p>
            <?php if (isset($_GET['success'])): ?>
                <div class="success">Changes saved successfully!</div>
            <?php endif; ?>
        </div>

        <div class="section">
            <h2>Section Content</h2>
            <form method="POST">
                <input type="hidden" name="action" value="update_section">
                <div class="form-group">
                    <label>Section Title</label>
                    <input type="text" name="section_title" value="<?= htmlspecialchars($section_title) ?>" required>
                </div>
                <div class="form-group">
                    <label>Section Description</label>
                    <textarea name="section_description" rows="3" style="width: 100%; padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px;" required><?= htmlspecialchars($section_description) ?></textarea>
                </div>
                <button type="submit">Update Section Content</button>
            </form>
        </div>

        <div class="section">
            <h2>Services & Colors</h2>
            <div class="grid">
                <?php foreach ($services as $service): ?>
                <form method="POST" style="border: 1px solid #ddd; padding: 15px; border-radius: 4px;">
                    <input type="hidden" name="action" value="update_service">
                    <input type="hidden" name="service_id" value="<?= $service['id'] ?>">
                    
                    <div class="form-group">
                        <label>Service Name</label>
                        <input type="text" name="service_name" value="<?= htmlspecialchars($service['service_name']) ?>" required>
                    </div>
                    
                    <div class="form-group">
                        <label>Description</label>
                        <input type="text" name="description" value="<?= htmlspecialchars($service['description'] ?? '') ?>" placeholder="Brief service description">
                    </div>
                    
                    <div class="form-group">
                        <label>Icon (Font Awesome class)</label>
                        <input type="text" name="icon" value="<?= htmlspecialchars($service['icon'] ?? '') ?>" placeholder="fas fa-building">
                    </div>
                    
                    <div class="form-group">
                        <label>Primary Color</label>
                        <input type="color" name="color_primary" value="<?= $service['color_primary'] ?>" required>
                        <span class="color-preview" style="background: <?= $service['color_primary'] ?>"></span>
                    </div>
                    
                    <div class="form-group">
                        <label>Secondary Color</label>
                        <input type="color" name="color_secondary" value="<?= $service['color_secondary'] ?>" required>
                        <span class="color-preview" style="background: <?= $service['color_secondary'] ?>"></span>
                    </div>
                    
                    <button type="submit">Update Service</button>
                </form>
                <?php endforeach; ?>
            </div>
        </div>

        <div class="section">
            <h2>Personnel Management</h2>
            
            <h3>Add New Personnel</h3>
            <form method="POST" style="background: #f8f9fa; padding: 15px; border-radius: 4px; margin-bottom: 20px;">
                <input type="hidden" name="action" value="add_personnel">
                <div style="display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 10px; align-items: end;">
                    <div class="form-group">
                        <label>Service</label>
                        <select name="service_id" required>
                            <?php foreach ($services as $service): ?>
                                <option value="<?= $service['id'] ?>"><?= htmlspecialchars($service['service_name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Name</label>
                        <input type="text" name="name" required>
                    </div>
                    <div class="form-group">
                        <label>Website URL</label>
                        <input type="url" name="website" placeholder="https://" required>
                    </div>
                    <button type="submit">Add Personnel</button>
                </div>
            </form>

            <h3>Current Personnel</h3>
            <div class="personnel-list">
                <?php 
                $current_service = '';
                foreach ($personnel as $person): 
                    if ($current_service !== $person['service_name']):
                        $current_service = $person['service_name'];
                        echo "<h4 style='margin: 20px 0 10px 0; color: #333;'>{$current_service}</h4>";
                    endif;
                ?>
                <div class="personnel-item">
                    <form method="POST" style="display: flex; gap: 10px; align-items: center; 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']) ?>" style="width: 200px;" required>
                        <input type="url" name="website" value="<?= htmlspecialchars($person['website']) ?>" style="flex: 1;" required>
                        <button type="submit" style="padding: 5px 10px; background: #28a745; border: none; color: white; border-radius: 4px;">Update</button>
                    </form>
                    <form method="POST" style="margin-left: 10px;" 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" style="padding: 5px 10px; background: #dc3545; border: none; color: white; border-radius: 4px;">Delete</button>
                    </form>
                </div>
                <?php endforeach; ?>
            </div>
        </div>

        <div class="section">
            <h2>Preview</h2>
            <p>This is how the TOA Wellness Wheel section currently appears on the website:</p>
            <div style="background: #f8f9fa; padding: 20px; border-radius: 8px; border: 1px solid #ddd;">
                <h3 style="color: #333; margin-bottom: 10px;"><?= htmlspecialchars($section_title) ?></h3>
                <p style="color: #666; margin-bottom: 20px;"><?= htmlspecialchars($section_description) ?></p>
                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px;">
                    <?php foreach ($services as $service): ?>
                    <div style="background: linear-gradient(135deg, <?= $service['color_primary'] ?>, <?= $service['color_secondary'] ?>); color: white; padding: 15px; border-radius: 8px; text-align: center;">
                        <div style="font-size: 24px; margin-bottom: 8px;">
                            <i class="<?= htmlspecialchars($service['icon'] ?? 'fas fa-circle') ?>"></i>
                        </div>
                        <h4 style="margin: 0 0 5px 0; font-size: 14px;"><?= htmlspecialchars($service['service_name']) ?></h4>
                        <?php if ($service['description']): ?>
                        <p style="margin: 0; font-size: 11px; opacity: 0.9;"><?= htmlspecialchars($service['description']) ?></p>
                        <?php endif; ?>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <div style="margin-top: 20px; text-align: center;">
                <a href="/files/atasteofafricanmarketplace/index.php#business-empowerment" target="_blank" style="background: #007cba; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;">View Live Section →</a>
            </div>
        </div>
    </div>
</body>
</html>