{{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">
                        
                    <div x-data="{
                        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
                        },
                    }">
                        <input type="hidden" name="beneficiaryid" x-model="fields.beneficiaryid">
                        <label for="combobox" class="block text-sm font-medium text-gray-700">Bénéficiaire</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</label>
                                <div class="mt-1">
                                    <input type="date" id="startdate" name="startdate" value="{{.ViewState.search.startdate}}"
                                        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</label>
                                <div class="mt-1">
                                    <input type="date" id="enddate" name="enddate" value="{{.ViewState.search.enddate}}"
                                        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"
                          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">Documents demandés</h2>
              <table>
                <tbody>
                  {{range .ViewState.search.mandatory_documents}}
                  <tr>
                    <td class="whitespace-nowrap py-4 px-3 text-sm text-gray-500">
                      {{index $.ViewState.search.file_types_map .}}
                    </td>
                  </tr>
                  {{end}}
                </tbody>
              </table>
            </div>            
            <div class="bg-white mt-8 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">Numéro</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="whitespace-nowrap 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>
                              </td>
                              <td class="whitespace-nowrap py-4 px-3 text-sm text-gray-500">{{.Data.licence_plate}}</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="whitespace-nowrap 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-lg bg-white px-4 pt-5 pb-4 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-sm sm:p-6">
                                        <div>
                                          <div class="mx-auto flex h-12 w-12 items-center justify-center rounded-co bg-co-blue">
                                            {{$.IconSet.Icon "hero:outline/folder-plus" "h-6 w-6 text-white"}}
                                          </div>
                                          <div class="mt-3 text-center sm:mt-5">
                                            <h3 class="text-lg font-medium leading-6 text-gray-900" id="modal-title">Documents demandés</h3>
                                            <div class="mt-2">
                                              <p class="text-sm text-gray-500">Ajoutez des documents pour finaliser</p>
                                            </div>
                                          </div>
                                        </div>
                                        <form enctype="multipart/form-data" method="POST" action="/app/vehicles/v/{{.ID}}/b/{{$.ViewState.search.beneficiary.ID}}?startdate={{$.ViewState.search.startdate}}&enddate={{$.ViewState.search.enddate}}">
                                          {{range $.ViewState.search.mandatory_documents}}
                                          {{$type := .}}
                                          <div class="p-2"
                                            x-data="{
                                              select: '{{index $.ViewState.search.documents_defaults $type}}'
                                            }">
                                            <label for="type" class="block text-sm font-medium text-gray-700">{{index $.ViewState.search.file_types_map $type}}</label>
                                            <select x-model="select" id="type-{{$type}}" name="type-{{$type}}" class="mt-1 block w-full rounded-2xl border-gray-300 py-2 pl-3 pr-10 text-base focus:border-co-blue focus:outline-none focus:ring-co-blue sm:text-sm">
                                            {{range $.ViewState.search.beneficiary_documents}}
                                              {{if eq $type .Metadata.Type}}
                                              <option value="{{.Key}}">Fichier bénéficiaire : {{.Metadata.Name}}</option>
                                              {{end}}
                                            {{end}}
                                              <option value="">Ajouter un fichier</option>
                                            </select>
                                            <div x-show="select == ''" class="p-2">
                                              <input type="file" name="doc-{{$type}}" />
                                            </div>
                                          </div>
                                          {{end}}
                                          <div class="mt-5 sm:mt-6">
                                            <button type="submit" class="inline-flex w-full justify-center rounded-2xl border border-transparent bg-co-blue px-4 py-2 text-base font-medium text-white shadow-sm hover:bg-co-blue focus:outline-none focus:ring-2 focus:ring-co-blue focus:ring-offset-2 sm:text-sm">Réserver</button>
                                          </div>
                                        </form>
                                        <div class="mt-5 sm:mt-6">
                                          <button @click="documentsdialog=false" type="button" class="inline-flex w-full justify-center max-w-xs bg-white hover:bg-gray-50 border-gray-300 border px-4 py-2 text-gray-700 items-center text-sm rounded-2xl focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-co-blue">Annuler</button>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </td>
                            </tr>
                            {{end}}
                          </tbody>
                        </table>
                      </div>
                    </div>
                  </div>
            </div>
            {{end}}
        </div>
    </div>
</div>
{{end}}