<div>
    @component('common.breadcrumb', ['addBtn' => false, 'permission_create' => 'Employee_documents_index',
        'breadcrumb_show' => $breadcrumb_show])
        @slot('breadcrumb_title')
            <h3>{{ $componentName }} | {{ $pageTitle }}</h3>
        @endslot
        <li class="breadcrumb-item">Empleado</li>
        <li class="breadcrumb-item active">Documentos</li>
    @endcomponent
    <div class="container-fluid">

        <div class="row justify-content-between file-content mb-2">
            <div class="col-lg-4 col-md-4 col-sm-12">
                <div class="form-inline">
                    <div class="form-group d-flex mb-0"><i class="fa fa-search"></i>
                        <input class="form-control-plaintext" type="text" wire:model="search" placeholder="Buscar...">
                    </div>
                </div>
            </div>
            @if (isset($count_reg))
                <div class="col-lg-4 col-md-4 col-sm-12">
                    <p class="p-l-20">Total Documentos: {{ $count_reg }}</p>
                </div>
            @endif

        </div>
    </div><!-- container-fluid -->
    <div class="container-fluid">
        <div class="card">
            <div class="card-body">
                <div class="row layout-top-spacing">
                    <table class="table table-hover table-responsive-md table-fixed table-striped">
                    @if ($count_reg > 0)
                        <thead>
                            <tr>
                                <th class="w-20">Nombre</th>
                                <th class="w-20">Estado</th>
                                <th class="w-20">Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($documents_company as $item)
                            <tr>
                                <td>{{ $item->name }}</td>
                                <td>
                                    <span class="badge badge-pill p-1 {{ $this->CheckStatusDocument($item->id)[0] == 0 ? 'bg-warning' : ($this->CheckStatusDocument($item->id)[0] == 1 ? 'bg-success' : 'bg-danger') }}">{{ $this->CheckStatusDocument($item->id)[1] }}</span>

                                </td>
                                <td>
                                    @if ($this->CheckStatusDocument($item->id)[2])<!-- la opción 3 del array es para verificar si puede subir el documento -->
                                        <a href="{{ asset('storage/users/'. $user_id .'/documents/'.$this->ViewDocument($item->id)) }}" target="_blank"
                                            class="btn btn-sm btn-primary">
                                            <i class="fa fa-download"></i> Ver Documento
                                        </a>
                                        @if($this->CheckStatusDocument($item->id)[0] == 0)
                                            <button onclick="ConfirmDocumentAction({{ $item->id }}, '1')" class="btn btn-sm btn-success">
                                                <i class="fa fa-check"></i> Aprobar
                                            </button>
                                            <button onclick="ConfirmDocumentAction({{ $item->id }}, '2')" class="btn btn-sm btn-danger">
                                                <i class="fa fa-times"></i> Rechazar
                                            </button>
                                        @endif
                                    @endif
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    @else
                        <tbody>
                            <tr>
                            <td colspan="3" class="text-center">
                                <h5 class="text-danger">No hay Documentos registrados</h5>
                            </div>
                        </div>
                    @endif
                    </table>
                        {{ $documents_company->links() }}
                </div><!-- row -->
        </div><!-- card-body -->
        </div><!-- card -->
    </div><!-- container-fluid -->


    @include('livewire.loading.component') <!--- component loading ,en targetLoading se coloca el id del metodo que se esta cargando --->
</div>

@include('common.scriptsCompStandar')

<script>
document.addEventListener('DOMContentLoaded', function() {
    window.livewire.on('show-modal-reject', msg => {
        $('#theModalReject').modal('show')
    });
    window.livewire.on('show-modal-support-document', msg => {
        $('#theModalUpload').modal('show')
    });
    window.livewire.on('hide-modal-support-document', msg => {
        $('#theModalUpload').modal('hide')
    });
});

function ConfirmDocumentAction(id, option)
    {
        let title = '';
        let icon = '';
        let btn_color = '';
        let reason = '';


        if (option == 1) {
            title = 'APROBAR';
            icon = 'success';
            html = '<p class="m-b-0">Al aprobar el documento, se cambiará el estado a <span class="text-success">APROBADO</span>.</p>',
            btn_color = 'btn btn-success';
        } else if (option == 2) {
            title = 'RECHAZAR';
            icon = 'error';
            html = '<textarea class="form-control fs-5" id="reason" placeholder="Digite el motivo aquí..." rows="3"></textarea>',
            btn_color = 'btn btn-danger';
        }
        swal.fire({
            title: title,
            html: html,
            icon: icon,
            showCancelButton: true,
            cancelButtonText: 'CERRAR',
            customClass: {
                confirmButton: btn_color,
                cancelButton: 'btn btn-info'
        },

            confirmButtonText: title,
        }).then(function(result){
            if(result.value)
            {
                if (option == 2){
                    reason = document.getElementById('reason').value;
                }
                window.livewire.emit('UserDocumentAction',id, option, reason);
                swal.close()
            }
        });

    }
</script>
