@extends('layouts.app')
@section('title', $exam->name)
@section('page_title', $exam->name)
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('exams.index') }}">Exams</a></li><li class="breadcrumb-item active">{{ $exam->name }}</li>
@endsection
@section('page_actions')
    @can('exams.update')
        <a href="{{ route('marks.index', ['exam_id' => $exam->id]) }}" class="btn btn-outline-primary"><i class="bi bi-pencil-square me-1"></i>Enter Marks</a>
    @endcan
    <a href="{{ route('results.gazette', ['exam_id' => $exam->id]) }}" class="btn btn-primary"><i class="bi bi-table me-1"></i>Gazette</a>
@endsection

@section('content')
    <div class="row g-3">
        <div class="col-lg-4">
            <div class="card"><div class="card-body">
                <dl class="row small mb-0">
                    <dt class="col-5 text-muted">Type</dt><dd class="col-7">{{ $exam->type_label }}</dd>
                    <dt class="col-5 text-muted">Session</dt><dd class="col-7">{{ $exam->academicSession?->name ?? '—' }}</dd>
                    <dt class="col-5 text-muted">Dates</dt><dd class="col-7">{{ optional($exam->start_date)->format('d M') }} – {{ optional($exam->end_date)->format('d M Y') }}</dd>
                    <dt class="col-5 text-muted">Status</dt><dd class="col-7"><span class="badge bg-soft-{{ $exam->is_published ? 'success' : 'secondary' }}">{{ $exam->is_published ? 'Published' : 'Draft' }}</span></dd>
                </dl>
            </div></div>

            @can('exams.update')
                <div class="card mt-3"><div class="card-header"><i class="bi bi-plus-circle me-1"></i>Generate Papers</div><div class="card-body">
                    <p class="small text-muted">Create papers for all of a class's allocated subjects.</p>
                    <form method="POST" action="{{ route('exams.papers.generate', $exam) }}">
                        @csrf
                        <div class="mb-2"><label class="form-label small">Class</label>
                            <select name="class_id" class="form-select form-select-sm" required>
                                <option value="">— Select class —</option>
                                @foreach($classes as $c)<option value="{{ $c->id }}">{{ $c->name }}</option>@endforeach
                            </select></div>
                        <div class="row g-2">
                            <div class="col-6"><label class="form-label small">Total Marks</label><input type="number" name="total_marks" value="100" class="form-control form-control-sm"></div>
                            <div class="col-6"><label class="form-label small">Passing</label><input type="number" name="passing_marks" value="40" class="form-control form-control-sm"></div>
                        </div>
                        <button class="btn btn-sm btn-primary mt-2 w-100"><i class="bi bi-plus me-1"></i>Generate</button>
                    </form>
                </div></div>
            @endcan
        </div>

        <div class="col-lg-8">
            <div class="card"><div class="card-header"><i class="bi bi-journals me-1"></i>Papers ({{ $exam->papers->count() }})</div><div class="card-body">
                @forelse($papersByClass as $classId => $papers)
                    <h6 class="mb-2">{{ $papers->first()->schoolClass?->name }}</h6>
                    <table class="table table-sm align-middle mb-3">
                        <thead><tr><th>Subject</th><th class="text-end">Total</th><th class="text-end">Passing</th><th></th></tr></thead>
                        <tbody>
                            @foreach($papers as $paper)
                                <tr>
                                    <td>{{ $paper->subject?->name }}</td>
                                    <td class="text-end">{{ number_format($paper->total_marks) }}</td>
                                    <td class="text-end">{{ number_format($paper->passing_marks) }}</td>
                                    <td class="text-end">
                                        @can('exams.update')
                                            <a href="{{ route('marks.index', ['exam_id'=>$exam->id,'class_id'=>$paper->class_id,'subject_id'=>$paper->subject_id]) }}" class="btn btn-sm btn-light border py-0" title="Enter marks"><i class="bi bi-pencil"></i></a>
                                            <form method="POST" action="{{ route('exam-papers.destroy', $paper) }}" class="d-inline" data-confirm="Remove paper?">@csrf @method('DELETE')<button class="btn btn-sm btn-light border py-0 text-danger"><i class="bi bi-x"></i></button></form>
                                        @endcan
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                @empty
                    <p class="text-muted text-center py-3">No papers yet. Use "Generate Papers" to add subjects per class.</p>
                @endforelse
            </div></div>
        </div>
    </div>
@endsection
