<?php
require_once __DIR__ . '/../api/lib.php';
hw_require_auth(['superadmin']);
$authUser = hw_get_auth_user();

$db = hw_db();

// Handle form submissions
if ($_POST) {
    $section = $_POST['section'] ?? '';
    $content = $_POST['content'] ?? '';
    
    if ($section && $content) {
        // Check if content exists
        $stmt = $db->prepare("SELECT id FROM website_content WHERE section = ?");
        $stmt->execute([$section]);
        
        if ($stmt->fetch()) {
            // Update existing
            $stmt = $db->prepare("UPDATE website_content SET content = ?, updated_at = NOW() WHERE section = ?");
            $stmt->execute([$content, $section]);
        } else {
            // Insert new
            $stmt = $db->prepare("INSERT INTO website_content (section, content, created_at, updated_at) VALUES (?, ?, NOW(), NOW())");
            $stmt->execute([$section, $content]);
        }
        
        $success = "Content updated successfully!";
    }
}

// Get all content sections
$stmt = $db->query("SELECT * FROM website_content ORDER BY section");
$content_sections = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Default sections if none exist
$default_sections = [
    'hero_title' => 'Welcome to Herbal Wellness',
    'hero_subtitle' => 'Your journey to natural health and wellness starts here',
    'about_title' => 'About Our Practice',
    'about_content' => 'We are dedicated to providing holistic wellness solutions...',
    'services_title' => 'Our Services',
    'contact_info' => 'Contact us for more information about our services'
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Website Content | Superadmin</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 -->
        <div class="bg-emerald-800 text-white w-64 flex-shrink-0">
            <div class="p-6">
                <h1 class="text-xl font-bold">Superadmin Panel</h1>
                <p class="text-emerald-200 text-sm mt-1">Website Management</p>
            </div>
            
            <nav class="mt-6">
                <a href="/files/atasteofafricanmarketplace/superadmin/dashboard.php" class="flex items-center px-6 py-3 hover:bg-emerald-700 text-emerald-100">
                    <i class="fas fa-tachometer-alt mr-3"></i>
                    Dashboard
                </a>
                <a href="/files/atasteofafricanmarketplace/superadmin/coaches.php" class="flex items-center px-6 py-3 hover:bg-emerald-700 text-emerald-100">
                    <i class="fas fa-users mr-3"></i>
                    Manage Coaches
                </a>
                <a href="/files/atasteofafricanmarketplace/superadmin/content.php" class="flex items-center px-6 py-3 bg-emerald-700 text-white">
                    <i class="fas fa-edit mr-3"></i>
                    Website Content
                </a>
                
                <div class="border-t border-emerald-700 mt-6 pt-6">
                    <a href="/files/atasteofafricanmarketplace/api/auth/logout.php" class="flex items-center px-6 py-3 hover:bg-red-600 text-emerald-100">
                        <i class="fas fa-sign-out-alt mr-3"></i>
                        Logout
                    </a>
                </div>
            </nav>
        </div>
        
        <!-- Main Content -->
        <div class="flex-1 p-8">
            <div class="mb-8">
                <h2 class="text-3xl font-bold text-gray-900">Website Content Management</h2>
                <p class="text-gray-600 mt-2">Edit homepage and website content sections</p>
            </div>
            
            <?php if (isset($success)): ?>
                <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-6">
                    <?= htmlspecialchars($success) ?>
                </div>
            <?php endif; ?>
            
            <!-- Content Sections -->
            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                <?php foreach ($default_sections as $section => $default_value): ?>
                    <?php
                    $current_content = $default_value;
                    foreach ($content_sections as $content) {
                        if ($content['section'] === $section) {
                            $current_content = $content['content'];
                            break;
                        }
                    }
                    ?>
                    <div class="bg-white p-6 rounded-lg shadow">
                        <h3 class="text-lg font-semibold text-gray-900 mb-4">
                            <?= ucwords(str_replace('_', ' ', $section)) ?>
                        </h3>
                        
                        <form method="POST" class="space-y-4">
                            <input type="hidden" name="section" value="<?= htmlspecialchars($section) ?>">
                            
                            <?php if (in_array($section, ['hero_title', 'about_title', 'services_title'])): ?>
                                <input type="text" name="content" 
                                       value="<?= htmlspecialchars($current_content) ?>"
                                       class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500"
                                       required>
                            <?php else: ?>
                                <textarea name="content" rows="4" 
                                          class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500"
                                          required><?= htmlspecialchars($current_content) ?></textarea>
                            <?php endif; ?>
                            
                            <button type="submit" 
                                    class="bg-emerald-600 text-white px-4 py-2 rounded hover:bg-emerald-700">
                                Update Content
                            </button>
                        </form>
                    </div>
                <?php endforeach; ?>
            </div>
            
            <!-- Add Custom Section -->
            <div class="mt-8 bg-white p-6 rounded-lg shadow">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">Add Custom Section</h3>
                
                <form method="POST" class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Section Name</label>
                        <input type="text" name="section" 
                               placeholder="e.g., footer_text, testimonial_1"
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500"
                               required>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Content</label>
                        <textarea name="content" rows="4" 
                                  class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500"
                                  required></textarea>
                    </div>
                    
                    <button type="submit" 
                            class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">
                        Add Section
                    </button>
                </form>
            </div>
            
            <!-- Existing Custom Sections -->
            <?php if (!empty($content_sections)): ?>
                <div class="mt-8">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">All Content Sections</h3>
                    <div class="bg-white rounded-lg shadow overflow-hidden">
                        <table class="min-w-full">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Section</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Content Preview</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Last Updated</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                <?php foreach ($content_sections as $content): ?>
                                    <tr>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                            <?= htmlspecialchars($content['section']) ?>
                                        </td>
                                        <td class="px-6 py-4 text-sm text-gray-500">
                                            <?= htmlspecialchars(substr($content['content'], 0, 100)) ?>
                                            <?= strlen($content['content']) > 100 ? '...' : '' ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            <?= date('M j, Y g:i A', strtotime($content['updated_at'])) ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>