<form>
    <input type="hidden" wire:model="quote_id">
    <input type="hidden" wire:model="user_id">

    <div class="row match-height mt-5">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Datos de la cotización</h4>
                </div>
                <div class="card-content">
                    <div class="card-body">
                        <div class="row">
                            @include('livewire.quotes._buttons')

                            <div class="col-lg-8">
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group form-autocomplete">
                                            <label for="user_document">Cédula</label>
                                            <input type="text" class="form-control document" wire:model="user_document"
                                                wire:keyup="autoCompleteUD" placeholder="Cédula"
                                                {{ $mode == 'edit' ? 'disabled' : '' }}>
                                            @error('user_document') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                            @if ($bool_autocomplete)
                                                <div class="wrapper-autocomplete">
                                                    <div class="list-group">
                                                        <button type="button" wire:click="selDocument(0)"
                                                            class="btn-link list-group-item list-group-item-action">
                                                            <div class="d-flex w-100 justify-content-between">
                                                                <h5 class="mb-1">
                                                                    <i class="fas fa-plus-circle"></i> Agregar nuevo
                                                                    usuario
                                                                </h5>
                                                            </div>
                                                        </button>
                                                        @foreach ($user_autocomplete as $item)
                                                            <button type="button"
                                                                wire:click="selDocument({{ $item->id }})"
                                                                class="btn-link list-group-item list-group-item-action">
                                                                <div class="d-flex w-100 justify-content-between">
                                                                    <h5 class="mb-1">{{ $item->document }}</h5>
                                                                </div>
                                                                <p class="mb-1">{{ $item->fullname }}</p>
                                                                <small>{{ $item->email }}</small>
                                                            </button>
                                                        @endforeach
                                                    </div>
                                                </div>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="user_name">Nombre</label>
                                            <input type="text" class="form-control" wire:model="user_name"
                                                placeholder="Nombre" {{ $user_new ? '' : 'disabled' }}>
                                            @error('user_name') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="user_lastname">Apellido</label>
                                            <input type="text" class="form-control" wire:model="user_lastname"
                                                placeholder="Apellido" {{ $user_new ? '' : 'disabled' }}>
                                            @error('user_lastname') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="user_email">Email</label>
                                            <input type="email" class="form-control" wire:model="user_email"
                                                placeholder="Email" {{ $user_new ? '' : 'disabled' }}>
                                            @error('user_email') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="user_email_optional">Emails opcionales</label>
                                            <input type="email" class="form-control" wire:model="user_email_optional"
                                                placeholder="Emails opcionales" {{ $user_new ? '' : 'disabled' }}>
                                                @foreach ($arrEmails as $key => $item)
                                                    @error('arrEmails.'.$key) <span
                                                    class="text-danger">{{ $message }}</span>@enderror
                                                @endforeach
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="user_phone">Teléfono</label>
                                            <input type="tel" class="form-control" wire:model="user_phone"
                                                placeholder="Teléfono" {{ $user_new ? '' : 'disabled' }}>
                                            @error('user_phone') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <div class="form-group" wire:ignore>
                                            <label for="user_main">Usuario principal</label>
                                            <select class="form-control user_main" wire:model="user_main" {{ $user_new ? '' : 'disabled' }}>
                                                <option value="0" id={{ rand() }}>Seleccione el usuario principal
                                                </option>
                                                @foreach ($users_list as $user)
                                                    <option value="{{ $user->id }}">{{ $user->document }} - {{ $user->fullname }}
                                                    </option>
                                                @endforeach
                                            </select>
                                            @error('user_main') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    @if ($mode == 'create')
                                        <div class="col-12">
                                            <div class="form-text">Para clientes nuevos, la contraseña será generada por
                                                el sistema y enviada por email.</div>
                                        </div>
                                    @endif
                                    <div class="col-12 mt-4 mb-2">
                                        <div class="form-group">
                                            <label>Tipo de Póliza:</label>
                                            <div class="form-check form-check-inline">
                                                <input class="form-check-input" type="radio" wire:model="type"
                                                    name="type" id="type_sv" value="SV"
                                                    {{ $type == 'RG' && Auth::user()->type != 'admin' && Auth::user()->category != 'ALL' ? 'disabled' : '' }}>
                                                <label class="form-check-label" for="type_sv">Salud y vida</label>
                                            </div>
                                            <div class="form-check form-check-inline">
                                                <input class="form-check-input" type="radio" wire:model="type"
                                                    name="type" id="type_rg" value="RG"
                                                    {{ $type == 'SV' && Auth::user()->type != 'admin' && Auth::user()->category != 'ALL' ? 'disabled' : '' }}>
                                                <label class="form-check-label" for="type_rg">Riesgos generales</label>
                                            </div>
                                        </div>
                                    </div>
                                    @if (Auth::user()->type == 'admin')
                                        <div class="col-md-12">
                                            @if ($mode != 'create')
                                                @if (!empty($quote->admin->id))
                                                    @if (!$quote->admin->status)
                                                        <div class="alert alert-warning" role="alert">
                                                            El agente <strong>{{ $quote->admin->fullname }}</strong>
                                                            ha sido inactivado, debe seleccionar un nuevo agente.
                                                        </div>
                                                    @endif
                                                @endif
                                            @endif
                                            <div class="form-group">
                                                <label for="admin_id">Agente</label>
                                                <select class="form-control" wire:model="admin_id">
                                                    <option value="" id={{ rand() }}>Seleccione el agente
                                                    </option>
                                                    @foreach ($agents_list as $agent)
                                                        <option value="{{ $agent->id }}">{{ $agent->fullname }}
                                                        </option>
                                                    @endforeach
                                                </select>
                                                @error('admin_id') <span
                                                    class="text-danger">{{ $message }}</span>@enderror
                                            </div>
                                        </div>
                                    @endif
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="product_detail_id">Producto</label>
                                            <select class="form-control" wire:model="product_detail_id">
                                                <option value="">Seleccione el producto</option>
                                                @foreach ($products_list as $item)
                                                    <option value="{{ $item->id }}">
                                                        @if (count($item->product->details) > 1)
                                                            {{ $item->product->name }} -
                                                        @endif
                                                        {{ $item->name }}
                                                    </option>
                                                @endforeach
                                            </select>
                                            @error('product_detail_id') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <div class="form-group" wire:ignore>
                                            <label for="description">Descripción de la cotización</label>
                                            <textarea wire:model.defer="description" id="description"
                                                class="form-control"></textarea>
                                            @error('description') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-lg-4">
                                <div class="card card-quote-details border">
                                    <div class="card-header">
                                        <h4>Listado de Opciones</h4>
                                    </div>
                                    <div class="card-body">
                                        @if (!count($details))
                                            <div class="wrapper-errors">
                                                @error('detail_insurer_id.0') <span class="text-danger error">Debe
                                                    agregar por lo menos 1 opción</span>@enderror
                                            </div>
                                        @endif
                                        @foreach ($details as $key => $value)
                                            <div class="quote-details-item border-bottom mb-4">
                                                <div class="row">
                                                    <div class="col-12">
                                                        <b>Opcion <span
                                                                class="badge badge-dark">#{{ $key + 1 }}</span></b>
                                                        <input type="hidden"
                                                            wire:model="detail_id.{{ $key }}">
                                                    </div>
                                                    <div class="col-12">
                                                        <div class="form-group">
                                                            <label for="">Seleccione la aseguradora</label>
                                                            <select class="form-control"
                                                                wire:model="detail_insurer_id.{{ $key }}">
                                                                <option value="">Seleccione...</option>
                                                                @foreach ($insurers_list as $insurer)
                                                                    <option value="{{ $insurer->id }}">
                                                                        {{ $insurer->name }}</option>
                                                                @endforeach
                                                            </select>
                                                            @error('detail_insurer_id.' . $key) <span
                                                                class="text-danger error">{{ $message }}</span>@enderror
                                                        </div>
                                                    </div>
                                                    <div class="col-12">
                                                        <div class="form-group">
                                                            <label for="">Monto de la cotización</label>
                                                            <input type="text" class="form-control money"
                                                                wire:model="detail_amount.{{ $key }}">
                                                            @error('detail_amount.' . $key) <span
                                                                class="text-danger error">{{ $message }}</span>@enderror
                                                        </div>
                                                    </div>
                                                    {{-- <div class="col-xl-6 col-lg-12">
                                                        <div class="form-group" wire:ignore>
                                                            <label for="">Fecha máxima de vigencia</label>
                                                            <input type="text" data-index="{{ $key }}" class="form-control detail_date_max datepicker">
                                                            @error('detail_date_max.' . $key) <span class="text-danger error">{{ $message }}</span>@enderror
                                                        </div>
                                                    </div> --}}
                                                    <div class="col-12">
                                                        <div class="form-group">
                                                            <label for="" class="form-label">
                                                                Archivo adjunto
                                                            </label>
                                                            @if (!empty($detail_file[$key]) && $mode != 'create')
                                                                @if (mb_strtolower(substr($detail_file[$key], -3) == 'pdf'))
                                                                    <a href="{{ Storage::url($detail_file[$key]) }}"
                                                                        download
                                                                        class="btn btn-link btn-link2 text-dark">
                                                                        <i class="fas fa-download"></i>
                                                                        Descargar Archivo
                                                                    </a>
                                                                @endif
                                                            @endif
                                                            <input class="form-control file" type="file"
                                                                wire:model="detail_file.{{ $key }}"
                                                                accept="application/pdf" wire:click='changeFile($event.target.value, {{ $key }})'
                                                                name="detail_file"
                                                                >
                                                                @error('detail_file_validate.' . $key) <span
                                                                class="text-danger error">{{ $message }}</span>@enderror
                                                        </div>
                                                    </div>
                                                    <div class="col-12 text-right">
                                                        <button type="button"
                                                            class="btn btn-danger btn-link btn-link2 text-danger"
                                                            wire:click="removeItem({{ $key }})">
                                                            <i class="far fa-trash-alt"></i>
                                                            Quitar
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        @endforeach
                                    </div>
                                    <div class="card-footer text-center">
                                        <button wire:click.prevent="addItem()" class="btn">
                                            <i class="fas fa-plus"></i>
                                            Agregar opción
                                        </button>
                                    </div>
                                </div>
                            </div>

                            {{-- @include('livewire.quotes._buttons') --}}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>



<script>
    document.addEventListener('livewire:load', function() {
        $('.document').mask('ZZZZZZZZZZZZZZZZZZZZ', {
            translation: {
                'Z': {
                    pattern: /[0-9A-Za-z]/,
                    optional: true
                }
            }
        });

        //User main select2
        $(".user_main").select2();
        $('.user_main').on('change', function (e) {
            let data = $(this).val();
            @this.set('user_main', data);
        });
    });
</script>

<script>

    //User main select2
    window.addEventListener('activeSelect2', event => {
        if(typeof event.detail.user_main == 'number') {
            $(".user_main").select2().val(String(event.detail.user_main)).trigger("change");;
        } else if(event.detail.user_main == 'disabled') {
            $(".user_main").prop("disabled", false);
        } else {
            $(".user_main").select2().prop("disabled", true);
            $('.user_main').on('change', function (e) {
                let data = $(this).val();
                @this.set('user_main', data);
            });
        }

    })
</script>


