@extends('layouts.app')
@section('title', (isset($record) ? 'Editar' : 'Nueva') . ' Ficha Académica')
@section('content')
<div class="row justify-content-center">
    <div class="col-md-10">
        <div class="card p-4">
            <div class="mb-4 d-flex justify-content-between align-items-center">
                <div>
                    <h3 class="mb-1 fw-bold">{{ isset($record) ? 'Editar' : 'Nueva' }} Ficha Académica</h3>
                    <p class="text-muted small">Organice la información por secciones para un mejor control</p>
                </div>
                <a href="{{ route('admin.records.index') }}" class="btn btn-light btn-sm rounded-pill px-3">Volver</a>
            </div>

            <form action="{{ isset($record) ? route('admin.records.update', $record->id) : route('admin.records.store') }}" method="POST">
                @csrf
                @if(isset($record)) @method('PUT') @endif

                <div class="row g-4">
                    {{-- SECCIÓN 1: DATOS PERSONALES --}}
                    <div class="col-md-12"><h6 class="fw-bold text-dark mb-0"><i class="bi bi-person-badge me-2"></i>DATOS PERSONALES</h6><hr class="mt-2"></div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold">Nombres *</label>
                        <input type="text" name="first_names" value="{{ old('first_names', $record->first_names ?? '') }}" class="form-control" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold">Apellidos *</label>
                        <input type="text" name="last_names" value="{{ old('last_names', $record->last_names ?? '') }}" class="form-control" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold">Cédula de Identidad *</label>
                        <input type="text" name="dni" value="{{ old('dni', $record->dni ?? '') }}" class="form-control" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold">Fecha de Nacimiento *</label>
                        <input type="date" name="birth_date" value="{{ old('birth_date', isset($record) ? $record->birth_date->format('Y-m-d') : '') }}" class="form-control" required>
                    </div>

                    {{-- CAMPOS PERSONALIZADOS DE SECCIÓN PERSONAL --}}
                    @foreach($customFields->where('section', 'personal') as $field)
                        <div class="col-md-6">
                            <label class="form-label small fw-bold text-primary">{{ $field->label }}</label>
                            <input type="text" name="custom[{{ $field->label }}]" value="{{ old('custom.'.$field->label, $record->custom_values[$field->label] ?? '') }}" class="form-control border-primary-subtle">
                        </div>
                    @endforeach

                    {{-- SECCIÓN 2: EXPEDIENTE ACADÉMICO --}}
                    <div class="col-md-12 mt-5"><h6 class="fw-bold text-dark mb-0"><i class="bi bi-book me-2"></i>EXPEDIENTE ACADÉMICO</h6><hr class="mt-2"></div>
                    <div class="col-md-4">
                        <label class="form-label small fw-bold">Cohorte *</label>
                        <input type="text" name="cohort" value="{{ old('cohort', $record->cohort ?? '') }}" class="form-control" placeholder="Ej: 2024-I" required>
                    </div>
                    <div class="col-md-8">
                        <label class="form-label small fw-bold">Especialidad / Título *</label>
                        <input type="text" name="specialty" value="{{ old('specialty', $record->specialty ?? '') }}" class="form-control" required>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small fw-bold">Libro *</label>
                        <input type="text" name="book" value="{{ old('book', $record->book ?? '') }}" class="form-control" required>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small fw-bold">Folio *</label>
                        <input type="text" name="folio" value="{{ old('folio', $record->folio ?? '') }}" class="form-control" required>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small fw-bold">Fecha de Registro *</label>
                        <input type="date" name="registration_date" value="{{ old('registration_date', isset($record) ? $record->registration_date->format('Y-m-d') : '') }}" class="form-control" required>
                    </div>
                    <div class="col-md-12">
                        <label class="form-label small fw-bold">Ubicación de Archivo Físico *</label>
                        <input type="text" name="location" value="{{ old('location', $record->location ?? '') }}" class="form-control" required>
                    </div>

                    {{-- CAMPOS PERSONALIZADOS DE SECCIÓN ACADÉMICA --}}
                    @foreach($customFields->where('section', 'academic') as $field)
                        <div class="col-md-6">
                            <label class="form-label small fw-bold text-primary">{{ $field->label }}</label>
                            <input type="text" name="custom[{{ $field->label }}]" value="{{ old('custom.'.$field->label, $record->custom_values[$field->label] ?? '') }}" class="form-control border-primary-subtle">
                        </div>
                    @endforeach

                    {{-- SECCIÓN 3: INFORMACIÓN ADICIONAL --}}
                    @if($customFields->where('section', 'additional')->count() > 0)
                        <div class="col-md-12 mt-5"><h6 class="fw-bold text-dark mb-0"><i class="bi bi-info-circle me-2"></i>INFORMACIÓN ADICIONAL</h6><hr class="mt-2"></div>
                        @foreach($customFields->where('section', 'additional') as $field)
                            <div class="col-md-6">
                                <label class="form-label small fw-bold text-primary">{{ $field->label }}</label>
                                <input type="text" name="custom[{{ $field->label }}]" value="{{ old('custom.'.$field->label, $record->custom_values[$field->label] ?? '') }}" class="form-control border-primary-subtle">
                            </div>
                        @endforeach
                    @endif
                </div>

                <div class="mt-5 d-flex gap-2">
                    <button type="submit" class="btn btn-primary px-5 py-2 fw-bold">
                        <i class="bi bi-save me-2"></i>GUARDAR FICHA
                    </button>
                    <a href="{{ route('admin.records.index') }}" class="btn btn-outline-secondary px-4 py-2">Cancelar</a>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
