
@extends('layouts.admin.theme')

@section('title', env('APP_NAME') . ' :: Pessoas Trans')

@push('after-styles')
<style>
    .card-prefeitura {
        border: none;
        border-radius: 12px;
    }

    .section-header {
        border-left: 4px solid #0d6efd;
        padding-left: 12px;
    }

    .table thead {
        background-color: #f8f9fa;
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .table-hover tbody tr:hover {
        background-color: #f1f5ff;
    }
</style>
@endpush

@section('content')
<div class="card card-prefeitura shadow-sm">
    @php
        $headLine = "<i class='fa-solid fa-venus-mars'></i> Cadastros de Pessoas Trans";
    @endphp

    @include('components.buttons._headLineButtonsBackAdd', [
        'headLine' => $headLine,
        'routeBack' => 'admin.dashboard',
        'nameBack' => __('Voltar'),
        'routeAdd' => 'admin.saude.pessoas-trans.create',
        'nameAdd' => __('Novo cadastro'),
        'routeOther' => null,
        'nameOther' => null,
        'permission' => 'GERENCIADOR_DO_CADASTRO_DE_PESSOAS_TRANS',
    ])

    <div class="card-body pt-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div class="section-header">
                <h5 class="mb-0 fw-semibold text-primary">
                    <i class="fa-solid fa-venus-mars me-2"></i>
                    Pessoas Trans Cadastradas
                </h5>
            </div>

            <div class="d-flex align-items-center gap-2">
                <form class="d-flex" method="get">
                    <input type="text"
                           class="form-control form-control-sm"
                           name="q"
                           placeholder="Buscar por nome ou CPF..."
                           value="{{ $busca ?? '' }}">
                    <button class="btn btn-sm btn-primary ms-2" type="submit">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
                </form>

                <button class="btn btn-sm btn-outline-secondary ms-2"
                        data-bs-toggle="modal"
                        data-bs-target="#modalRelatorio">
                    <i class="fa-solid fa-file-pdf me-1"></i>
                    Relatório
                </button>
            </div>
        </div>

        @if(session('success'))
            <div class="alert alert-success">
                <i class="fa-solid fa-circle-check me-1"></i>
                {{ session('success') }}
            </div>
        @endif

        @if(session('error'))
            <div class="alert alert-danger">
                <i class="fa-solid fa-triangle-exclamation me-1"></i>
                {{ session('error') }}
            </div>
        @endif

        <div class="table-responsive">
            <table class="table table-hover table-bordered align-middle">
                <thead>
                    <tr>
                        <th class="text-center" style="width: 70px;">#</th>
                        <th>Nome social</th>
                        <th>Nome civil</th>
                        <th style="width: 150px;">CPF</th>
                        <th style="width: 160px;">Data de cadastro</th>
                        <th class="text-center" style="width: 150px;">Ações</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($registros as $registro)
                        <tr>
                            <td class="text-center">{{ $registro->id }}</td>
                            <td>{{ $registro->nome_social }}</td>
                            <td>{{ $registro->nome_civil }}</td>
                            <td>{{ $registro->cpf }}</td>
                            <td>{{ optional($registro->created_at)->format('d/m/Y') }}</td>
                            <td class="text-center">
                                <div class="btn-group">
                                    <a href="{{ route('admin.saude.pessoas-trans.edit', $registro) }}"
                                       class="btn btn-sm btn-warning"
                                       title="Editar">
                                        <i class="fa-solid fa-pen-to-square"></i>
                                    </a>
                                    <form action="{{ route('admin.saude.pessoas-trans.destroy', $registro) }}"
                                          method="POST"
                                          class="d-inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit"
                                                onclick="return confirm('Deseja realmente excluir este cadastro?');"
                                                class="btn btn-sm btn-danger"
                                                title="Excluir">
                                            <i class="fa-solid fa-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center text-muted fst-italic">
                                Nenhum cadastro encontrado.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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

{{-- Modal de Relatório --}}
<div class="modal fade" id="modalRelatorio" tabindex="-1" aria-labelledby="modalRelatorioLabel" data-bs-backdrop="false">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form action="{{ route('admin.saude.pessoas-trans.relatorio') }}" method="POST" target="_blank">
                @csrf

                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title" id="modalRelatorioLabel">
                        <i class="fa-solid fa-file-pdf me-1"></i>
                        Relatório personalizado de cadastros
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body">
                    <p class="mb-3">
                        Selecione os campos que deseja incluir no relatório. O filtro de busca atual
                        (nome/CPF) será considerado na geração do PDF.
                    </p>

                    <input type="hidden" name="q" value="{{ $busca ?? '' }}">

                    <div class="row">
                        @foreach($camposRelatorio as $campo => $label)
                            <div class="col-md-4 mb-2">
                                <div class="form-check">
                                    <input class="form-check-input"
                                           type="checkbox"
                                           name="campos_dados[]"
                                           id="campo_{{ $campo }}"
                                           value="{{ $campo }}"
                                           checked>
                                    <label class="form-check-label" for="campo_{{ $campo }}">
                                        {{ $label }}
                                    </label>
                                </div>
                            </div>
                        @endforeach
                    </div>
                </div>

                <div class="modal-footer d-flex justify-content-between"> 
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        Fechar
                    </button>

                    <button type="submit" class="btn btn-primary">
                        <i class="fa-solid fa-file-export me-2"></i>
                        Exportar PDF
                    </button>

                </div>
            </form>
        </div>
    </div>
</div>
@endsection
