
<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>
@livewire('claims.scripts')

<form>
    <input type="hidden" wire:model="claim_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 reclamación</h4>
                </div>
                <div class="card-content">
                    <div class="card-body">
                        <div class="row">
                            @include('livewire.claims._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="">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"
                                                {{ $mode == 'edit' ? 'disabled' : '' }}>
                                                <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-4">
                                        <div class="form-group">
                                            <label for="policy_number">Número de póliza</label>
                                            <input type="text" class="form-control" wire:model="policy_number"
                                                placeholder="Número de póliza" {{ $mode == 'edit' ? 'disabled' : '' }}>
                                            @error('policy_number') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="reclaimed_amount">Monto reclamado</label>
                                            <input type="text" class="form-control money" wire:model="reclaimed_amount"
                                                placeholder="Monto reclamado" required {{ $mode == 'edit' ? 'disabled' : '' }}>
                                            @error('reclaimed_amount') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="amount_approved">Monto aprobado</label>
                                            <input type="text" class="form-control money" wire:model="amount_approved"
                                                placeholder="Monto aprobado">
                                            @error('amount_approved') <span
                                                class="text-danger">{{ $message }}</span>@enderror
                                        </div>
                                    </div>
                                    <div class="col-md-12 mt-3">
                                        <div class="form-group">
                                            <label for="description">Actualización de la reclamación</label>
                                            <span class="float-end text-primary" id="view_list_generic"
                                                wire:click='showQuickly()'>Ver listado
                                                genérico</span>
                                            <div class="mb-4 mt-4 div_list_g w-92 {{ $showQuickly }}">
                                                <div class="d-flex w-100">
                                                    <select name="list_generic_selec" i
                                                        class="quickly_answers w-75 mb-3" wire:ignore.self
                                                        wire:model='quickly_id'>
                                                        <option value="" {{ $quickly_id == '' ? 'selected' : '' }}>
                                                        </option>
                                                        @foreach ($quickly_answers as $item)
                                                            <option value="{{ trim($item->id) }}"
                                                                {{ $quickly_id == $item->id ? 'selected' : '' }}>
                                                                @php
                                                                    echo trim($item->title);
                                                                @endphp
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                    <span class="ml-3 d-flex justify-content-center align-items-center">
                                                        <i class="fas fa-plus fa-lg" id="addListGe"></i>
                                                        <i class="fas fa-pencil-alt fa-lg mx-3" id="updateListGe"></i>
                                                        <i class="far fa-trash-alt fa-lg" id="deleteListGe"></i>
                                                    </span>
                                                </div>
                                            </div>

                                            {{-- Modal quickly a --}}
                                            <div wire:ignore.self>
                                                @include('parts.modal-list-generic')
                                            </div>
                                            <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>Histórico de Actualizaciones</h4>
                                    </div>
                                    <div class="card-body">
                                        <div class="border min-vh-100">
                                            @if ($this->mode == 'edit')
                                                @foreach ($claim_historys as $item)
                                                    <div class="m-1 p-2 border-bottom mb-3">
                                                        <span class="mb-2 d-block">{{ $item->date_formatted }}</span>
                                                        <div>
                                                            @php
                                                                echo $item->item;
                                                            @endphp
                                                        </div>
                                                    </div>
                                                @endforeach
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    {{-- @include('livewire.claims._buttons') --}}

                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
</form>

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

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

    });
</script>

