{{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">Ajouter un véhicule</h1>
</div>

<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8 mt-8" x-data="{
        fields: {
            licence_plate: null,
            name: null,
            type: null,
            informations: '',
        },
        rules: {
            licence_plate: ['required'], // 'regexMatch:^[A-Z]{1,2}-[0-9]{1,3}-[A-Z]{1,2}$'
            name: ['required'],
            type: ['required'],
            informations: ['optional']
        },
        formValidation: {
            valid: false,
            fields: {
                name: {valid: null},
                licence_plate: {valid: null},
                type: {valid: null},
                informations: {valid: null},
            }
        },
        isFormValid: true,
        init() {
            let quill = new Quill(this.$refs.quill, { theme: 'snow', modules: { toolbar: [[{ 'header': [1, 2, 3, 4, 5, 6, false] }], ['bold', 'italic', 'underline'], ['link', 'image']] } })
 
            quill.root.innerHTML = this.fields.informations
 
            quill.on('text-change', () => {
                this.fields.informations = quill.root.innerHTML
            })
        },
        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.validate()
            if(!this.formValidation.valid) {
                this.isFormValid = false
                event.preventDefault()
            }
            return this.formValidation.valid
        },

        displayAutomatic(type) {
            return type == 'Voiture'
        }
    }">
    <form class="space-y-6" method="POST" @submit="submit">
        <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">Identité du véhicule</h3>
                    <p class="mt-1 text-sm text-gray-500">Informations de base sur le véhicule</p>
                </div>
                <div class="mt-5 md:mt-0 md:col-span-2">
                    <div class="grid grid-cols-3 md:grid-cols-6 gap-6">
                        <div class="col-span-5">
                            <label for="name" class="block text-sm font-medium text-gray-700">Modèle (ou nom donné au véhicule)</label>
                            <input type="text" name="name" id="name"
                                class="mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm  rounded-2xl"
                                x-model="fields.name" @blur="validateField('name')"
                                :class="formValidation.fields.name.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                        </div>
                        <div class="col-span-3">
                            <label for="type" class="block text-sm font-medium text-gray-700">Type de véhicule</label>
                            <select id="type" name="type" 
                                x-model="fields.type" @blur="validateField('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"
                                :class="formValidation.fields.type.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                                <option value="" selected></option>
                                {{range .ViewState.vehicle_types}}
                                <option value="{{.}}">{{.}}</option>
                                {{end}}
                            </select>
                            <div class="m-4" x-show="displayAutomatic(fields.type)">
                                <legend class="sr-only">Automatique</legend>
                                <div class="relative flex items-start">
                                    <div class="flex h-5 items-center">
                                    <input 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">Ce véhicule a une boite automatique.</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-span-3">
                            <label for="licence_plate"
                                class="block text-sm font-medium text-gray-700">Numéro (Immat, Bicycode, ...)</label>
                            <input type="text" name="licence_plate" id="licence_plate" placeholder="XX-123-YY"
                                class="mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm  rounded-2xl"
                                x-model="fields.licence_plate"
                                @blur="fields.licence_plate = fields.licence_plate.toUpperCase(); validateField('licence_plate')"
                                :class="formValidation.fields.licence_plate.valid == false ? 'border-co-red border-2' : 'border-gray-300'">
                        </div>
                        <!--<div class="col-span-3">
                            <label for="kilometers"
                                class="block text-sm font-medium text-gray-700">Kilométrage</label>
                            <input type="text" name="kilometers" id="kilometers" placeholder="1 km"
                                class="mt-1 focus:ring-co-blue focus:border-co-blue block w-full shadow-sm sm:text-sm  rounded-2xl"
                                x-model="fields.kilometers">
                        </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">Informations pratiques</h3>
                    <p class="mt-1 text-sm text-gray-500">Informations pratiques pour la réservation</p>
                </div>
                <div class="mt-5 md:mt-0 md:col-span-2">
                    {{ $fieldName := "address" }}
                    {{ template "address_autocomplete" dict "FieldName" $fieldName }}

                    <div class="mt-5">
                        <label for="informations" class="block text-sm font-medium text-gray-700">Informations pratiques</label>
                        <div class="mt-1">
                            <div x-ref="quill" class="shadow-sm focus:ring-co-blue focus:border-co-blue block w-full sm:text-sm border-gray-300 rounded-b-2xl"></div>
                            <input type="hidden" name="informations" x-model="fields.informations" />
                        </div>
                    </div>
                </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/vehicles-management/">
                <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">Ajouter
                le véhicule</button>
        </div>
    </form>
</div>
{{end}}
