<x-app-layout>
    <x-slot name="header">
        <div class="flex items-center justify-between">
            <h2 class="text-xl font-semibold leading-tight">
                {{ $event->exists ? __('Edit Event') : __('Create Event') }}
            </h2>

            {{-- “Back” breadcrumb --}}
            <a href="{{ route('calendar.show', $calendar) }}"
               class="text-sm text-gray-500 hover:text-gray-700">
                ← {{ $calendar->name }}
            </a>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-2xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white shadow-sm sm:rounded-lg p-6">
                <form method="POST"
                      action="{{ $event->exists
                        ? route('calendar.event.update', [$calendar, $event])
                        : route('calendar.event.store',  $calendar) }}">

                    @csrf
                    @if($event->exists)
                        @method('PUT')
                    @endif

                    {{-- Title --}}
                    <div class="mb-6">
                        <x-input-label for="title" :value="__('Title')" />
                        <x-text-input id="title" name="title" type="text" class="mt-1 block w-full"
                                      :value="old('title', $event->meta?->title ?? '')" required autofocus />
                        <x-input.error class="mt-2" :messages="$errors->get('title')" />
                    </div>

                    {{-- Description --}}
                    <div class="mb-6">
                        <x-input-label for="description" :value="__('Description')" />
                        <textarea id="description" name="description" rows="3"
                                  class="mt-1 block w-full rounded-md shadow-xs border-gray-300 focus:border-indigo-300 focus:ring-3">{{ old('description', $event->meta?->description ?? '') }}</textarea>
                        <x-input.error class="mt-2" :messages="$errors->get('description')" />
                    </div>

                    {{-- Location --}}
                    <div class="mb-6">
                        <x-input-label for="location" :value="__('Location')" />
                        <x-text-input id="location"
                                      name="location"
                                      type="text"
                                      class="mt-1 block w-full"
                                      :value="old('location', $event->meta?->location ?? '')"
                                      {{-- live suggestions via htmx --}}
                                      hx-get="{{ route('location.suggest') }}"
                                      hx-trigger="keyup changed delay:300ms"
                                      hx-target="#location-suggestions"
                                      hx-swap="innerHTML" />

                        {{-- suggestion dropdown target --}}
                        <div id="location-suggestions" class="relative z-20"></div>

                        {{-- hidden fields (filled when user clicks a suggestion; handy for step #2) --}}
                        <input type="hidden" id="loc_display_name" name="loc_display_name" />
                        <input type="hidden" id="loc_place_name" name="loc_place_name" />
                        <input type="hidden" id="loc_street" name="loc_street" />
                        <input type="hidden" id="loc_city" name="loc_city" />
                        <input type="hidden" id="loc_state" name="loc_state" />
                        <input type="hidden" id="loc_postal" name="loc_postal" />
                        <input type="hidden" id="loc_country" name="loc_country" />
                        <input type="hidden" id="loc_lat" name="loc_lat" />
                        <input type="hidden" id="loc_lon" name="loc_lon" />

                        <x-input.error class="mt-2" :messages="$errors->get('location')" />
                    </div>

                    {{-- Start / End --}}
                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-6 mb-6">

                        <div>
                            <x-input-label for="start_at" :value="__('Starts')" />
                            <x-text-input id="start_at" name="start_at" type="datetime-local"
                                          class="mt-1 block w-full"
                                          :value="old('start_at', $start)"
                                          required />
                            <x-input.error class="mt-2" :messages="$errors->get('start_at')" />
                        </div>

                        <div>
                            <x-input-label for="end_at" :value="__('Ends')" />
                            <x-text-input id="end_at" name="end_at" type="datetime-local"
                                          class="mt-1 block w-full"
                                          :value="old('end_at', $end)"
                                          required />
                            <x-input.error class="mt-2" :messages="$errors->get('end_at')" />
                        </div>

                    </div>

                    {{-- All-day --}}
                    <div class="flex items-center mb-6">
                        <input id="all_day" name="all_day" type="checkbox" value="1"
                               @checked(old('all_day', $event->meta?->all_day)) />
                        <label for="all_day" class="ms-2 text-sm text-gray-700">
                            {{ __('All day event') }}
                        </label>
                    </div>

                    {{-- Recurrence (advanced) --}}
                    <details class="mb-6">
                        <summary class="cursor-pointer text-sm text-gray-600">
                            {{ __('Repeat (advanced)') }}
                        </summary>
                        <div class="mt-3">
                            <x-input-label for="rrule" :value="__('RRULE')" />
                            <x-text-input id="rrule" name="rrule" type="text" class="mt-1 block w-full"
                                          :value="old('rrule', $rrule ?? '')" />
                            <p class="mt-1 text-xs text-gray-500">
                                Example: <code>FREQ=WEEKLY;BYDAY=MO,WE</code>
                            </p>
                            <x-input.error class="mt-2" :messages="$errors->get('rrule')" />
                        </div>
                    </details>

                    {{-- Submit --}}
                    <div class="flex justify-end space-x-2">
                        <a href="{{ route('calendar.show', $calendar) }}"
                           class="inline-flex items-center px-4 py-2 bg-gray-200 rounded-md">
                            {{ __('Cancel') }}
                        </a>

                        <x-primary-button>
                            {{ $event->exists ? __('Save') : __('Create') }}
                        </x-primary-button>
                    </div>

                </form>
            </div>
        </div>
    </div>
</x-app-layout>
