@extends('layouts.admin.theme')

@section('title', env('APP_NAME') . ' :: Buscar Currículos')

@section('content')
    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h5 class="my-2">Lista de Currículos</h5>
        </div>

        <div class="card-body">
            @include('errors.messageFlash')

            <div class="modal fade" id="modal-relatorio" tabindex="-1" aria-hidden="true" data-bs-backdrop="false">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content border-0">
                        <div class="modal-header bg-primary text-white">
                            <h5 class="modal-title">Selecione os Status</h5>
                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"
                                aria-label="Fechar"></button>
                        </div>

                        <form action="{{ route('admin.curriculos.gerarRelatorio') }}" method="GET" target="_blank">
                            <div class="modal-body"
                                style="max-height: 300px; overflow-y: auto; padding-top: 1rem; padding-bottom: 1rem;">
                                @foreach ($status as $item)
                                    <div class="form-check mb-2">
                                        <input class="form-check-input" type="checkbox" value="{{ $item->value }}"
                                            id="status-{{ $item->value }}" name="statuses[]">
                                        <label class="form-check-label" for="status-{{ $item->value }}">
                                            {{ $item->name }}
                                        </label>
                                    </div>
                                @endforeach
                            </div>

                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary me-2"
                                    data-bs-dismiss="modal">Fechar</button>
                                <button type="submit" class="btn btn-primary">Gerar Relatório</button>
                            </div>
                        </form>

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

            <form method="GET" action="{{ route('admin.curriculos.index') }}" class="mb-5 mt-4">
                <div class="row">
                    <div class="col-12 mb-3">
                        <label for="busca" class="form-label">{{ __('Buscar por nome, e-mail ou CPF') }}:</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="fa-solid fa-magnifying-glass"></i></span>
                            <input type="text" name="busca" id="busca" class="form-control"
                                value="{{ request('busca') }}" placeholder="{{ __('Digite o nome, e-mail ou CPF') }}">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-4">
                        <label for="occupation_id" class="form-label">Área de Interesse:</label>
                        <select name="occupation_id" id="occupation_id" class="form-select">
                            <option value="">-- Todas --</option>
                            @foreach ($occupations as $occupation)
                                <option value="{{ $occupation->id }}"
                                    {{ request('occupation_id') == $occupation->id ? 'selected' : '' }}>
                                    {{ $occupation->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-4">
                        <label for="neighbourhood_id" class="form-label">Bairro:</label>
                        <select name="neighbourhood_id" id="neighbourhood_id" class="form-select">
                            <option value="">-- Todas --</option>
                            @foreach ($neighbourhoods as $neighbourhood)
                                <option value="{{ $neighbourhood->id }}"
                                    {{ request('neighbourhood_id') == $neighbourhood->id ? 'selected' : '' }}>
                                    {{ $neighbourhood->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-4">
                        <label for="gender_id" class="form-label">Sexo:</label>
                        <select name="gender_id" id="gender_id" class="form-select">
                            <option value="">-- Todos --</option>
                            @foreach ($genders as $gender)
                                <option value="{{ $gender->value }}"
                                    {{ request('gender_id') == $gender->value ? 'selected' : '' }}>
                                    {{ $gender->label() }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="row mt-4">
                    <div class="col-md-3">
                        <label for="data_cadastro_inicio" class="form-label">Cadastro: de</label>
                        <input type="date" class="form-control" id="data_cadastro_inicio" name="data_cadastro_inicio"
                            value="{{ request('data_cadastro_inicio') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="data_cadastro_fim" class="form-label">Cadastro: até</label>
                        <input type="date" class="form-control" id="data_cadastro_fim" name="data_cadastro_fim"
                            value="{{ request('data_cadastro_fim') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="data_atualizacao_inicio" class="form-label">Atualização: de</label>
                        <input type="date" class="form-control" id="data_atualizacao_inicio"
                            name="data_atualizacao_inicio" value="{{ request('data_atualizacao_inicio') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="data_atualizacao_fim" class="form-label">Atualização: até</label>
                        <input type="date" class="form-control" id="data_atualizacao_fim" name="data_atualizacao_fim"
                            value="{{ request('data_atualizacao_fim') }}">
                    </div>
                </div>

                <div class="row mt-4">
                    <div class="col-md-6">
                        <div class="d-flex justify-content-start gap-2 btn-curriculo">
                            <a href="#" class="mobile-btn-atualizar btn btn-danger" data-bs-toggle="modal"
                                data-bs-target="#modal-relatorio">
                                <i class="fa-solid fa-file"></i> Relatório
                            </a>

                            <a href="{{ Route('admin.curriculos.listaCadastros') }}" class="mobile-btn-atualizar btn btn-primary">
                                <i class="fa-solid fa-file"></i> Editar Cadastro
                            </a>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-end gap-2 btn-curriculo mt-4">
                            <button type="submit" class="mobile-btn-atualizar btn btn-primary">
                                <i class="fa fa-search me-1"></i> Buscar
                            </button>
                            <a href="{{ route('admin.curriculos.index') }}"
                                class="mobile-btn-atualizar btn btn-secondary">
                                <i class="fa fa-times me-1"></i> Limpar
                            </a>
                        </div>
                    </div>
                </div>
            </form>

            <div class="table-responsive mt-5">
                <table class="table table-bordered table-hover table-striped align-middle text-center">
                    @include('components.tables._thead', [
                    'thead' => ['Nome', 'Sexo', 'Bairro', 'E-mail', 'Área de Interesse', 'Status', 'Data de Cadastro', 'Ultima Atualização', 'Ações']
                    ])
                    
                    <tbody>
                        @forelse($curriculos as $curriculo)
                            <tr>
                                <td>{{ $curriculo->name ?? 'Não informado' }}</td>
                                <td>{{ $curriculo->gender_id->name ?? 'Não informado' }}</td>
                                <td>{{ $curriculo->neighbourhood->name ?? 'Não informado' }}</td>
                                <td>{{ $curriculo->email ?? 'Não informado' }}</td>
                                <td>{{ optional($curriculo->occupation)->name ?? 'Não informado' }}</td>
                                <td>{{ $curriculo->ultimo_status ?? 'Sem status' }}</td>
                                <td>{{ date('d/m/Y', strtotime($curriculo['created_at'])) }}</td>
                                <td>{{ date('d/m/Y', strtotime($curriculo['updated_at'])) }}</td>
                                <td>
                                    <div class="d-flex gap-2">
                                        @php
                                            $searchParams = request()->except(['page']);
                                            $returnUrl = route('admin.curriculos.index', $searchParams);
                                        @endphp
                                        <a href="{{ route('admin.curriculos.gerarCurriculo', ['id' => $curriculo->id]) }}?return_url={{ urlencode($returnUrl) }}"
                                            class="btn btn-sm btn-outline-primary d-flex align-items-center justify-content-center"
                                            title="Gerar Currículo" target="_blank" style="width: 36px; height: 36px;">
                                            <i class="fa-solid fa-file-lines"></i>
                                        </a>

                                        <a href="{{ route('admin.curriculos.acompanhamentoCurriculo', ['id' => $curriculo->id]) }}?{{ http_build_query(array_merge($searchParams, ['return_url' => $returnUrl])) }}"
                                            class="btn btn-sm btn-outline-primary d-flex align-items-center justify-content-center"
                                            title="Acompanhamento do Currículo" target="_blank"
                                            style="width: 36px; height: 36px;">
                                            <i class="fa-solid fa-clock"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="9">* Nenhum currículo encontrado!</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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

@endsection
