{% extends 'base/base.html' %}
{% load static %}

{% block title %}{% if is_edit %}Edit{% else %}Add{% endif %} Ride - ThrillWiki{% endblock %}

{% block content %}
<div class="container px-4 py-8 mx-auto">
    <div class="max-w-4xl mx-auto">
        <h1 class="mb-6 text-3xl font-bold">
            {% if is_edit %}Edit {{ object.name }}{% else %}Add New Ride{% endif %}
            {% if park %}
            <div class="mt-2 text-lg font-normal text-gray-600 dark:text-gray-400">
                at <a href="{% url 'parks:park_detail' park.slug %}" class="text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300">{{ park.name }}</a>
            </div>
            {% endif %}
        </h1>

        <form method="post" enctype="multipart/form-data" class="space-y-6" role="form" aria-label="{% if is_edit %}Edit ride{% else %}Add new ride{% endif %}" x-data="{
            status: '{{ form.instance.status|default:'OPERATING' }}',
            clearResults(containerId) {
                const container = document.getElementById(containerId);
                if (container && !container.contains(event.target)) {
                    container.querySelector('[id$=search-results]').innerHTML = '';
                }
            },
            handleStatusChange(event) {
                this.status = event.target.value;
                if (this.status === 'CLOSING') {
                    document.getElementById('id_closing_date').required = true;
                } else {
                    document.getElementById('id_closing_date').required = false;
                }
            },
            showClosingDate() {
                return ['CLOSING', 'SBNO', 'CLOSED_PERM', 'DEMOLISHED', 'RELOCATED'].includes(this.status);
            }
        }">
            {% csrf_token %}

            {% if not park %}
            {# Park Selection - Only shown when creating from global view #}
            <fieldset class="p-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <legend class="mb-4 text-xl font-semibold">Park Information</legend>
                <div class="space-y-4">
                    <div id="park-search-container" class="relative" @click.outside="clearResults('park-search-container')">
                        {% include 'forms/partials/form_field.html' with field=form.park_search label="Park" %}
                        {{ form.park }}
                        <div id="park-search-results" class="relative" role="listbox" aria-label="Park search results"></div>
                        {% if form.park.errors %}
                            <div class="mt-1 text-sm text-red-600 dark:text-red-400" role="alert">
                                {{ form.park.errors }}
                            </div>
                        {% endif %}
                    </div>
                </div>
            </fieldset>
            {% endif %}

            {# Basic Information #}
            <fieldset class="p-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <legend class="mb-4 text-xl font-semibold">Basic Information</legend>
                <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                    <div class="col-span-2">
                        {% include 'forms/partials/form_field.html' with field=form.name label="Name" %}
                    </div>

                    <div class="col-span-2">
                        {% include 'forms/partials/form_field.html' with field=form.category label="Category" %}
                    </div>

                    <div id="coaster-fields" class="col-span-2"></div>
                </div>
            </fieldset>

            {# Manufacturer and Model #}
            <fieldset class="p-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <legend class="mb-4 text-xl font-semibold">Manufacturer and Model</legend>
                <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                    <div id="manufacturer-search-container" class="relative" @click.outside="clearResults('manufacturer-search-container')">
                        {% include 'forms/partials/form_field.html' with field=form.manufacturer_search label="Manufacturer" %}
                        {{ form.manufacturer }}
                        <div id="manufacturer-search-results" class="relative" role="listbox" aria-label="Manufacturer search results"></div>
                    </div>

                    <div id="designer-search-container" class="relative" @click.outside="clearResults('designer-search-container')">
                        {% include 'forms/partials/form_field.html' with field=form.designer_search label="Designer" %}
                        {{ form.designer }}
                        <div id="designer-search-results" class="relative" role="listbox" aria-label="Designer search results"></div>
                    </div>

                    <div id="ride-model-search-container" class="relative col-span-2" @click.outside="clearResults('ride-model-search-container')">
                        {% include 'forms/partials/form_field.html' with field=form.ride_model_search label="Ride Model" %}
                        {{ form.ride_model }}
                        <div id="ride-model-search-results" class="relative" role="listbox" aria-label="Ride model search results"></div>
                    </div>
                </div>
            </fieldset>

            {# Status and Dates #}
            <fieldset class="p-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <legend class="mb-4 text-xl font-semibold">Status and Dates</legend>
                <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                    <div>
                        {% include 'forms/partials/form_field.html' with field=form.status label="Status" %}
                    </div>

                    <div>
                        {% include 'forms/partials/form_field.html' with field=form.status_since label="Status Since" %}
                    </div>

                    <div>
                        {% include 'forms/partials/form_field.html' with field=form.opening_date label="Opening Date" %}
                    </div>

                    <div x-show="showClosingDate()">
                        {% include 'forms/partials/form_field.html' with field=form.closing_date label="Closing Date" %}
                        <p x-show="status === 'CLOSING'" class="mt-1 text-xs text-red-600" role="alert">Required when status is "Closing"</p>
                    </div>

                    <div x-show="status === 'CLOSING'" class="col-span-2">
                        {% include 'forms/partials/form_field.html' with field=form.post_closing_status label="Status After Closing" %}
                    </div>
                </div>
            </fieldset>

            {# Specifications #}
            <fieldset class="p-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <legend class="mb-4 text-xl font-semibold">Specifications</legend>
                <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                    <div>
                        {% include 'forms/partials/form_field.html' with field=form.min_height_in label="Minimum Height (inches)" %}
                    </div>

                    <div>
                        {% include 'forms/partials/form_field.html' with field=form.max_height_in label="Maximum Height (inches)" %}
                    </div>

                    <div>
                        {% include 'forms/partials/form_field.html' with field=form.capacity_per_hour label="Hourly Capacity" %}
                    </div>

                    <div>
                        {% include 'forms/partials/form_field.html' with field=form.ride_duration_seconds label="Ride Duration (seconds)" %}
                    </div>
                </div>
            </fieldset>

            {# Description #}
            <fieldset class="p-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <legend class="mb-4 text-xl font-semibold">Description</legend>
                <div>
                    {% include 'forms/partials/form_field.html' with field=form.description label="Description" %}
                </div>
            </fieldset>

            {# Submission Details #}
            <fieldset class="p-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <legend class="mb-4 text-xl font-semibold">Submission Details</legend>
                <div class="space-y-4">
                    <div>
                        <label for="id_reason" class="block mb-1 text-sm font-medium text-gray-700 dark:text-gray-300">
                            Reason for Changes
                        </label>
                        <textarea name="reason" id="id_reason" rows="2"
                            class="w-full px-4 py-2 border border-gray-300 rounded-lg form-textarea dark:border-gray-600 dark:bg-gray-700 dark:text-white"
                            placeholder="Explain why you're making these changes"
                            aria-describedby="reason-help"></textarea>
                        <p id="reason-help" class="mt-1 text-xs text-gray-500 dark:text-gray-400">Optional: Provide context for reviewers</p>
                    </div>
                    <div>
                        <label for="id_source" class="block mb-1 text-sm font-medium text-gray-700 dark:text-gray-300">
                            Source
                        </label>
                        <input type="text" name="source" id="id_source"
                            class="w-full px-4 py-2 border border-gray-300 rounded-lg form-input dark:border-gray-600 dark:bg-gray-700 dark:text-white"
                            placeholder="Where did you get this information?"
                            aria-describedby="source-help">
                        <p id="source-help" class="mt-1 text-xs text-gray-500 dark:text-gray-400">Optional: Link or reference to your source</p>
                    </div>
                </div>
            </fieldset>

            {# Submit Button #}
            <div class="flex justify-end">
                <button type="submit" class="px-6 py-2 text-white bg-blue-600 rounded-lg hover:bg-blue-700 dark:hover:bg-blue-800">
                    {% if is_edit %}Save Changes{% else %}Add Ride{% endif %}
                </button>
            </div>
        </form>
    </div>
</div>
{% endblock %}
