{# app/modules/cms/templates/cms/store/content-page-edit.html #}
{% extends "store/base.html" %}
{% from 'shared/macros/alerts.html' import loading_state, error_state, alert_dynamic %}
{% from 'shared/macros/headers.html' import back_button %}
{% from 'shared/macros/inputs.html' import number_stepper %}
{% from 'shared/macros/modals.html' import modal, confirm_modal_dynamic %}

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

{% block alpine_data %}storeContentPageEditor({{ page_id if page_id else 'null' }}){% 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 custom page for your shop</span>
            <span x-show="pageId && isOverride">Customize this platform default page</span>
            <span x-show="pageId && !isOverride">Edit your custom page</span>
        </p>
    </div>
    <div class="flex items-center space-x-3">
        {{ back_button('/store/' + store_code + '/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') }}

<!-- Override Info Banner -->
<div x-show="!loading && isOverride" class="mb-6 bg-purple-50 dark:bg-purple-900/20 border border-purple-200 dark:border-purple-800 rounded-lg p-4">
    <div class="flex items-start justify-between">
        <div class="flex">
            <span x-html="$icon('information-circle', 'w-5 h-5 text-purple-500 mr-3 flex-shrink-0 mt-0.5')"></span>
            <div>
                <h4 class="text-sm font-medium text-purple-800 dark:text-purple-200">Overriding Platform Default</h4>
                <p class="text-sm text-purple-700 dark:text-purple-300 mt-1">
                    You're customizing the "<span x-text="form.title"></span>" page. Your version will be shown to customers instead of the platform default.
                </p>
            </div>
        </div>
        <div class="flex items-center gap-2 ml-4 flex-shrink-0">
            <button
                @click="showDefaultPreview()"
                class="inline-flex items-center px-3 py-1.5 text-xs font-medium text-purple-700 bg-white dark:bg-purple-900/50 dark:text-purple-300 border border-purple-300 dark:border-purple-700 rounded-lg hover:bg-purple-50 dark:hover:bg-purple-900 transition-colors"
            >
                <span x-html="$icon('eye', 'w-4 h-4 mr-1')"></span>
                View Default
            </button>
            <button
                @click="showDeletePageConfirm = true"
                class="inline-flex items-center px-3 py-1.5 text-xs font-medium text-red-700 bg-white dark:bg-red-900/50 dark:text-red-300 border border-red-300 dark:border-red-700 rounded-lg hover:bg-red-50 dark:hover:bg-red-900 transition-colors"
            >
                <span x-html="$icon('arrow-uturn-left', 'w-4 h-4 mr-1')"></span>
                Revert to Default
            </button>
        </div>
    </div>
</div>

<!-- Default Content Preview Modal -->
{% call modal('defaultPreviewModal', 'Platform Default Content', 'showingDefaultPreview', size='lg', show_footer=false) %}
    <div x-show="loadingDefault" class="text-center py-8">
        <span x-html="$icon('spinner', 'inline w-8 h-8 text-purple-600')"></span>
        <p class="mt-2 text-sm text-gray-500">Loading default content...</p>
    </div>
    <div x-show="!loadingDefault && defaultContent">
        <h4 class="text-sm font-medium text-gray-500 dark:text-gray-400 mb-2">Title</h4>
        <p class="text-lg font-semibold text-gray-900 dark:text-white mb-4" x-text="defaultContent?.title"></p>
        <h4 class="text-sm font-medium text-gray-500 dark:text-gray-400 mb-2">Content</h4>
        <div class="prose dark:prose-invert max-w-none bg-gray-50 dark:bg-gray-700 rounded-lg p-4" x-html="defaultContent?.content"></div> <!-- noqa: SEC015 sanitized: admin-authored content -->
    </div>
{% endcall %}

<!-- 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 Title -->
                <div class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Page Title <span class="text-red-500">*</span>
                    </label>
                    <input
                        type="text"
                        x-model="form.title"
                        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="About Our Store"
                    >
                </div>

                <!-- Slug -->
                <div class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        URL Slug <span class="text-red-500">*</span>
                    </label>
                    <div class="flex items-center">
                        <span class="text-sm text-gray-500 dark:text-gray-400 mr-2">/</span>
                        <input
                            type="text"
                            x-model="form.slug"
                            required
                            maxlength="100"
                            pattern="[a-z0-9\-_]+"
                            :disabled="isOverride"
                            class="flex-1 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:opacity-50 disabled:cursor-not-allowed"
                            placeholder="about-us"
                        >
                    </div>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        <span x-show="!isOverride">URL-safe identifier (lowercase, numbers, hyphens, underscores only)</span>
                        <span x-show="isOverride">Slug cannot be changed for override pages</span>
                    </p>
                </div>
            </div>
        </div>

        <!-- Content -->
        <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 Content
            </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>
                <textarea
                    x-model="form.content"
                    required
                    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="<h2>Your content here...</h2>"
                ></textarea>
                <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                    <span x-show="form.content_format === 'html'">Enter HTML content. Basic HTML tags are supported.</span>
                    <span x-show="form.content_format === 'markdown'">Enter Markdown content. Will be converted to HTML.</span>
                </p>
            </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
            </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
                        x-model="form.meta_description"
                        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="A brief description for search engines"
                    ></textarea>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        <span x-text="(form.meta_description || '').length"></span>/300 characters (150-160 recommended)
                    </p>
                </div>

                <!-- Meta Keywords -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Meta Keywords
                    </label>
                    <input
                        type="text"
                        x-model="form.meta_keywords"
                        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="keyword1, keyword2, keyword3"
                    >
                </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 -->
                <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 your customers
                    </p>
                </div>

                <div class="flex gap-2">
                    <a
                        :href="`/store/${storeCode}/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>

{{ confirm_modal_dynamic(
    'deletePageModal',
    'Delete Page',
    "isOverride ? 'Are you sure you want to revert to the platform default? Your customizations will be lost.' : 'Are you sure you want to delete this page? This cannot be undone.'",
    'deletePage()',
    'showDeletePageConfirm',
    'Delete',
    'Cancel',
    'danger'
) }}
{% endblock %}

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