@extends('layouts.admin.theme')

@section('title', env('APP_NAME') . ' :: Editar Cadastro')

@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 Cadastros</h5>

            <div class="ms-auto d-flex gap-2">
                @if (in_array('ADMIN', explode(',', auth()->user()->permission)))
                    <a href="{{ route('admin.curriculos.logs') }}" class="btn btn-danger btn-lg px-4">
                        <i class="bi bi-clipboard-data me-1"></i> Auditoria
                    </a>
                @endif
                <a href="{{ route('admin.curriculos.index') }}" class="btn btn-secondary btn-lg px-4">
                    <i class="bi bi-x-circle me-1"></i> Voltar
                </a>
            </div>
        </div>

        <div class="card-body">
            @include('errors.messageFlash')
            
            <div class="btn-group mb-3 flex-wrap bg-light d-flex justify-content-start p-3" role="group">
                @php
                    $currentParams = request()->except(['tipo', 'page']);
                @endphp
                <a href="{{ route('admin.curriculos.listaCadastros', array_merge(['tipo' => 'candidatos'], $currentParams)) }}"
                    class="btn btn-outline-primary filter-btn me-2 mb-2 {{ $tipo === 'candidatos' ? 'active' : '' }}">
                    <i class="fa-solid fa-users"></i> Candidatos
                </a>
                <a href="{{ route('admin.curriculos.listaCadastros', array_merge(['tipo' => 'empresas'], $currentParams)) }}"
                    class="btn btn-outline-primary filter-btn me-2 mb-2 {{ $tipo === 'empresas' ? 'active' : '' }}">
                    <i class="fa-solid fa-building"></i> Empresas
                </a>
            </div>

            <form method="GET" action="{{ route('admin.curriculos.listaCadastros') }}" class="mb-4">
                <div class="input-group">
                    <input type="hidden" name="tipo" value="{{ $tipo }}">
                    <input type="text" name="busca" value="{{ request('busca') }}" class="form-control"
                        placeholder="Buscar por nome, CPF/CNPJ ou email...">

                    <button class="btn btn-primary" type="submit">Buscar</button>

                    <a href="{{ route('admin.curriculos.listaCadastros', ['tipo' => $tipo]) }}"
                        class="btn btn-outline-secondary">
                        Limpar
                    </a>
                </div>
            </form>

            <div class="table-responsive mt-4">
                <table class="table text-center align-middle">
                    <thead>
                        <tr>
                            <th class="text-center">Nome</th>
                            <th class="text-center">
                                {{ $tipo === 'candidatos' ? 'CPF' : 'CNPJ' }}
                            </th>
                            <th class="text-center">Email</th>
                            <th class="text-center align-middle" style="width: 10%;">Ações</th>
                        </tr>
                    </thead>
                    <tbody>
                        @if ($tipo === 'candidatos' && $candidatos)
                            @foreach ($candidatos as $candidato)
                                <tr class="filter-row designacao-row">
                                    <td class="align-middle">{{ $candidato->name ?? 'Sem nome' }}</td>
                                    <td class="align-middle">{{ $candidato->cpf ?? 'Sem nome' }}</td>
                                    <td class="align-middle">{{ $candidato->email ?? 'Sem nome' }}</td>
                                    <td class="text-center align-middle d-flex justify-content-center">
                                        @php
                                            $searchParams = request()->except(['page']);
                                            $returnUrl = route('admin.curriculos.listaCadastros', $searchParams);
                                        @endphp
                                        <a href="{{ route('admin.curriculos.edit', ['tipo' => 'candidato', 'id' => $candidato->id]) }}?{{ http_build_query(array_merge($searchParams, ['return_url' => $returnUrl])) }}"
                                            class="btn btn-primary me-2">
                                            <i class="fa-solid fa-pen-to-square"></i> Editar
                                        </a>
                                        @if (in_array('ADMIN', explode(',', auth()->user()->permission)))
                                            <button type="button" class="btn btn-danger" data-bs-toggle="modal"
                                                data-bs-target="#auditoriaModal-{{ $candidato->id }}">
                                                <i class="fa-solid fa-shield-halved"></i>
                                            </button>

                                            <div class="modal fade" id="auditoriaModal-{{ $candidato->id }}" tabindex="-1"
                                                aria-hidden="true">
                                                <div class="modal-dialog modal-lg modal-dialog-scrollable">
                                                    <div class="modal-content">
                                                        <div class="modal-header bg-primary text-white">
                                                            <h5 class="modal-title">Auditoria - {{ $candidato->name }}</h5>
                                                            <button type="button" class="btn-close"
                                                                data-bs-dismiss="modal"></button>
                                                        </div>
                                                        <div class="modal-body">
                                                            @if ($candidato->logs->count())
                                                                <table class="table table-striped">
                                                                    <thead>
                                                                        <tr>
                                                                            <th>Chave Primária</th>
                                                                            <th>Matrícula</th>
                                                                            <th>Usuário</th>
                                                                            <th>Ação</th>
                                                                            <th>Detalhes</th>
                                                                            <th>Data</th>
                                                                        </tr>
                                                                    </thead>
                                                                    <tbody>
                                                                        @foreach ($candidato->logs as $log)
                                                                            <tr>
                                                                                <td>{{ $log->id }}
                                                                                </td>
                                                                                <td>{{ $log->user->matriculation }}
                                                                                </td>
                                                                                <td>{{ $log->user->firstName . ' ' . $log->user->lastName }}
                                                                                </td>
                                                                                <td>{{ $log->action }}</td>
                                                                                <td>{{ $log->details }}</td>
                                                                                <td>{{ $log->created_at->format('d/m/Y H:i:s') }}
                                                                                </td>
                                                                            </tr>
                                                                        @endforeach
                                                                    </tbody>
                                                                </table>
                                                            @else
                                                                <div class="text-center">Nenhum registro encontrado</div>
                                                            @endif
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary"
                                                                data-bs-dismiss="modal">Fechar</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        @endif
                                    </td>

                                </tr>
                            @endforeach
                        @else
                            @foreach ($empresas as $empresa)
                                <tr class="filter-row designacao-row">
                                    <td class="align-middle">{{ $empresa->corporate_name ?? 'Não informado' }}</td>
                                    <td class="align-middle">{{ $empresa->cnpj ?? 'Não informado' }}</td>
                                    <td class="align-middle">{{ $empresa->email ?? 'Não informado' }}</td>
                                    <td class="text-center align-middle d-flex justify-content-center">
                                        @php
                                            $searchParams = request()->except(['page']);
                                            $returnUrl = route('admin.curriculos.listaCadastros', $searchParams);
                                        @endphp
                                        <a href="{{ route('admin.curriculos.edit', ['tipo' => 'empresa', 'id' => $empresa->id]) }}?{{ http_build_query(array_merge($searchParams, ['return_url' => $returnUrl])) }}"
                                            class="btn btn-primary me-2">
                                            <i class="fa-solid fa-pen-to-square"></i> Editar
                                        </a>

                                        @if (in_array('ADMIN', explode(',', auth()->user()->permission)))
                                            <button type="button" class="btn btn-danger" data-bs-toggle="modal"
                                                data-bs-target="#auditoriaModal-{{ $empresa->id }}">
                                                <i class="fa-solid fa-shield-halved"></i>
                                            </button>

                                            <div class="modal fade" id="auditoriaModal-{{ $empresa->id }}" tabindex="-1"
                                                aria-hidden="true">
                                                <div class="modal-dialog modal-lg modal-dialog-scrollable">
                                                    <div class="modal-content">
                                                        <div class="modal-header bg-primary text-white">
                                                            <h5 class="modal-title">Auditoria - {{ $empresa->name }}</h5>
                                                            <button type="button" class="btn-close"
                                                                data-bs-dismiss="modal"></button>
                                                        </div>
                                                        <div class="modal-body">
                                                            @if ($empresa->logs->count())
                                                                <table class="table table-striped">
                                                                    <thead>
                                                                        <tr>
                                                                            <th>Chave Primária</th>
                                                                            <th>Matrícula</th>
                                                                            <th>Usuário</th>
                                                                            <th>Ação</th>
                                                                            <th>Detalhes</th>
                                                                            <th>Data</th>
                                                                        </tr>
                                                                    </thead>
                                                                    <tbody>
                                                                        @foreach ($empresa->logs as $log)
                                                                            <tr>
                                                                                <td>{{ $log->id }}
                                                                                </td>
                                                                                <td>{{ $log->user->matriculation }}
                                                                                </td>
                                                                                <td>{{ $log->user->firstName . ' ' . $log->user->lastName }}
                                                                                </td>
                                                                                <td>{{ $log->action }}</td>
                                                                                <td>{{ $log->details }}</td>
                                                                                <td>{{ $log->created_at->format('d/m/Y H:i:s') }}
                                                                                </td>
                                                                            </tr>
                                                                        @endforeach
                                                                    </tbody>
                                                                </table>
                                                            @else
                                                                <div class="text-center">Nenhum registro encontrado</div>
                                                            @endif
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary"
                                                                data-bs-dismiss="modal">Fechar</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    </tbody>
                </table>
            </div>

            <div class="mt-4">
                @if ($tipo === 'candidatos' && $candidatos)
                    {{ $candidatos->appends(request()->except('page'))->links() }}
                @elseif ($tipo === 'empresas' && $empresas)
                    {{ $empresas->appends(request()->except('page'))->links() }}
                @endif
            </div>

        </div>

    @endsection
