<?php 
$pageTitle = ($action === 'create' ? 'Create New Page' : 'Edit Page') . ' - Admin Panel';
$headerTitle = $action === 'create' ? 'Create New Page' : 'Edit Page';

ob_start();
?>

<?php if (isset($success)): ?>
    <div class="bg-green-50 border border-green-200 rounded-lg p-4 mb-6">
        <div class="flex">
            <svg class="w-5 h-5 text-green-400 mr-3 mt-0.5" fill="currentColor" viewBox="0 0 20 20">
                <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
            </svg>
            <p class="text-sm text-green-700"><?= htmlspecialchars($success) ?></p>
        </div>
    </div>
<?php endif; ?>


<?php if (isset($error)): ?>
    <div class="bg-red-50 border border-red-200 rounded-lg p-4 mb-6">
        <div class="flex">
            <svg class="w-5 h-5 text-red-400 mr-3 mt-0.5" fill="currentColor" viewBox="0 0 20 20">
                <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/>
            </svg>
            <p class="text-sm text-red-700"><?= htmlspecialchars($error) ?></p>
        </div>
    </div>
<?php endif; ?>

<div class="bg-white shadow rounded-lg">
    <div class="px-6 py-4 border-b border-gray-200">
        <h3 class="text-lg font-medium text-gray-900">
            <?= $action === 'create' ? 'Create New Page' : 'Edit Page' ?>
        </h3>
    </div>

    <div class="px-6 py-6">
        <form method="POST" action="/admin/pages/">
            <input type="hidden" name="action" value="<?= $action ?>">
            <?php if ($action === 'edit'): ?>
                <input type="hidden" name="id" value="<?= $page['id'] ?>">
            <?php endif; ?>

            <div class="space-y-6">
                <!-- Basic Page Information -->
                <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                    <div>
                        <label for="name" class="block text-sm font-medium text-gray-700 mb-2">Page Name *</label>
                        <input type="text" name="name" id="name" required
                               value="<?= htmlspecialchars($page['name'] ?? '') ?>" 
                               class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                    </div>

                    <div>
                        <label for="slug" class="block text-sm font-medium text-gray-700 mb-2">Slug</label>
                        <input type="text" name="slug" id="slug" 
                               value="<?= htmlspecialchars($page['slug'] ?? '') ?>" 
                               placeholder="Auto-generated from name if empty"
                               class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                        <p class="text-xs text-gray-500 mt-1">Leave empty to auto-generate from page name</p>
                    </div>

                    <div>
                        <label for="layout" class="block text-sm font-medium text-gray-700 mb-2">Layout</label>
                        <select name="layout" id="layout"
                                class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            <?php foreach ($layouts as $layoutValue => $layoutLabel): ?>
                                <option value="<?= $layoutValue ?>" <?= ($page['layout'] ?? 'default') === $layoutValue ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($layoutLabel) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        
                        <div class="flex items-center mt-3">
                            <input type="hidden" name="is_homepage" value="0">
                            <input type="checkbox" name="is_homepage" value="1" id="is_homepage" 
                                   <?= !empty($page['is_homepage']) ? 'checked' : '' ?>
                                   class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                            <label for="is_homepage" class="ml-2 block text-sm text-gray-900">Set as Homepage</label>
                        </div>
                    </div>
                </div>

                <?php if ($action === 'create'): ?>
                    <!-- For new pages, show only basic info and a note -->
                    <div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
                        <div class="flex">
                            <svg class="w-5 h-5 text-blue-400 mr-3 mt-0.5" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"/>
                            </svg>
                            <div>
                                <h4 class="text-sm font-medium text-blue-800">Create Page First</h4>
                                <p class="text-sm text-blue-700 mt-1">Save this page to access layout-specific content fields and advanced options.</p>
                            </div>
                        </div>
                    </div>
                <?php else: ?>
                    <!-- Tabs System for Content and SEO -->
                    <div x-data="{ activePageTab: 'content' }" class="mt-6">
                        <!-- Tabs Navigation -->
                        <div class="bg-white rounded-lg shadow-sm">
                            <div class="border-b border-gray-200">
                                <nav class="-mb-px flex space-x-8 px-6">
                                    <button type="button" @click="activePageTab = 'content'" 
                                            :class="activePageTab === 'content' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'"
                                            class="py-4 px-1 border-b-2 font-medium text-sm whitespace-nowrap transition-colors">
                                        <svg class="w-5 h-5 inline mr-2" fill="currentColor" viewBox="0 0 20 20">
                                            <path d="M9 2a1 1 0 000 2h2a1 1 0 100-2H9z"/>
                                            <path fill-rule="evenodd" d="M4 5a2 2 0 012-2h8a2 2 0 012 2v6a2 2 0 01-2 2H6a2 2 0 01-2-2V5zm3 1a1 1 0 000 2h.01a1 1 0 100-2H7zm3 0a1 1 0 000 2h3a1 1 0 100-2h-3zm-3 4a1 1 0 100 2h.01a1 1 0 100-2H7zm3 0a1 1 0 100 2h3a1 1 0 100-2h-3z" clip-rule="evenodd"/>
                                        </svg>
                                        Content
                                    </button>
                                    <button type="button" @click="activePageTab = 'faq'" 
                                            :class="activePageTab === 'faq' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'"
                                            class="py-4 px-1 border-b-2 font-medium text-sm whitespace-nowrap transition-colors">
                                        <svg class="w-5 h-5 inline mr-2" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd"/>
                                        </svg>
                                        FAQ
                                    </button>
                                    <button type="button" @click="activePageTab = 'seo'" 
                                            :class="activePageTab === 'seo' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'"
                                            class="py-4 px-1 border-b-2 font-medium text-sm whitespace-nowrap transition-colors">
                                        <svg class="w-5 h-5 inline mr-2" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M12.316 3.051a1 1 0 01.633 1.265l-4 12a1 1 0 11-1.898-.632l4-12a1 1 0 011.265-.633zM5.707 6.293a1 1 0 010 1.414L3.414 10l2.293 2.293a1 1 0 11-1.414 1.414l-3-3a1 1 0 010-1.414l3-3a1 1 0 011.414 0zm8.586 0a1 1 0 011.414 0l3 3a1 1 0 010 1.414l-3 3a1 1 0 11-1.414-1.414L16.586 10l-2.293-2.293a1 1 0 010-1.414z" clip-rule="evenodd"/>
                                        </svg>
                                        SEO & Meta
                                    </button>
                                </nav>
                            </div>
                        </div>

                        <!-- Content Tab -->
                        <div x-show="activePageTab === 'content'" 
                             x-transition:enter="transition ease-out duration-200"
                             x-transition:enter-start="opacity-0"
                             x-transition:enter-end="opacity-100"
                             x-transition:leave="transition ease-in duration-150"
                             x-transition:leave-start="opacity-100"
                             x-transition:leave-end="opacity-0"
                             class="bg-white rounded-lg shadow-sm mt-6">
                            <div class="p-6">
                                <!-- Page Title -->
                                <div class="mb-6">
                                    <label for="title" class="block text-sm font-medium text-gray-700 mb-2">Page Title</label>
                                    <input type="text" name="title" id="title"
                                           value="<?= htmlspecialchars($page['title'] ?? '') ?>" 
                                           class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                                </div>

                                <!-- Layout-Specific Forms -->
                                <?php 
                                $currentLayout = $page['layout'] ?? 'default';
                                $layoutFormPath = __DIR__ . "/layouts/{$currentLayout}.php";
                                if (file_exists($layoutFormPath)) {
                                    include $layoutFormPath;
                                } else {
                                    // Fallback to default form
                                    include __DIR__ . "/layouts/default.php";
                                }
                                ?>
                            </div>
                        </div>

                        <!-- FAQ Tab -->
                        <?php 
                        $faqItemsJson = !empty($faqs['extra_fields']) ? htmlspecialchars(json_encode($faqs['extra_fields']), ENT_QUOTES, 'UTF-8') : '[]';
                        ?>
                        <div x-show="activePageTab === 'faq'" 
                             x-data='{ faqItems: <?= $faqItemsJson ?> }'
                             x-transition:enter="transition ease-out duration-200"
                             x-transition:enter-start="opacity-0"
                             x-transition:enter-end="opacity-100"
                             x-transition:leave="transition ease-in duration-150"
                             x-transition:leave-start="opacity-100"
                             x-transition:leave-end="opacity-0"
                             class="bg-white rounded-lg shadow-sm mt-6">
                            <div class="p-6">
                                <h3 class="text-lg font-medium text-gray-900 mb-6">FAQ Management</h3>
                                
                                <div class="mb-6">
                                    <label for="faq_title" class="block text-sm font-medium text-gray-700 mb-2">FAQ Section Title</label>
                                    <input type="text" name="faq[title]" id="faq_title" 
                                           value="<?= htmlspecialchars($faqs['title'] ?? '') ?>"
                                           placeholder="Enter FAQ section title..." 
                                           class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                                </div>
                                
                                <div class="space-y-4">
                                    <template x-for="(item, index) in faqItems" :key="index">
                                        <div class="faq-item bg-gray-50 rounded-lg p-4 border border-gray-200">
                                            <div class="space-y-4">
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-2">Question</label>
                                                    <input type="text" :name="`faq[items][${index}][question]`" x-model="item.question"
                                                           placeholder="Enter FAQ question..." 
                                                           class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-2">Answer</label>
                                                    <textarea :name="`faq[items][${index}][answer]`" x-model="item.answer" rows="3"
                                                              placeholder="Enter FAQ answer..."
                                                              class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"></textarea>
                                                </div>
                                                <button type="button" @click="faqItems.splice(index, 1)"
                                                        class="inline-flex items-center px-3 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500">
                                                    Remove
                                                </button>
                                            </div>
                                        </div>
                                    </template>
                                    
                                    <button type="button" @click="faqItems.push({ question: '', answer: '' })"
                                            class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                                        <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z" clip-rule="evenodd"/>
                                        </svg>
                                        Add FAQ Item
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- SEO Tab -->
                        <div x-show="activePageTab === 'seo'" 
                             x-transition:enter="transition ease-out duration-200"
                             x-transition:enter-start="opacity-0"
                             x-transition:enter-end="opacity-100"
                             x-transition:leave="transition ease-in duration-150"
                             x-transition:leave-start="opacity-100"
                             x-transition:leave-end="opacity-0"
                             class="bg-white rounded-lg shadow-sm mt-6">
                            <div class="p-6">
                                <h3 class="text-lg font-medium text-gray-900 mb-6">SEO & Meta Information</h3>
                                
                                <div class="space-y-6">
                                    <div>
                                        <label for="seo_title" class="block text-sm font-medium text-gray-700 mb-2">Meta Title</label>
                                        <input type="text" name="seo[title]" id="seo_title"
                                               value="<?= htmlspecialchars($seoMeta['title'] ?? '') ?>" 
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                               placeholder="Enter SEO title for search engines">
                                        <p class="text-xs text-gray-500 mt-1">Recommended length: 50-60 characters</p>
                                    </div>

                                    <div>
                                        <label for="seo_description" class="block text-sm font-medium text-gray-700 mb-2">Meta Description</label>
                                        <textarea name="seo[description]" id="seo_description" rows="3"
                                                  class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                                  placeholder="Enter meta description for search engines"><?= htmlspecialchars($seoMeta['description'] ?? '') ?></textarea>
                                        <p class="text-xs text-gray-500 mt-1">Recommended length: 150-160 characters</p>
                                    </div>

                                    <div>
                                        <label for="seo_keywords" class="block text-sm font-medium text-gray-700 mb-2">Meta Keywords</label>
                                        <input type="text" name="seo[keywords]" id="seo_keywords"
                                               value="<?= htmlspecialchars($seoMeta['keywords'] ?? '') ?>" 
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                               placeholder="keyword1, keyword2, keyword3">
                                        <p class="text-xs text-gray-500 mt-1">Separate keywords with commas</p>
                                    </div>

                                    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                                        <div class="grid grid-cols-1 lg:grid-cols-4 gap-4">
                                            <div class="lg:col-span-3">
                                                <label for="seo_canonical" class="block text-sm font-medium text-gray-700 mb-2">Canonical URL</label>
                                                <input type="url" name="seo[canonical]" id="seo_canonical"
                                                       value="<?= htmlspecialchars($seoMeta['canonical'] ?? '') ?>" 
                                                       class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                                       placeholder="https://example.com/page">
                                            </div>

                                            <div>
                                                <label for="seo_locale" class="block text-sm font-medium text-gray-700 mb-2">Locale</label>
                                                <input type="text" name="seo[extra_fields][locale]" id="seo_locale"
                                                       value="<?= htmlspecialchars($seoMeta['extra_fields']['locale'] ?? 'en') ?>" 
                                                       class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                                       placeholder="en">
                                            </div>
                                        </div>

                                        <div>
                                            <label for="seo_image" class="block text-sm font-medium text-gray-700 mb-2">Social Share Image</label>
                                            <input type="url" name="seo[image]" id="seo_image"
                                                   value="<?= htmlspecialchars($seoMeta['image'] ?? '') ?>" 
                                                   class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                                   placeholder="https://example.com/image.jpg">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>

                <div class="flex justify-between pt-6 border-t border-gray-200">
                    <a href="/admin/pages/" class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors">
                        <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
                            <path fill-rule="evenodd" d="M7.707 14.707a1 1 0 01-1.414 0L2.586 11H9a1 1 0 010 2H2.586l3.707 3.707a1 1 0 01-1.414 1.414l-5.414-5.414a1 1 0 010-1.414L4.879 6.879a1 1 0 011.414 1.414L2.586 11H9a1 1 0 010 2H2.586l3.707 3.707z" clip-rule="evenodd"/>
                        </svg>
                        Cancel
                    </a>

                    <button type="submit" class="inline-flex items-center px-6 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors">
                        <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
                            <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
                        </svg>
                        <?= $action === 'create' ? 'Create Page' : 'Update Page' ?>
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<?php
$content = ob_get_clean();

include __DIR__ . '/../partials/layout.php';
?>