@extends('layouts.app')
@section('title', 'Gestión de Fichas')
@section('content')
<div class="d-flex flex-wrap justify-content-between align-items-center mb-4 gap-3">
    <div>
        <h2 class="mb-0 fw-bold">Listado de Fichas Académicas</h2>
        <p class="text-muted small mb-0">Gestione y exporte la información de los egresados</p>
    </div>
    <div class="d-flex gap-2">
        <!-- Botón Importar (Abre Modal) -->
        <button type="button" class="btn btn-outline-dark rounded-pill px-3" data-bs-toggle="modal" data-bs-target="#importModal">
            <i class="bi bi-file-earmark-arrow-up me-1"></i> Importar CSV
        </button>

        <!-- Botón Exportar Todo -->
        <a href="{{ route('admin.records.export_all') }}" class="btn btn-outline-success rounded-pill px-3">
            <i class="bi bi-file-earmark-excel me-1"></i> Exportar Todo
        </a>

        <!-- Botón Crear Nueva -->
        <a href="{{ route('admin.records.create') }}" class="btn btn-primary rounded-pill px-4">
            <i class="bi bi-plus-lg me-1"></i> Nueva Ficha
        </a>
    </div>
</div>

<div class="card overflow-hidden">
    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0">
            <thead class="table-light">
                <tr class="text-uppercase small fw-bold">
                    <th class="ps-4">Cédula</th>
                    <th>Egresado</th>
                    <th>Cohorte</th>
                    <th>Especialidad</th>
                    <th>Registro</th>
                    <th class="text-end pe-4">Acciones</th>
                </tr>
            </thead>
            <tbody>
                @forelse($records as $record)
                    <tr>
                        <td class="ps-4 font-monospace fw-bold text-primary">{{ $record->dni }}</td>
                        <td>
                            <div class="fw-bold">{{ $record->last_names }}</div>
                            <div class="small text-muted">{{ $record->first_names }}</div>
                        </td>
                        <td><span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle px-3">{{ $record->cohort }}</span></td>
                        <td class="small fw-medium">{{ $record->specialty }}</td>
                        <td class="small text-muted">{{ $record->registration_date->format('d/m/Y') }}</td>
                        <td class="text-end pe-4">
                            <div class="btn-group">
                                <a href="{{ route('admin.records.export_excel', $record->id) }}" class="btn btn-sm btn-light border" title="Exportar a Excel">
                                    <i class="bi bi-file-earmark-excel text-success"></i>
                                </a>
                                <a href="{{ route('admin.records.edit', $record->id) }}" class="btn btn-sm btn-light border" title="Editar">
                                    <i class="bi bi-pencil text-dark"></i>
                                </a>
                                <form action="{{ route('admin.records.destroy', $record->id) }}" method="POST" onsubmit="return confirm('¿Eliminar esta ficha definitivamente?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-light border" title="Eliminar">
                                        <i class="bi bi-trash text-danger"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="text-center py-5 text-muted">
                            <i class="bi bi-inbox fs-1 d-block mb-2 opacity-25"></i>
                            No hay registros cargados.
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="px-4 py-3 border-top bg-light">
        {{ $records->links() }}
    </div>
</div>

<!-- MODAL DE IMPORTACIÓN -->
<div class="modal fade" id="importModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content border-0 shadow-lg" style="border-radius: 20px;">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-bold">Importación Masiva</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ route('admin.records.import') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="modal-body p-4">
                    <p class="text-muted small mb-4">Suba un archivo <strong>CSV</strong> con las cabeceras exactas (dni, first_names, last_names, cohort, specialty, birth_date, registration_date, book, folio, location).</p>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Seleccionar Archivo (.csv)</label>
                        <input type="file" name="file" class="form-control" accept=".csv" required>
                    </div>

                    <div class="alert alert-info border-0 small mb-0">
                        <i class="bi bi-info-circle-fill me-2"></i><strong>Nota:</strong> Si el archivo contiene columnas que coinciden con sus "Campos Personalizados", los datos se cargarán automáticamente.
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light rounded-pill px-4" data-bs-data-bs-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-primary rounded-pill px-4">Comenzar Carga</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
