<div>
    @include('parts.overlay', ['target' => 'store'])

    <form wire:submit.prevent="{{ $form_client_vehicle['action'] }}" autocomplete="off" >
        <div class="row match-height mt-5">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title">{{  $form_client_vehicle['title']  }}</h4>
                    </div>
                    <div class="card-content">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-12">
                                    <div style="max-width: 500px; margin: auto;">
                                        <div class="form-group">
                                            <div class="d-flex justify-content-between align-items-center">
                                                <label for="client_id">Cliente</label>
                                                <a  wire:click="$emit('showVqClientFormModal')" class="text-primary" style="cursor: pointer;">crear nuevo</a>
                                            </div>
                                            <select wire:model="data_client_vehicle.client_id" class="form-control kt-select2 @error('data_client_vehicle.client_id') is-invalid @enderror"
                                                id="client_id">
                                                <option value="">::. Seleccione .::</option>
                                                @foreach ($data_client_vehicle['clients_items'] as $key => $client)
                                                    <option value="{{ $client['id'] }}" {{ $data_client_vehicle['client_id']==$client['id'] ? 'selected' : ''}}>{{ $client['name'] }}</option>
                                                @endforeach
                                            </select>
                                            @error('data_client_vehicle.client_id') <span class="text-danger">{{ $message }}</span> @enderror
                                        </div>
                                    </div>
                                </div>

                                <div class="col-12 mt-3">
                                    <div style="max-width: 500px; margin: auto;">
                                        <div class="form-group">
                                            <div class="d-flex justify-content-between align-items-center">
                                                <label for="vq_car_brand_id">Vehículo</label>
                                                <a  wire:click="$emit('showVqVehicleFormModal')" class="text-primary" style="cursor: pointer;">crear nuevo</a>
                                            </div>
                                            <select wire:model="data_client_vehicle.car_id" class="form-control kt-select2 @error('data_client_vehicle.car_id') is-invalid @enderror"
                                                id="car_id">
                                                <option value=""></option>
                                                @foreach ($data_client_vehicle['brands_items'] as $key_brand => $brand)
                                                    @if (count($brand['cars']) > 0)
                                                        <optgroup label="{{ $brand['name'] }}">
                                                            @foreach ($brand['cars'] as $key_car => $car)
                                                                <option value="{{ $car['id'] }}" {{ $data_client_vehicle['car_id']==$car['id'] ? 'selected' : ''}}>{{ $brand['name'] }} {{ $car['model'] }} - {{ $car['year'] }} - {{ $car['type_use'] }} - {{ $car['pax'] }} Pasajeros</option>
                                                            @endforeach
                                                        </optgroup>
                                                    @endif
                                                @endforeach
                                            </select>
                                            @error('data_client_vehicle.car_id') <span class="text-danger">{{ $message }}</span> @enderror
                                        </div>
                                    </div>
                                </div>

                                <div class="col-12 mt-2">
                                    <div style="max-width: 500px; margin: auto;">
                                    <div class="form-group">
                                        <label for="price">Valor</label>
                                        <input type="text" class="form-control price" wire:model="data_client_vehicle.price" placeholder="0.00">
                                        @error('data_client_vehicle.price') <span class="text-danger">{{ $message }}</span>@enderror
                                    </div>
                                </div>

                                <div class="col-12 d-flex justify-content-end">
                                    <button id="btn-submit" type="submit" class="btn btn-primary mr-1 mb-1" wire:loading.attr="disabled"
                                        wire:target="{{ $form_client_vehicle['action'] }}">
                                        {{ $form_client_vehicle['label'] }}
                                        <i class="fas fa-arrow-circle-right"></i>
                                    </button>
                                    {{-- <a href="{{ route('vehicle-quote.manage-vehicle.index') }}" class="btn btn-light-secondary mr-1 mb-1">
                                        <i class="fas fa-ban"></i>
                                        Cancelar
                                    </a> --}}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <!--begin: Modal -->
    <div wire:ignore>
        @livewire('components.vq-client-form-modal')
    </div>

    <div wire:ignore>
        @livewire('components.vq-vehicle-form-modal')
    </div>
    <!--end: Modal-->
</div>

{{-- Script para user_main --}}
@push('styles')
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.2.0/css/datepicker.min.css" rel="stylesheet">
    <style>
        .select2-container .select2-selection--single {height: 37px !important;}
        .select2-container--default .select2-selection--single .select2-selection__arrow { height: 37px; }
        .select2-container--default .select2-selection--single .select2-selection__rendered { line-height: 37px; }
        .select2-container--default .select2-selection--single .select2-selection__clear { line-height: 35px; }
    </style>
@endpush
@section('js')
    @include('parts.js-preview')
@endsection
@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.2.0/js/bootstrap-datepicker.min.js"></script>
<script type="text/javascript">
        document.addEventListener('livewire:load', function () {
            $("#year").datepicker({
                format: "yyyy",
                viewMode: "years",
                minViewMode: "years",
                autoclose:true
            });

            // format price input mask (money)
            $('.price').inputmask('decimal', {
                radixPoint: ".",
                groupSeparator: ",",
                digits: 2,
                autoGroup: true,
                prefix: '$ ', //Space after $, this will not truncate the first character.
                rightAlign: false,
                oncleared: function () { self.Value(''); },
                removeMaskOnSubmit: true
                //get value
                //var value = $('#price').inputmask('unmaskedvalue');
            });

            function init_vq_select_customer_vehicle() {

                 // Not STYLES
                $('.kt-select2').select2({
                    placeholder: "::. Seleccione .::",
                    allowClear: true
                });

                // Client
                $('#client_id').off('change');
                $('#client_id').on('change', function (e) {
                    $("#client_id").select2({
                        placeholder:  "::. Seleccione .::",
                        allowClear: true
                    });
                    @this.set('data_client_vehicle.client_id', $(this).val());
                });

                // Car
                $('#car_id').off('change');
                $('#car_id').on('change', function (e) {
                    $("#car_id").select2({
                        placeholder: "::. Seleccione .::",
                        allowClear: true
                    });
                    @this.set('data_client_vehicle.car_id', $(this).val());
                });

                // Valor
                $('.price').off('change');
                $('.price').on('change', function (e) {
                    let value = $('.price').inputmask('unmaskedvalue');
                    @this.set('data_client_vehicle.price', value);
                });

                // FORM VEHICLE
                $('#vq_car_brand_id').off('change');
                $('#vq_car_brand_id').on('change', function (e) {
                    $("#vq_car_brand_id").select2({
                        placeholder: "Seleccione",
                        allowClear: true
                    });
                    @this.call('setCarBrand', $(this).val());
                });

                 // FORM VEHICLE MODEL ID
                 $('#vq_car_model_id').off('change');
                $('#vq_car_model_id').on('change', function (e) {
                    $("#vq_car_model_id").select2({
                        placeholder: "Seleccione",
                        allowClear: true
                    });
                    @this.call('setCarModel', $(this).val());
                });

                // FORM VEHICLE USE TYPE ID
                $('#vq_car_use_type_id').off('change');
                $('#vq_car_use_type_id').on('change', function (e) {
                    $("#vq_car_use_type_id").select2({
                        placeholder: "Seleccione",
                        allowClear: true
                    });
                    @this.call('setCarUseType', $(this).val());
                });

                // FORM VEHICLE YEAR
                $('#year').off('changeDate');
                $('#year').on('changeDate', function (e) {
                    @this.call('setYear', $(this).val());
                });
            }

            init_vq_select_customer_vehicle();

            Livewire.hook('message.processed', (message, component) => { init_vq_select_customer_vehicle(); });
        });
</script>
@endpush
