@extends('layouts.app')
@section('title', 'Configurar Campos')
@section('content')
<div class="row justify-content-center">
    <div class="col-md-10">
        <div class="card p-4">
            <div class="mb-4">
                <h3 class="fw-bold">Gestión de Campos Dinámicos</h3>
                <p class="text-muted small">Configure dónde desea que aparezcan los datos adicionales de cada estudiante.</p>
            </div>

            <div class="bg-light p-4 rounded-4 mb-5 border">
                <h6 class="fw-bold mb-3"><i class="bi bi-plus-circle me-2"></i>Nuevo Campo Personalizado</h6>
                <form action="{{ route('admin.custom_fields.store') }}" method="POST">
                    @csrf
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-bold">Nombre de la Etiqueta</label>
                            <input type="text" name="label" class="form-control" placeholder="Ej: Promedio General" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-bold">Ubicación (Sección)</label>
                            <select name="section" class="form-select" required>
                                @foreach($sections as $key => $val)
                                    <option value="{{ $key }}">{{ $val }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button class="btn btn-primary w-100" type="submit">Agregar</button>
                        </div>
                    </div>
                </form>
            </div>

            <div class="row row-cols-1 row-cols-md-3 g-4">
                @foreach($sections as $key => $sectionName)
                    <div class="col">
                        <div class="h-100 p-3 border rounded-4 bg-white">
                            <h6 class="fw-bold text-primary border-bottom pb-2 mb-3">
                                <i class="bi bi-layout-sidebar-inset me-2"></i>{{ $sectionName }}
                            </h6>
                            <div class="list-group list-group-flush">
                                @forelse($fields->where('section', $key) as $field)
                                    <div class="list-group-item d-flex justify-content-between align-items-center px-0 py-2 border-0">
                                        <span class="small fw-medium">{{ $field->label }}</span>
                                        <form action="{{ route('admin.custom_fields.destroy', $field->id) }}" method="POST">
                                            @csrf @method('DELETE')
                                            <button type="submit" class="btn btn-link text-danger btn-sm p-0">
                                                <i class="bi bi-x-circle"></i>
                                            </button>
                                        </form>
                                    </div>
                                @empty
                                    <div class="text-center py-3 text-muted small italic">Sin campos extras</div>
                                @endforelse
                            </div>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    </div>
</div>
@endsection
