{{define "content"}}
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
    <h1 class="text-2xl font-semibold text-gray-900">Véhicules partagés</h1>

    <div class="sm:flex sm:items-center">
        <div class="sm:flex-auto">
            <p class="mt-2 text-sm text-gray-700"></p>
        </div>
        <div class="mt-4 sm:mt-0 sm:ml-16 sm:flex-none">
            <a href="/app/vehicles/bookings/">
                <button type="button"
                    class="inline-flex items-center justify-center rounded-2xl border border-transparent bg-co-blue px-4 py-2 text-sm font-medium text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-co-blue focus:ring-offset-2 sm:w-auto">
                    Voir les mises à disposition
                </button>
            </a>
        </div>
    </div>

    <div class="mt-8 max-w-3xl mx-auto grid grid-cols-1 gap-6 lg:max-w-7xl lg:grid-flow-col-dense lg:grid-cols-3">
        <div class="space-y-6 lg:col-start-1 lg:col-span-1">
            <div class="bg-white shadow sm:rounded-2xl">
                <h2 id="timeline-title" class="text-lg font-medium text-gray-900 p-4 sm:px-6">Chercher un véhicule</h2>
                <div class="border-t border-gray-200 px-4 py-5 sm:px-6">
                    <form method="GET" x-data="{
                        beneficiarySelected: {{if .ViewState.search}}true{{else}}false{{end}},
                        text: '{{if .ViewState.search}}{{.ViewState.search.beneficiary.Data.first_name}} {{.ViewState.search.beneficiary.Data.last_name}}{{end}}',
                        beneficiariesListOpen: false,
                        beneficiaries: {{json .ViewState.beneficiaries}},
                        filteredBeneficiaries: (beneficiaries, text) => {
                            if(text=='') {
                              return beneficiaries
                            }
                            return beneficiaries.filter(b => b['data']['first_name'].toLowerCase().includes(text.toLowerCase()) || b['data']['last_name'].toLowerCase().includes(text.toLowerCase()))
                        },
                        fields: {
                            beneficiaryid: {{if .ViewState.search}}'{{.ViewState.search.beneficiary.ID}}'{{else}}null{{end}},
                        },
                        selectbeneficiary(beneficiary) {
                            this.fields.beneficiaryid = beneficiary.id
                            this.text = beneficiary.data.first_name + ' ' + beneficiary.data.last_name
                            this.beneficiariesListOpen = false
                            this.beneficiarySelected = true
                        },
                    }" @submit="if(!beneficiarySelected) { $event.preventDefault(); alert('Veuillez sélectionner un bénéficiaire'); }">

                    <div>
                        <input type="hidden" name="beneficiaryid" x-model="fields.beneficiaryid">
                        <label for="combobox" class="block text-sm font-medium text-gray-700">Bénéficiaire <span class="text-red-600">*</span></label>
                        <div class="relative mt-1 mb-4">
                          <input autocomplete="off" @focus="beneficiariesListOpen = true" x-model="text" id="combobox" type="text" class="w-full rounded-2xl border border-gray-300 bg-white py-2 pl-3 pr-12 shadow-sm focus:border-co-blue focus:outline-none focus:ring-1 focus:ring-co-blue sm:text-sm" role="combobox" aria-controls="options" aria-expanded="false">
                          
                          <button @click="beneficiariesListOpen = ! beneficiariesListOpen" type="button" class="absolute inset-y-0 right-0 flex items-center rounded-r-2xl px-2 focus:outline-none">
                            <!-- Heroicon name: solid/selector -->
                            <svg class="h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                              <path fill-rule="evenodd" d="M10 3a1 1 0 01.707.293l3 3a1 1 0 01-1.414 1.414L10 5.414 7.707 7.707a1 1 0 01-1.414-1.414l3-3A1 1 0 0110 3zm-3.707 9.293a1 1 0 011.414 0L10 14.586l2.293-2.293a1 1 0 011.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd" />
                            </svg>
                          </button>
                      
                          <ul x-show="beneficiariesListOpen" class="absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-xl bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm" id="options" role="listbox">
                            <!--
                              Combobox option, manage highlight styles based on mouseenter/mouseleave and keyboard navigation.
                      
                              Active: "text-white bg-indigo-600", Not Active: "text-gray-900"
                            -->
                            <template x-for="beneficiary in filteredBeneficiaries(beneficiaries, text)">
                                <li @click="selectbeneficiary(beneficiary)" class="relative cursor-default hover:bg-gray-100 select-none py-2 pl-3 pr-9 text-gray-900" id="option-0" role="option" tabindex="-1">
                                <!-- Selected: "font-semibold" -->
                                <span class="truncate" x-text="beneficiary.data.first_name"></span> <span class="truncate" x-text="beneficiary.data.last_name"></span>
                        
                                <!--
                                    Checkmark, only display for selected option.
                        
                                    Active: "text-white", Not Active: "text-indigo-600"
                                -->
                                <span class="absolute inset-y-0 right-0 flex items-center pr-4 text-co-blue">
                                    <!-- Heroicon name: solid/check -->
                                    <!-- <svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd" />
                                    </svg> -->
                                </span>
                                </li>
                            </template>
                      
                            <!-- More items... -->
                          </ul>
                        </div>
                      </div>


                        <!-- {{ $fieldName := "address" }}
                        {{ template "address_autocomplete" dict "FieldName" $fieldName }} -->
                        <div class="py-4 grid grid-cols-2">
                            <div class="lg:col-span-1">
                                <label for="startdate" class="block text-sm font-medium text-gray-700">Du <span class="text-red-600">*</span></label>
                                <div class="mt-1">
                                    <input type="date" id="startdate" name="startdate" value="{{if .ViewState.search}}{{(timeFrom .ViewState.search.startdate).Format "2006-01-02"}}{{end}}" required
                                        class="shadow-sm focus:ring-co-blue focus:border-co-blue block w-full sm:text-sm border-gray-300 rounded-l-2xl border-r-1">
                                </div>
                            </div>
                            <div class="lg:col-span-1">
                                <label for="enddate" class="block text-sm font-medium text-gray-700">Au <span class="text-red-600">*</span></label>
                                <div class="mt-1">
                                    <input type="date" id="enddate" name="enddate" value="{{if .ViewState.search}}{{(timeFrom .ViewState.search.enddate).Format "2006-01-02"}}{{end}}" required
                                        class="shadow-sm focus:ring-co-blue focus:border-co-blue block w-full sm:text-sm border-gray-300 rounded-r-2xl border-l-0">
                                </div>
                            </div>

                        </div>
                        <div class="py-4 grid grid-cols-2 gap-4">
                            <div class="lg:col-span-1">
                                <label for="starttime" class="block text-sm font-medium text-gray-700">Prise en charge</label>
                                <div class="mt-1">
                                    <input type="time" id="starttime" name="starttime" value="{{if .ViewState.search}}{{(timeFrom .ViewState.search.startdate).Format "15:04"}}{{else}}09:00{{end}}"
                                        class="shadow-sm focus:ring-co-blue focus:border-co-blue block w-full sm:text-sm border-gray-300 rounded-2xl">
                                </div>
                            </div>
                            <div class="lg:col-span-1">
                                <label for="endtime" class="block text-sm font-medium text-gray-700">Restitution</label>
                                <div class="mt-1">
                                    <input type="time" id="endtime" name="endtime" value="{{if .ViewState.search}}{{(timeFrom .ViewState.search.enddate).Format "15:04"}}{{else}}18:00{{end}}"
                                        class="shadow-sm focus:ring-co-blue focus:border-co-blue block w-full sm:text-sm border-gray-300 rounded-2xl">
                                </div>
                            </div>
                        </div>
                        <div class="py-4"
                          x-data="{
                            type: {{if .ViewState.searched}}'{{.ViewState.search.selected_type}}'{{else}}null{{end}},

                            displayAutomatic(type) {
                              return type == 'Voiture'
                            }
                          }">
                          <label for="type" class="block text-sm font-medium text-gray-700">Type de véhicule</label>
                            <select id="type" name="type" 
                                x-model="type"
                                class="max-w-lg mt-1 block focus:ring-co-blue focus:border-co-blue w-full shadow-sm sm:max-w-xs sm:text-sm rounded-2xl border-gray-300">
                                <option value=""></option>
                                {{range .ViewState.vehicles_types}}
                                <option value="{{.}}"{{ if eq $.ViewState.search.selected_type . }} selected{{end}}>{{.}}</option>
                                {{end}}
                            </select>
                            <div class="m-4" x-show="displayAutomatic(type)">
                                <legend class="sr-only">Automatique</legend>
                                <div class="relative flex items-start">
                                    <div class="flex h-5 items-center">
                                    <input {{if .ViewState.search.automatic }} checked{{end}}
                                      id="automatic" aria-describedby="automatic-description" name="automatic" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-co-blue focus:ring-co-blue">
                                    </div>
                                    <div class="ml-3 text-sm">
                                    <label for="automatic" class="font-medium text-gray-700">Automatique</label>
                                    <p id="automatic-description" class="text-gray-500">Rechercher uniquement un véhicule à boite automatique.</p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <button type="submit"
                            class="rounded-2xl border border-transparent bg-co-blue px-4 py-2 my-4 mt-8 w-full text-sm font-medium text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-co-blue focus:ring-offset-2 sm:w-auto">
                            Chercher
                        </button>
                    </form>
                </div>
            </div>
        </div>
        <div class="lg:col-start-2 lg:col-span-2">
            {{if .ViewState.searched}}
            <div class="bg-white px-4 py-5 shadow sm:rounded-2xl sm:px-6">
                <h2 id="timeline-title" class="text-lg font-medium text-gray-900">Véhicules disponibles</h2>
                <div class="mt-8 flex flex-col">
                    <div class="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
                      <div class="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
                        <table class="min-w-full divide-y divide-gray-300">
                          <thead>
                            <tr>
                              <th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6 md:pl-0">Véhicule</th>
                              <th scope="col" class="py-3.5 px-3 text-left text-sm font-semibold text-gray-900">Gestionnaire</th>
                              <th scope="col" class="py-3.5 px-3 text-left text-sm font-semibold text-gray-900">Lieu</th>
                              <th scope="col" class="relative py-3.5 pl-3 pr-4 sm:pr-6 md:pr-0">
                                <span class="sr-only">Réserver</span>
                              </th>
                            </tr>
                          </thead>
                          <tbody class="divide-y divide-gray-200">
                            {{range .ViewState.search.vehicles}}
                            <tr>
                              <td class="py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6 md:pl-0">
                                <div class="font-medium text-gray-900">{{.Data.name}}</div>
                                <div class="text-gray-500">
                                  {{.Type}}
                                  {{if eq .Type "Voiture"}}
                                  {{ if .Data.automatic}} (boite auto){{ end }}
                                  {{end}}
                                </div>
                                {{if .Data.licence_plate}}
                                <div class="text-gray-500 text-xs mt-1">{{.Data.licence_plate}}</div>
                                {{end}}
                              </td>
                              <td class="whitespace-nowrap py-4 px-3 text-sm text-gray-500">{{(index $.ViewState.search.admingroups (index .Administrators 0)).Data.name}}</td>
                              <td class="py-4 px-3 text-sm text-gray-500">{{if .Data.address}}{{.Data.address.properties.label}}{{end}}</td>
                              <td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 md:pr-0"
                                x-data="{
                                  documentsdialog: false
                                }">
                                <!-- <a href="/app/vehicles/v/{{.ID}}/b/{{$.ViewState.search.beneficiary.ID}}?startdate={{$.ViewState.search.startdate}}&enddate={{$.ViewState.search.enddate}}" class="text-co-blue hover:text-co-blue">Réserver<span class="sr-only"> pour {{$.ViewState.search.beneficiary.Data.first_name}} {{$.ViewState.search.beneficiary.Data.last_name}}</span></a> -->
                                <a href="#" @click="documentsdialog = !documentsdialog" class="text-co-blue hover:text-co-blue">Réserver<span class="sr-only"> pour {{$.ViewState.search.beneficiary.Data.first_name}} {{$.ViewState.search.beneficiary.Data.last_name}}</span></a>
                                <div class="relative z-10" aria-labelledby="modal-title" role="dialog" aria-modal="true"
                                x-show="documentsdialog">
                                  <div class="fixed inset-0 bg-gray-900 bg-opacity-30 transition-opacity"></div>
                                
                                  <div class="fixed inset-0 z-10 overflow-y-auto">
                                    <div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
                                      <div class="relative transform overflow-hidden rounded-2xl bg-white text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg">
                                        <!-- Header -->
                                        <div class="bg-co-blue px-6 py-5">
                                          <div class="flex items-center">
                                            <div class="flex-shrink-0 flex h-12 w-12 items-center justify-center rounded-xl bg-white/20 backdrop-blur">
                                              {{$.IconSet.Icon "hero:outline/document-text" "h-6 w-6 text-white"}}
                                            </div>
                                            <div class="ml-4">
                                              <h3 class="text-lg font-semibold text-white" id="modal-title">Documents demandés</h3>
                                              <p class="text-sm text-white/70">{{len $.ViewState.search.mandatory_documents}} document(s) requis pour cette réservation</p>
                                            </div>
                                          </div>
                                        </div>

                                        <form enctype="multipart/form-data" method="POST" action="/app/vehicles/v/{{.ID}}/b/{{$.ViewState.search.beneficiary.ID}}?startdate={{(timeFrom $.ViewState.search.startdate).Format "2006-01-02"}}&enddate={{(timeFrom $.ViewState.search.enddate).Format "2006-01-02"}}&starttime={{(timeFrom $.ViewState.search.startdate).Format "15:04"}}&endtime={{(timeFrom $.ViewState.search.enddate).Format "15:04"}}">
                                          <div class="px-6 py-5 space-y-4 max-h-96 overflow-y-auto">
                                            {{range $type := $.ViewState.search.mandatory_documents}}
                                            <div class="bg-gray-50 rounded-2xl p-4 border border-gray-200"
                                              x-data="{
                                                select: '{{index $.ViewState.search.documents_defaults $type}}',
                                                dragover: false
                                              }">
                                              <div class="flex items-start justify-between mb-3">
                                                <div class="flex items-center">
                                                  <span class="flex-shrink-0 inline-flex items-center justify-center h-8 w-8 rounded-xl bg-co-blue text-white">
                                                    {{$.IconSet.Icon "hero:outline/document" "h-4 w-4"}}
                                                  </span>
                                                  <div class="ml-3">
                                                    <label for="type-{{$type}}" class="block text-sm font-semibold text-gray-900">{{index $.ViewState.search.file_types_map $type}}</label>
                                                    <p class="text-xs text-gray-500">Document obligatoire</p>
                                                  </div>
                                                </div>
                                                <span x-show="select !== ''" class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-co-green/20 text-co-green">
                                                  {{$.IconSet.Icon "hero:solid/check-circle" "h-3.5 w-3.5 mr-1"}}
                                                  Sélectionné
                                                </span>
                                              </div>

                                              <select x-model="select" id="type-{{$type}}" name="type-{{$type}}"
                                                class="block w-full rounded-2xl border-gray-300 py-2.5 pl-3 pr-10 text-sm focus:border-co-blue focus:outline-none focus:ring-2 focus:ring-co-blue/20 transition-shadow">
                                                {{range $.ViewState.search.beneficiary_documents}}
                                                  {{if eq $type .Metadata.Type}}
                                                  <option value="{{.Key}}">{{.Metadata.Name}}</option>
                                                  {{end}}
                                                {{end}}
                                                <option value="">Téléverser un nouveau fichier</option>
                                              </select>

                                              <!-- File upload area -->
                                              <div x-show="select == ''" x-transition class="mt-3"
                                                x-data="{ fileName: '' }">
                                                <input type="file" name="doc-{{$type}}" x-ref="fileInput"
                                                  @change="fileName = $event.target.files[0]?.name || ''"
                                                  class="hidden" />
                                                <!-- Empty state: drop zone -->
                                                <div x-show="!fileName"
                                                  class="relative border-2 border-dashed rounded-2xl p-4 text-center transition-colors cursor-pointer"
                                                  :class="dragover ? 'border-co-blue bg-co-blue/5' : 'border-gray-300 hover:border-gray-400'"
                                                  @click="$refs.fileInput.click()"
                                                  @dragover.prevent="dragover = true"
                                                  @dragleave.prevent="dragover = false"
                                                  @drop.prevent="dragover = false; $refs.fileInput.files = $event.dataTransfer.files; fileName = $event.dataTransfer.files[0]?.name || ''">
                                                  <div class="space-y-1">
                                                    {{$.IconSet.Icon "hero:outline/cloud-arrow-up" "mx-auto h-8 w-8 text-gray-400"}}
                                                    <p class="text-sm text-gray-600">Glissez un fichier ou <span class="text-co-blue font-medium">parcourir</span></p>
                                                    <p class="text-xs text-gray-400">PDF, JPG, PNG jusqu'à 10Mo</p>
                                                  </div>
                                                </div>
                                                <!-- File selected state -->
                                                <div x-show="fileName" class="flex items-center justify-between p-3 bg-co-green/10 border border-co-green/30 rounded-2xl">
                                                  <div class="flex items-center min-w-0 cursor-pointer flex-1" @click="$refs.fileInput.click()">
                                                    {{$.IconSet.Icon "hero:solid/document-check" "h-5 w-5 text-co-green flex-shrink-0"}}
                                                    <span class="ml-2 text-sm text-gray-800 truncate" x-text="fileName"></span>
                                                    <span class="ml-2 text-xs text-co-green hover:text-co-green/80">(modifier)</span>
                                                  </div>
                                                  <button type="button" @click="fileName = ''; $refs.fileInput.value = ''" class="ml-2 flex-shrink-0 text-co-red hover:text-co-red/80">
                                                    {{$.IconSet.Icon "hero:outline/x-mark" "h-5 w-5"}}
                                                  </button>
                                                </div>
                                              </div>
                                            </div>
                                            {{end}}
                                          </div>

                                          <!-- Footer -->
                                          <div class="bg-gray-50 px-6 py-4 flex flex-col-reverse sm:flex-row sm:justify-end gap-3 border-t border-gray-200">
                                            <button @click="documentsdialog=false" type="button"
                                              class="w-full sm:w-auto inline-flex justify-center items-center px-4 py-2.5 border border-gray-300 rounded-2xl text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-co-blue transition-colors">
                                              Annuler
                                            </button>
                                            <button type="submit"
                                              class="w-full sm:w-auto inline-flex justify-center items-center px-6 py-2.5 border border-transparent rounded-2xl text-sm font-medium text-white bg-co-blue hover:bg-co-blue/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-co-blue shadow-sm transition-colors">
                                              {{$.IconSet.Icon "hero:outline/check" "h-4 w-4 mr-2"}}
                                              Confirmer la réservation
                                            </button>
                                          </div>
                                        </form>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </td>
                            </tr>
                            {{end}}
                          </tbody>
                        </table>
                      </div>
                    </div>
                  </div>
            </div>
            {{end}}
        </div>
    </div>
</div>
{{end}}
