@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: Lista de Parques e Jardins')
@section('content')

    @include("errors.messageFlash")


    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center mb-3">
            <h5 class="mb-0">Lista de Validação de Documentos</h5>
            <div class="d-flex">
                <button class="btn btn-light btn-sm" onclick="window.location.href='{{ route('admin.validacaoDocumentos.create') }}'">
                    <i class="fa-solid fa-plus"></i> Novo documento
                </button>
            </div>
        </div>
        <div class="card-body">
            <form id="searchAccounts" class="d-flex mb-4" method="get">
                <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                <input type="text" class="form-control me-2" name="q" placeholder="Pesquisar documento..."
                    value="{{ request()->input('q') }}">
                <button class="btn btn-outline-success" type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
            </form>

            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped align-middle text-center">
                    @include('components.tables._thead', ['thead' => ['Arquivo', 'Pertence a', 'Código', 'Categoria', 'CPF', 'Resumo', 'Status', 'Ações']])
                    <tbody>
                        @php $index = 0 @endphp
                        @forelse($documentos as $documento)
                            <tr>
                                <td>
                                    @php
                                        $extensoesImagem = ['jpg', 'jpeg', 'png', 'gif'];
                                        $extensao = pathinfo($documento->documento, PATHINFO_EXTENSION);
                                    @endphp
                                    @if (in_array(strtolower($extensao), $extensoesImagem))
                                    <button type="button" style="border: 0; background" class="btn" data-bs-toggle="modal" data-bs-target="#open_{{ $index }}">
                                        <img src="{{ asset($documento->documento) }}" alt="Comprovante" class="img-fluid rounded-2" width="90px">
                                    </button>
                                    <div class="modal fade" id="open_{{ $index }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                        <div class="modal-dialog modal-fullscreen">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h1 class="modal-title fs-5" id="exampleModalLabel">Visualizar comprovante</h1>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <img src="{{ asset($documento->documento) }}" alt="Comprovante" class="img-fluid rounded-2">
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fechar</button>
                                                </div>
                                            </div>
                                        </div>
                                    @elseif (strtolower($extensao) === 'pdf')
                                        <a href="{{ asset($documento->documento) }}" class="text-primary" target="_blank" download><i class="fa-solid fa-download"></i> Baixar PDF</a>
                                    @else
                                        <span class="text-muted">Arquivo não suportado</span>
                                    @endif
                                </td>
                                <td>{{ $documento->pertence->firstName }} {{ $documento->pertence->lastName }}</td>
                                <td>{{ $documento->code }}</td>
                                <td>
                                    @foreach(App\Enums\StatusTipoDocumentoEnum::cases() as $status)
                                        {{ $documento->categoria == $status->name ? $status->value : '' }}
                                    @endforeach
                                </td>
                                <td>{{ $documento->pertence->cpf }}</td>
                                <td>{!! $documento->resumo !!}</td>
                                <td>
                                    <form id="{{ "checkStatus_" . $index }}" class="fix"
                                        action="{{ route("admin.validacaoDocumentos.status", $documento->id)}}" method="post">
                                        @csrf
                                        <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                        @if($documento->status == "ATIVADO")
                                            <div class="form-check form-switch d-flex justify-content-center">
                                                <input type="hidden" name="checkStatus" value="DESATIVADO">
                                                <input class="form-check-input" type="checkbox" checked
                                                    onclick="javascript:$('#{{ "checkStatus_" . $index }}').submit()">
                                            </div>
                                        @else
                                            <div class="form-check form-switch d-flex justify-content-center">
                                                <input type="hidden" name="checkStatus" value="ATIVADO">
                                                <input class="form-check-input" type="checkbox"
                                                    onclick="javascript:$('#{{ "checkStatus_" . $index }}').submit()">
                                            </div>
                                        @endif
                                    </form>
                                </td>
                                <td class="text-nowrap text-center align-middle">
                                    <div class="d-flex justify-content-center flex-wrap gap-1">
                                        <a href="{{ route('admin.validacaoDocumentos.edit', $documento->id) }}"
                                            class="btn btn-sm btn-warning" title="Editar"><i
                                                class="fa-solid fa-pen-to-square"></i>
                                        </a>

                                        @if (in_array('DELETE', explode(',', auth()->user()->papers)))
                                            <form class="d-inline" action="{{ route('admin.validacaoDocumentos.destroy', $documento->id) }}"
                                                method="post" onsubmit="return confirmDelete(this)">
                                                @csrf
                                                @method('delete')
                                                <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                                <input type="hidden" name="confirm"
                                                    value="{{ __('* Este documento será excluído e esta ação não poderá ser desfeita, deseja continuar?') }}">
                                                <button type="submit" class="btn btn-sm btn-danger" title="Excluir">
                                                    <i class="fa-solid fa-trash"></i>
                                                </button>
                                            </form>
                                        @endif
                                    </div>
                                </td>
                            </tr>
                            @php $index++ @endphp
                        @empty
                            <tr>
                                <td colspan="6" class="text-muted fst-italic">* Nenhum registro encontrado!</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-center mt-4">
                {!! $documentos->links() !!}
            </div>
        </div>
    </div>
@endsection