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

{% block title %}Search Rides - ThrillWiki{% endblock %}

{% block content %}
<div class="container mx-auto px-4 py-8">
    <div class="max-w-4xl mx-auto">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900 dark:text-white mb-2">Search Rides</h1>
            <p class="text-gray-600 dark:text-gray-400">Find rides across all theme parks</p>
        </div>

        <!-- Search Form -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xs border border-gray-200 dark:border-gray-700 p-6 mb-6">
            <form method="get" 
                  hx-get="{% url 'search:ride_search' %}" 
                  hx-target="#ride-search-results" 
                  hx-trigger="input changed delay:300ms from:input[name='ride'], submit"
                  hx-indicator="#search-loading">
                
                <div class="space-y-4">
                    <div>
                        <label for="{{ search_form.ride.id_for_label }}" 
                               class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                            Search Rides
                        </label>
                        {{ search_form.ride }}
                        {% if search_form.ride.help_text %}
                            <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">{{ search_form.ride.help_text }}</p>
                        {% endif %}
                        {% if search_form.ride.errors %}
                            <div class="mt-1 text-sm text-red-600 dark:text-red-400">
                                {{ search_form.ride.errors }}
                            </div>
                        {% endif %}
                    </div>

                    <div class="flex items-center justify-between">
                        <button type="submit" 
                                class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-xs text-white bg-blue-600 hover:bg-blue-700 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 dark:bg-blue-500 dark:hover:bg-blue-600">
                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
                            </svg>
                            Search
                        </button>
                        
                        <div id="search-loading" class="htmx-indicator">
                            <div class="flex items-center text-sm text-gray-500 dark:text-gray-400">
                                <svg class="animate-spin -ml-1 mr-2 h-4 w-4 text-blue-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                                    <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                                    <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                                </svg>
                                Searching...
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <!-- Search Results -->
        <div id="ride-search-results">
            {% include "search/partials/ride_search_results.html" %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    // Clear search results when input is empty
    document.addEventListener('DOMContentLoaded', function() {
        const searchInput = document.querySelector('input[name="ride"]');
        if (searchInput) {
            searchInput.addEventListener('input', function() {
                if (this.value.trim() === '') {
                    const resultsContainer = document.getElementById('ride-search-results');
                    if (resultsContainer) {
                        resultsContainer.innerHTML = '<div class="text-center text-gray-500 dark:text-gray-400 py-8">Start typing to search for rides...</div>';
                    }
                }
            });
        }
    });
</script>
{% endblock %}