{# app/modules/cms/templates/cms/admin/content-page-edit.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/alerts.html' import loading_state, error_state, alert_dynamic %}
{% from 'shared/macros/headers.html' import page_header_flex, back_button, action_button %}
{% from 'shared/macros/inputs.html' import number_stepper %}
{% from 'shared/macros/richtext.html' import quill_css, quill_js, quill_editor %}

{% block title %}{% if page_id %}Edit{% else %}Create{% endif %} Content Page{% endblock %}

{% block alpine_data %}contentPageEditor({{ page_id if page_id else 'null' }}){% endblock %}

{% block quill_css %}
{{ quill_css() }}
{% endblock %}

{% block quill_script %}
{{ quill_js() }}
{% endblock %}

{% block content %}
{# Dynamic title/subtitle and save button text based on create vs edit mode #}
<div class="flex items-center justify-between my-6">
    <div>
        <h2 class="text-2xl font-semibold text-gray-700 dark:text-gray-200" x-text="pageId ? 'Edit Content Page' : 'Create Content Page'"></h2>
        <p class="text-sm text-gray-600 dark:text-gray-400 mt-1">
            <span x-show="!pageId">Create a new platform default or store-specific page</span>
            <span x-show="pageId">Modify an existing content page</span>
        </p>
    </div>
    <div class="flex items-center space-x-3">
        {{ back_button('/admin/content-pages', 'Back to List') }}
        <button
            @click="savePage()"
            :disabled="saving"
            class="flex items-center px-4 py-2 text-sm font-medium leading-5 transition-colors duration-150 border rounded-lg focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed text-white bg-purple-600 border-transparent hover:bg-purple-700 focus:shadow-outline-purple"
        >
            <span x-show="!saving" x-html="$icon('check', 'w-4 h-4 mr-2')"></span>
            <span x-show="saving" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
            <span x-text="saving ? 'Saving...' : (pageId ? 'Update Page' : 'Create Page')"></span>
        </button>
    </div>
</div>

{{ loading_state('Loading page...') }}

{{ error_state('Error', show_condition='error && !loading') }}

{{ alert_dynamic(type='success', message_var='successMessage', show_condition='successMessage') }}

<!-- Main Form -->
<div x-show="!loading" class="bg-white dark:bg-gray-800 rounded-lg shadow-md overflow-hidden">
    <form @submit.prevent="savePage()">
        <!-- Basic Information -->
        <div class="p-6 border-b border-gray-200 dark:border-gray-700">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">
                Basic Information
            </h3>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <!-- Page Type -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Page Type
                    </label>
                    <select
                        x-model="pageType"
                        @change="updatePageType()"
                        class="w-full px-3 py-2 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:border-purple-500 dark:bg-gray-700"
                    >
                        <option value="content">Content Page</option>
                        <option value="landing">Landing Page</option>
                    </select>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        <span x-show="pageType === 'content'">Standard page with rich text content (About, FAQ, Privacy...)</span>
                        <span x-show="pageType === 'landing'">Section-based page with hero, features, CTA blocks</span>
                    </p>
                </div>

                <!-- Slug -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Slug <span class="text-red-500">*</span>
                    </label>
                    <input
                        type="text"
                        x-model="form.slug"
                        required
                        maxlength="100"
                        pattern="[a-z0-9\-_]+"
                        class="w-full px-3 py-2 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:border-purple-500 dark:bg-gray-700"
                        placeholder="about"
                    >
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        URL-safe identifier (lowercase, numbers, hyphens, underscores only)
                    </p>
                </div>

                <!-- Platform Selection -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Platform <span class="text-red-500">*</span>
                    </label>
                    <select
                        x-model="form.platform_id"
                        class="w-full px-3 py-2 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:border-purple-500 dark:bg-gray-700"
                        :disabled="loadingPlatforms"
                    >
                        <template x-for="plat in (platforms || [])" :key="plat.id">
                            <option :value="plat.id" x-text="plat.name"></option>
                        </template>
                    </select>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        Which platform this page belongs to (e.g., OMS, Loyalty+)
                    </p>
                </div>

                <!-- Store Override (optional) -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Store Override
                    </label>
                    <select
                        x-model="form.store_id"
                        class="w-full px-3 py-2 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:border-purple-500 dark:bg-gray-700"
                        :disabled="loadingStores"
                    >
                        <option :value="null">None (Platform Default)</option>
                        <template x-for="store in (stores || [])" :key="store.id">
                            <option :value="store.id" x-text="store.name"></option>
                        </template>
                    </select>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        <span x-show="!form.store_id">This is a platform-wide default page</span>
                        <span x-show="form.store_id">This page overrides the default for selected store only</span>
                    </p>
                </div>
            </div>
        </div>

        <!-- Title with Language Tabs -->
        <div class="p-6 border-b border-gray-200 dark:border-gray-700">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">
                Page Title
                <span class="text-sm font-normal text-gray-500 ml-2">(Multi-language)</span>
            </h3>

            <!-- Language Tabs for Title/Content -->
            <div class="mb-4">
                <div class="border-b border-gray-200 dark:border-gray-700">
                    <nav class="flex -mb-px space-x-4">
                        <template x-for="lang in supportedLanguages" :key="'tc-' + lang">
                            <button
                                type="button"
                                @click="titleContentLang = lang"
                                :class="titleContentLang === lang ? 'border-purple-500 text-purple-600 dark:text-purple-400' : 'border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300'"
                                class="py-2 px-4 border-b-2 font-medium text-sm transition-colors"
                            >
                                <span x-text="languageNames[lang] || lang.toUpperCase()"></span>
                                <span x-show="lang === defaultLanguage" class="ml-1 text-xs text-gray-400">(default)</span>
                            </button>
                        </template>
                    </nav>
                </div>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    Title <span class="text-red-500">*</span>
                    <span class="font-normal text-gray-400 ml-1" x-text="'(' + (languageNames[titleContentLang] || titleContentLang) + ')'"></span>
                </label>
                <input
                    type="text"
                    :value="getTranslatedTitle()"
                    @input="setTranslatedTitle($event.target.value)"
                    required
                    maxlength="200"
                    class="w-full px-3 py-2 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:border-purple-500 dark:bg-gray-700"
                    :placeholder="'Page title in ' + (languageNames[titleContentLang] || titleContentLang)"
                >
            </div>
        </div>

        <!-- Content (only for Content Page type) -->
        <div x-show="pageType === 'content'" x-cloak class="p-6 border-b border-gray-200 dark:border-gray-700">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">
                Page Content
                <span class="text-sm font-normal text-gray-500 ml-2" x-text="'(' + (languageNames[titleContentLang] || titleContentLang) + ')'"></span>
            </h3>

            <!-- Content Format -->
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    Content Format
                </label>
                <div class="flex gap-4">
                    <label class="flex items-center cursor-pointer">
                        <input type="radio" x-model="form.content_format" value="html" class="mr-2">
                        <span class="text-sm">HTML</span>
                    </label>
                    <label class="flex items-center cursor-pointer">
                        <input type="radio" x-model="form.content_format" value="markdown" class="mr-2">
                        <span class="text-sm">Markdown</span>
                    </label>
                </div>
            </div>

            <!-- Content Editor -->
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    Content <span class="text-red-500">*</span>
                </label>

                <!-- Rich Text Editor for HTML format -->
                <div x-show="form.content_format === 'html'" x-cloak>
                    {{ quill_editor(
                        id='content-editor',
                        model='form.content',
                        placeholder='Write your content here...',
                        min_height='300px',
                        toolbar='full',
                        help_text='Use the toolbar to format your content. Supports headings, lists, links, images, and more.'
                    ) }}
                </div>

                <!-- Plain textarea for Markdown format -->
                <div x-show="form.content_format === 'markdown'" x-cloak>
                    <textarea
                        x-model="form.content"
                        rows="12"
                        class="w-full px-3 py-2 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:border-purple-500 dark:bg-gray-700 font-mono text-sm"
                        placeholder="# Your heading here&#10;&#10;Write your **markdown** content..."
                    ></textarea>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        Enter Markdown content. Will be converted to HTML when displayed.
                    </p>
                </div>
            </div>

            {# Available Placeholders (for store default pages) #}
            {% set placeholders = [
                ('store_name', "The store's display name"),
                ('store_email', "The store's contact email"),
                ('store_phone', "The store's phone number"),
            ] %}
            <div x-show="!form.store_id || form.store_id === 'null'" x-cloak
                 class="mb-4 p-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg">
                <h4 class="text-sm font-semibold text-blue-800 dark:text-blue-300 mb-2 flex items-center">
                    <span x-html="$icon('information-circle', 'w-4 h-4 mr-1.5')"></span>
                    Available Placeholders
                </h4>
                <p class="text-xs text-blue-700 dark:text-blue-400 mb-2">
                    Use these placeholders in store default pages. They will be automatically replaced with the store's actual information when displayed.
                </p>
                <div class="flex flex-wrap gap-2">
                    {% for name, description in placeholders %}
                    <code class="px-2 py-1 text-xs bg-blue-100 dark:bg-blue-800/40 text-blue-800 dark:text-blue-300 rounded cursor-pointer hover:bg-blue-200 dark:hover:bg-blue-800/60 transition-colors"
                          @click="navigator.clipboard.writeText('{% raw %}{{{% endraw %}{{ name }}{% raw %}}}{% endraw %}')"
                          title="{{ description }} — click to copy">
                        {% raw %}{{{% endraw %}{{ name }}{% raw %}}}{% endraw %}
                    </code>
                    {% endfor %}
                </div>
                <p class="text-xs text-blue-600 dark:text-blue-500 mt-2">
                    Click a placeholder to copy it to your clipboard.
                </p>
            </div>
        </div>

        <!-- ══════════════════════════════════════════════════════════════════ -->
        <!-- SECTIONS EDITOR (for Landing Page type)                            -->
        <!-- ══════════════════════════════════════════════════════════════════ -->
        <div x-show="pageType === 'landing'" x-cloak class="p-6 border-b border-gray-200 dark:border-gray-700">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white">
                    Homepage Sections
                    <span class="text-sm font-normal text-gray-500 ml-2">(Multi-language content)</span>
                </h3>
                <span x-show="!sectionsLoaded" class="text-sm text-gray-500">
                    <span x-html="$icon('spinner', 'w-4 h-4 inline mr-1')"></span>
                    Loading sections...
                </span>
            </div>

            <!-- Language Tabs -->
            <div class="mb-6" x-show="sectionsLoaded">
                <div class="border-b border-gray-200 dark:border-gray-700">
                    <nav class="flex -mb-px space-x-4">
                        <template x-for="lang in supportedLanguages" :key="lang">
                            <button
                                type="button"
                                @click="currentLang = lang"
                                :class="currentLang === lang ? 'border-purple-500 text-purple-600 dark:text-purple-400' : 'border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300'"
                                class="py-2 px-4 border-b-2 font-medium text-sm transition-colors"
                            >
                                <span x-text="languageNames[lang] || lang.toUpperCase()"></span>
                                <span x-show="lang === defaultLanguage" class="ml-1 text-xs text-gray-400">(default)</span>
                            </button>
                        </template>
                    </nav>
                </div>
            </div>

            <!-- Section Accordions -->
            <div class="space-y-4" x-show="sectionsLoaded">

                <!-- ═══════════════════════════════════════════════════════════ -->
                <!-- HERO SECTION                                                 -->
                <!-- ═══════════════════════════════════════════════════════════ -->
                <div x-show="isSectionAvailable('hero')" class="border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden">
                    <button
                        type="button"
                        @click="openSection = openSection === 'hero' ? null : 'hero'"
                        class="w-full flex items-center justify-between p-4 text-left bg-gray-50 dark:bg-gray-700/50 hover:bg-gray-100 dark:hover:bg-gray-700"
                    >
                        <span class="font-medium text-gray-900 dark:text-white">Hero Section</span>
                        <div class="flex items-center space-x-3">
                            <label class="flex items-center" @click.stop>
                                <input type="checkbox" x-model="sections.hero.enabled" class="w-4 h-4 text-purple-600 rounded">
                                <span class="ml-2 text-sm text-gray-500">Enabled</span>
                            </label>
                            <span :class="openSection === 'hero' ? 'rotate-180' : ''" class="transition-transform" x-html="$icon('chevron-down', 'w-5 h-5 text-gray-400')"></span>
                        </div>
                    </button>
                    <div x-show="openSection === 'hero'" x-collapse class="p-4 space-y-4 border-t border-gray-200 dark:border-gray-700">
                        <!-- Badge Text -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Badge Text</label>
                            <input
                                type="text"
                                x-model="sections.hero.badge_text.translations[currentLang]"
                                class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-gray-900 dark:text-white"
                                :placeholder="'Badge text in ' + languageNames[currentLang]"
                            >
                        </div>
                        <!-- Title -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Title <span class="text-red-500">*</span></label>
                            <input
                                type="text"
                                x-model="sections.hero.title.translations[currentLang]"
                                class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-gray-900 dark:text-white"
                                :placeholder="'Hero title in ' + languageNames[currentLang]"
                            >
                        </div>
                        <!-- Subtitle -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Subtitle</label>
                            <textarea
                                x-model="sections.hero.subtitle.translations[currentLang]"
                                rows="2"
                                class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-gray-900 dark:text-white"
                                :placeholder="'Hero subtitle in ' + languageNames[currentLang]"
                            ></textarea>
                        </div>
                        <!-- Buttons -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Buttons</label>
                            <template x-for="(button, idx) in sections.hero.buttons" :key="idx">
                                <div class="flex gap-2 mb-2">
                                    <input
                                        type="text"
                                        x-model="button.text.translations[currentLang]"
                                        class="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm"
                                        :placeholder="'Button text'"
                                    >
                                    <input
                                        type="text"
                                        x-model="button.url"
                                        class="w-32 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm"
                                        placeholder="/signup"
                                    >
                                    <select x-model="button.style" class="w-28 px-2 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm">
                                        <option value="primary">Primary</option>
                                        <option value="secondary">Secondary</option>
                                        <option value="outline">Outline</option>
                                    </select>
                                    <button type="button" @click="removeButton('hero', idx)" class="px-3 py-2 text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-lg">
                                        <span x-html="$icon('trash', 'w-4 h-4')"></span>
                                    </button>
                                </div>
                            </template>
                            <button type="button" @click="addButton('hero')" class="text-sm text-purple-600 hover:text-purple-700 font-medium">
                                + Add Button
                            </button>
                        </div>
                    </div>
                </div>

                <!-- ═══════════════════════════════════════════════════════════ -->
                <!-- FEATURES SECTION                                             -->
                <!-- ═══════════════════════════════════════════════════════════ -->
                <div x-show="isSectionAvailable('features')" class="border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden">
                    <button
                        type="button"
                        @click="openSection = openSection === 'features' ? null : 'features'"
                        class="w-full flex items-center justify-between p-4 text-left bg-gray-50 dark:bg-gray-700/50 hover:bg-gray-100 dark:hover:bg-gray-700"
                    >
                        <span class="font-medium text-gray-900 dark:text-white">Features Section</span>
                        <div class="flex items-center space-x-3">
                            <label class="flex items-center" @click.stop>
                                <input type="checkbox" x-model="sections.features.enabled" class="w-4 h-4 text-purple-600 rounded">
                                <span class="ml-2 text-sm text-gray-500">Enabled</span>
                            </label>
                            <span :class="openSection === 'features' ? 'rotate-180' : ''" class="transition-transform" x-html="$icon('chevron-down', 'w-5 h-5 text-gray-400')"></span>
                        </div>
                    </button>
                    <div x-show="openSection === 'features'" x-collapse class="p-4 space-y-4 border-t border-gray-200 dark:border-gray-700">
                        <!-- Section Title -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Section Title</label>
                            <input
                                type="text"
                                x-model="sections.features.title.translations[currentLang]"
                                class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-gray-900 dark:text-white"
                                :placeholder="'Features title in ' + languageNames[currentLang]"
                            >
                        </div>
                        <!-- Feature Cards -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Feature Cards</label>
                            <template x-for="(feature, idx) in sections.features.features" :key="idx">
                                <div class="p-3 mb-3 bg-gray-50 dark:bg-gray-700/50 rounded-lg">
                                    <div class="flex items-center justify-between mb-2">
                                        <span class="text-sm font-medium text-gray-600 dark:text-gray-400">Feature <span x-text="idx + 1"></span></span>
                                        <button type="button" @click="removeFeature(idx)" class="text-red-500 hover:text-red-700">
                                            <span x-html="$icon('trash', 'w-4 h-4')"></span>
                                        </button>
                                    </div>
                                    <div class="grid grid-cols-1 md:grid-cols-3 gap-2">
                                        <input
                                            type="text"
                                            x-model="feature.icon"
                                            class="px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm"
                                            placeholder="Icon name (e.g., bolt)"
                                        >
                                        <input
                                            type="text"
                                            x-model="feature.title.translations[currentLang]"
                                            class="px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm"
                                            :placeholder="'Title'"
                                        >
                                        <input
                                            type="text"
                                            x-model="feature.description.translations[currentLang]"
                                            class="px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm"
                                            :placeholder="'Description'"
                                        >
                                    </div>
                                </div>
                            </template>
                            <button type="button" @click="addFeature()" class="text-sm text-purple-600 hover:text-purple-700 font-medium">
                                + Add Feature Card
                            </button>
                        </div>
                    </div>
                </div>

                <!-- ═══════════════════════════════════════════════════════════ -->
                <!-- PRICING SECTION                                              -->
                <!-- ═══════════════════════════════════════════════════════════ -->
                <div x-show="isSectionAvailable('pricing')" class="border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden">
                    <button
                        type="button"
                        @click="openSection = openSection === 'pricing' ? null : 'pricing'"
                        class="w-full flex items-center justify-between p-4 text-left bg-gray-50 dark:bg-gray-700/50 hover:bg-gray-100 dark:hover:bg-gray-700"
                    >
                        <span class="font-medium text-gray-900 dark:text-white">Pricing Section</span>
                        <div class="flex items-center space-x-3">
                            <label class="flex items-center" @click.stop>
                                <input type="checkbox" x-model="sections.pricing.enabled" class="w-4 h-4 text-purple-600 rounded">
                                <span class="ml-2 text-sm text-gray-500">Enabled</span>
                            </label>
                            <span :class="openSection === 'pricing' ? 'rotate-180' : ''" class="transition-transform" x-html="$icon('chevron-down', 'w-5 h-5 text-gray-400')"></span>
                        </div>
                    </button>
                    <div x-show="openSection === 'pricing'" x-collapse class="p-4 space-y-4 border-t border-gray-200 dark:border-gray-700">
                        <!-- Section Title -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Section Title</label>
                            <input
                                type="text"
                                x-model="sections.pricing.title.translations[currentLang]"
                                class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-gray-900 dark:text-white"
                                :placeholder="'Pricing title in ' + languageNames[currentLang]"
                            >
                        </div>
                        <!-- Use Subscription Tiers -->
                        <div class="flex items-center">
                            <input type="checkbox" x-model="sections.pricing.use_subscription_tiers" class="w-4 h-4 text-purple-600 rounded">
                            <span class="ml-2 text-sm text-gray-700 dark:text-gray-300">Use subscription tiers from database</span>
                        </div>
                        <p class="text-xs text-gray-500">When enabled, pricing cards are dynamically pulled from your subscription tier configuration.</p>
                    </div>
                </div>

                <!-- ═══════════════════════════════════════════════════════════ -->
                <!-- CTA SECTION                                                  -->
                <!-- ═══════════════════════════════════════════════════════════ -->
                <div x-show="isSectionAvailable('cta')" class="border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden">
                    <button
                        type="button"
                        @click="openSection = openSection === 'cta' ? null : 'cta'"
                        class="w-full flex items-center justify-between p-4 text-left bg-gray-50 dark:bg-gray-700/50 hover:bg-gray-100 dark:hover:bg-gray-700"
                    >
                        <span class="font-medium text-gray-900 dark:text-white">Call to Action Section</span>
                        <div class="flex items-center space-x-3">
                            <label class="flex items-center" @click.stop>
                                <input type="checkbox" x-model="sections.cta.enabled" class="w-4 h-4 text-purple-600 rounded">
                                <span class="ml-2 text-sm text-gray-500">Enabled</span>
                            </label>
                            <span :class="openSection === 'cta' ? 'rotate-180' : ''" class="transition-transform" x-html="$icon('chevron-down', 'w-5 h-5 text-gray-400')"></span>
                        </div>
                    </button>
                    <div x-show="openSection === 'cta'" x-collapse class="p-4 space-y-4 border-t border-gray-200 dark:border-gray-700">
                        <!-- Title -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Title</label>
                            <input
                                type="text"
                                x-model="sections.cta.title.translations[currentLang]"
                                class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-gray-900 dark:text-white"
                                :placeholder="'CTA title in ' + languageNames[currentLang]"
                            >
                        </div>
                        <!-- Subtitle -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Subtitle</label>
                            <textarea
                                x-model="sections.cta.subtitle.translations[currentLang]"
                                rows="2"
                                class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-gray-900 dark:text-white"
                                :placeholder="'CTA subtitle in ' + languageNames[currentLang]"
                            ></textarea>
                        </div>
                        <!-- Buttons -->
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Buttons</label>
                            <template x-for="(button, idx) in sections.cta.buttons" :key="idx">
                                <div class="flex gap-2 mb-2">
                                    <input
                                        type="text"
                                        x-model="button.text.translations[currentLang]"
                                        class="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm"
                                        :placeholder="'Button text'"
                                    >
                                    <input
                                        type="text"
                                        x-model="button.url"
                                        class="w-32 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm"
                                        placeholder="/signup"
                                    >
                                    <select x-model="button.style" class="w-28 px-2 py-2 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-700 text-sm">
                                        <option value="primary">Primary</option>
                                        <option value="secondary">Secondary</option>
                                        <option value="outline">Outline</option>
                                    </select>
                                    <button type="button" @click="removeButton('cta', idx)" class="px-3 py-2 text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-lg">
                                        <span x-html="$icon('trash', 'w-4 h-4')"></span>
                                    </button>
                                </div>
                            </template>
                            <button type="button" @click="addButton('cta')" class="text-sm text-purple-600 hover:text-purple-700 font-medium">
                                + Add Button
                            </button>
                        </div>
                    </div>
                </div>

            </div>
        </div>

        <!-- SEO Settings -->
        <div class="p-6 border-b border-gray-200 dark:border-gray-700">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">
                SEO & Metadata
                <span class="text-sm font-normal text-gray-500 ml-2" x-text="'(' + (languageNames[titleContentLang] || titleContentLang) + ')'"></span>
            </h3>

            <div class="space-y-4">
                <!-- Meta Description -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Meta Description
                    </label>
                    <textarea
                        :value="getTranslatedMetaDescription()"
                        @input="setTranslatedMetaDescription($event.target.value)"
                        rows="2"
                        maxlength="300"
                        class="w-full px-3 py-2 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:border-purple-500 dark:bg-gray-700"
                        :placeholder="'Meta description in ' + (languageNames[titleContentLang] || titleContentLang)"
                    ></textarea>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        150-160 characters recommended for search engines
                    </p>
                </div>
            </div>
        </div>

        <!-- Navigation & Display Settings -->
        <div class="p-6 border-b border-gray-200 dark:border-gray-700">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">
                Navigation & Display
            </h3>

            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                <!-- Display Order -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Display Order
                    </label>
                    {{ number_stepper(model='form.display_order', min=0, max=100, step=1, label='Display Order') }}
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">Lower = first</p>
                </div>

                <!-- Show in Header -->
                <div class="flex items-center">
                    <label class="flex items-center cursor-pointer">
                        <input
                            type="checkbox"
                            x-model="form.show_in_header"
                            class="w-5 h-5 text-purple-600 border-gray-300 rounded focus:ring-purple-500"
                        >
                        <span class="ml-3 text-sm font-medium text-gray-900 dark:text-white">
                            Show in Header
                        </span>
                    </label>
                </div>

                <!-- Show in Footer -->
                <div class="flex items-center">
                    <label class="flex items-center cursor-pointer">
                        <input
                            type="checkbox"
                            x-model="form.show_in_footer"
                            class="w-5 h-5 text-purple-600 border-gray-300 rounded focus:ring-purple-500"
                        >
                        <span class="ml-3 text-sm font-medium text-gray-900 dark:text-white">
                            Show in Footer
                        </span>
                    </label>
                </div>

                <!-- Show in Legal (Bottom Bar) -->
                <div class="flex items-center">
                    <label class="flex items-center cursor-pointer">
                        <input
                            type="checkbox"
                            x-model="form.show_in_legal"
                            class="w-5 h-5 text-purple-600 border-gray-300 rounded focus:ring-purple-500"
                        >
                        <span class="ml-3 text-sm font-medium text-gray-900 dark:text-white">
                            Show in Legal
                        </span>
                    </label>
                    <span class="ml-2 text-gray-400 dark:text-gray-500 cursor-help" title="Bottom bar next to copyright">
                        <span x-html="$icon('information-circle', 'w-4 h-4')"></span>
                    </span>
                </div>
            </div>
        </div>

        <!-- Publishing Settings -->
        <div class="p-6 bg-gray-50 dark:bg-gray-700/50">
            <div class="flex items-center justify-between">
                <div>
                    <label class="flex items-center cursor-pointer">
                        <input
                            type="checkbox"
                            x-model="form.is_published"
                            class="w-5 h-5 text-purple-600 border-gray-300 rounded focus:ring-purple-500"
                        >
                        <span class="ml-3 text-sm font-medium text-gray-900 dark:text-white">
                            Published
                        </span>
                    </label>
                    <p class="ml-8 text-xs text-gray-500 dark:text-gray-400">
                        Make this page visible to the public
                    </p>
                </div>

                <div class="flex gap-2">
                    <a
                        href="/admin/content-pages"
                        class="px-6 py-2 text-sm font-medium leading-5 text-gray-700 dark:text-gray-200 transition-colors duration-150 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg hover:border-gray-400 dark:hover:border-gray-500"
                    >
                        Cancel
                    </a>
                    <button
                        type="submit"
                        :disabled="saving"
                        class="px-6 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple disabled:opacity-50 disabled:cursor-not-allowed"
                    >
                        <span x-text="saving ? 'Saving...' : (pageId ? 'Update Page' : 'Create Page')"></span>
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('cms_static', path='admin/js/content-page-edit.js') }}"></script>
{% endblock %}
