{{ 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">Modifier le conducteur</h1>
</div>

<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8 mt-8"
    x-data="{
        fields: {
          first_name: '{{.ViewState.driver.Data.first_name}}',
          last_name: '{{.ViewState.driver.Data.last_name}}',
          email: '{{.ViewState.driver.Data.email}}' ,
          phone_number: '{{.ViewState.driver.Data.phone_number}}',
          birthdate: {{if .ViewState.driver.Data.birthdate}}'{{ (timeFrom .ViewState.driver.Data.birthdate).Format "2006-01-02" }}'{{else}}null{{end}},
        },
        other_properties: {{if .ViewState.driver.Data.other_properties}}{{ json .ViewState.driver.Data.other_properties }}{{else}}{}{{end}},
        other_properties_serialized: {{ json .ViewState.driver.Data.other_properties }},
        rules: {
            first_name: ['required'],
            last_name: ['required'],
            email: ['required', 'email'],
            phone_number: ['required', 'regexMatch:^((\\+)33|0)[1-9](\\d{2}){4}$'],
            birthdate: ['required'],
        },
        formValidation: {
            valid: false,
            fields: {
                first_name: {valid: null},
                last_name: {valid: null},
                email: {valid: null},
                phone_number: {valid: null},
                birthdate: {valid: null},
            }
        },
        isFormValid: true,
        validate() {
            this.formValidation = Iodine.assert(this.fields, this.rules)
        },
        validateField(field) {
            this.formValidation.fields[field] = Iodine.assert(this.fields[field], this.rules[field])
        },
        submit(event) {
            this.other_properties_serialized = JSON.stringify(this.other_properties)
            this.validate()
            if(!this.formValidation.valid) {
                this.isFormValid = false
                event.preventDefault()
            }
            return this.formValidation.valid
        }
    }">
    <form class="space-y-6" method="POST" @submit="submit">
        <input type="hidden" name="other_properties" x-model="other_properties_serialized" />
        <div class="bg-white shadow px-4 py-5 sm:rounded-lg sm:p-6">
            <div class="md:grid md:grid-cols-3 md:gap-6">
                <div class="md:col-span-1">
                    <h3 class="text-lg font-medium leading-6 text-gray-900">Informations obligatoires</h3>
                    <p class="mt-1 text-sm text-gray-500">Informations personnelles obligatoires pour créer le conducteur</p>
                </div>
                <div class="mt-5 md:mt-0 md:col-span-2">
                    <div class="grid grid-cols-6 gap-6">
                        <div class="col-span-6 sm:col-span-3">
                            <label for="first_name" class="block text-sm font-medium text-gray-700">Prénom</label>
                            <input type="text" name="first_name" id="first_name" autocomplete="given-name"
                                class="p-2 mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm  rounded-2xl"
                                x-model="fields.first_name" @blur="validateField('first_name')"
                                :class="formValidation.fields.first_name.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                        </div>

                        <div class="col-span-6 sm:col-span-3">
                            <label for="last_name" class="block text-sm font-medium text-gray-700">Nom</label>
                            <input type="text" name="last_name" id="last_name" autocomplete="family-name"
                                class="p-2 mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm rounded-2xl"
                                x-model="fields.last_name" @blur="validateField('last_name')"
                                :class="formValidation.fields.last_name.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                        </div>

                        <div class="col-span-6 sm:col-span-3">
                            <label for="email" class="block text-sm font-medium text-gray-700">Email</label>
                            <input type="text" name="email" id="email" autocomplete="email"
                                class="p-2 mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm rounded-2xl"
                                x-model="fields.email" @blur="validateField('email')"
                                :class="formValidation.fields.email.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                        </div>

                        <div class="col-span-6 sm:col-span-3">
                            <label for="phone_number" class="block text-sm font-medium text-gray-700">Numéro de
                                téléphone</label>
                            <input type="text" name="phone_number" id="phone_number" autocomplete="phone" placeholder="+33612345678"
                                class="p-2 mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm rounded-2xl"
                                x-model="fields.phone_number" @blur="validateField('phone_number')"
                                :class="formValidation.fields.phone_number.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                        </div>

                        <div class="col-span-6 sm:col-span-3">
                            <label for="birthdate" class="block text-sm font-medium text-gray-700">Date de
                                naissance</label>
                            <input type="date" name="birthdate" id="birthdate" autocomplete="birthdate" placeholder="JJ/MM/AAAA"
                                class="p-2 mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm rounded-2xl"
                                x-model="fields.birthdate" @blur="validateField('birthdate')"
                                :class="formValidation.fields.birthdate.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                        </div>
                    {{ $fieldName := "address" }}
                    {{if .ViewState.driver.Data.address}}
                    {{$default := .ViewState.driver.Data.address}}
                    {{ template "address" dict "FieldName" $fieldName  "Default" $default}}
                    {{else}}
                    {{ template "address_autocomplete" dict "FieldName" $fieldName}}
                    {{end}}
                    </div>
                </div>
            </div>
        </div>

        <div class="bg-white shadow px-4 py-5 sm:rounded-lg sm:p-6">
            <div class="md:grid md:grid-cols-3 md:gap-6">
                <div class="md:col-span-1">
                    <h3 class="text-lg font-medium leading-6 text-gray-900">Informations optionnelles</h3>
                    <p class="mt-1 text-sm text-gray-500">Autres informations de profil optionnelles</p>
                </div>
                <div class="mt-5 space-y-6 md:mt-0 md:col-span-2">
                    <div class="grid grid-cols-6 gap-6">

                        {{range .ViewState.profile_optional_fields}}
                        <div class="col-span-6 sm:col-span-3">
                            <label for="{{.name}}" class="block text-sm font-medium text-gray-700">{{.label}}</label>
                            {{if eq .type "select"}}
                            <div class="sm:mt-0 sm:col-span-2">
                                <select id="{{.name}}" name="{{.name}}"
                                    {{if eq .name "gender"}}x-model="gender"{{else}}x-model="other_properties.{{.name}}"{{end}}
                                    class="p-2 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 border-gray-300 rounded-2xl">
                                    {{range .options}}
                                    <option value="{{.value}}">{{.label}}</option>
                                    {{end}}
                                </select>
                            </div>
                            {{else if eq .type "textarea"}}
                            <textarea id="{{.name}}" name="{{.name}}" rows="3" x-model="other_properties.{{.name}}"
                                class="mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm border-gray-300 rounded-2xl"></textarea>
                            {{else if eq .type "multicheckbox"}}
                            <div class="mt-1 space-y-2">
				{{$name := .name}}
                                {{range .options}}
                                <label class="flex items-center">
                                    <input type="checkbox" name="{{$name}}[]" value="{{.value}}"
				    x-model="other_properties.{{$name}}___{{.value}}"
                                           class="h-4 w-4 text-co-blue focus:ring-co-blue border-co-blue rounded">
                                    <span class="ml-2 text-sm text-gray-700">{{.label}}</span>
                                </label>
                                {{end}}
                            </div>
                            {{else if eq .type "date"}}
                            <input type="date" id="{{.name}}" name="{{.name}}" x-model="other_properties.{{.name}}"
                                class="mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm border-gray-300 rounded-2xl">
                            {{else if eq .type "number"}}
                            <input type="number" id="{{.name}}" name="{{.name}}" x-model="other_properties.{{.name}}"
                                class="mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm border-gray-300 rounded-2xl">
                            {{else}}
                            <input type="text" id="{{.name}}" name="{{.name}}" x-model="other_properties.{{.name}}"
                                class="mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm border-gray-300 rounded-2xl">
                            {{end}}
                        </div>
                        {{end}}

                        <!-- <div class="col-span-3 sm:col-span-3">
                            <label class="block text-sm font-medium text-gray-700"> Photo </label>
                            <div class="mt-1 flex items-center space-x-5">
                                <span class="inline-block h-12 w-12 rounded-co overflow-hidden bg-gray-100">
                                    {{.IconSet.Icon "img:profile-picture-placeholder" "h-full w-full"}}
                                </span>
                                <button type="button"
                                    class="bg-white py-2 px-3 border border-gray-300 rounded-2xl shadow-sm text-sm leading-4 font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-co-blue">
                                    Charger la photo
                                </button>
                            </div>
                        </div> -->


                    </div>
                </div>
            </div>
        </div>
        <!--<div class="bg-white shadow px-4 py-5 sm:rounded-lg sm:p-6">
            <div class="md:grid md:grid-cols-3 md:gap-6">
                <div class="md:col-span-1">
                    <h3 class="text-lg font-medium leading-6 text-gray-900">Paramètres</h3>
                    <p class="mt-1 text-sm text-gray-500">Paramètres liés au conducteur, utiles pour exploiter les fonctionnalités de PARCOURSMOB</p>
                </div>
                <div class="mt-5 space-y-6 md:mt-0 md:col-span-2">

                    {{ $fieldName := "address" }}
                    {{ template "address_autocomplete" dict "FieldName" $fieldName }}

                </div>
            </div>
        </div>-->

        <div class="flex justify-end">
            <p x-show="! isFormValid" class="px-4 py-2 text-sm text-co-red">Certains champs de sont pas valides.</p>
<a href="/app/solidarity-transport/drivers/{{.ViewState.driver.ID}}">
                <button type="button"
                    class="bg-white py-2 px-4 border border-gray-300 rounded-2xl shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-co-blue">Annuler</button>
            </a>
            <button type="submit"
                class="ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-2xl text-white bg-co-blue hover:bg-co-blue focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-co-blue">Modifier le conducteur solidaire</button>
        </div>
    </form>
</div>
{{end}}

