@extends('layouts.app')
@section('title', 'Marks Entry')
@section('page_title', 'Marks Entry')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li><li class="breadcrumb-item active">Marks Entry</li>
@endsection

@section('content')
    <div class="card mb-3"><div class="card-body">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-4"><label class="form-label small">Exam</label>
                <select name="exam_id" class="form-select form-select-sm" onchange="this.form.submit()">
                    <option value="">— Select exam —</option>
                    @foreach($exams as $e)<option value="{{ $e->id }}" @selected($exam && $exam->id==$e->id)>{{ $e->name }}</option>@endforeach
                </select></div>
            @if($exam)
                @php $classOptions = $papers->groupBy('class_id'); @endphp
                <div class="col-md-4"><label class="form-label small">Class</label>
                    <select name="class_id" class="form-select form-select-sm" onchange="this.form.submit()">
                        <option value="">— Select class —</option>
                        @foreach($classOptions as $cid => $ps)<option value="{{ $cid }}" @selected($classId==$cid)>{{ $ps->first()->schoolClass?->name }}</option>@endforeach
                    </select></div>
                <div class="col-md-4"><label class="form-label small">Subject</label>
                    <select name="subject_id" class="form-select form-select-sm" onchange="this.form.submit()">
                        <option value="">— Select subject —</option>
                        @if($classId)@foreach($papers->where('class_id', $classId) as $p)<option value="{{ $p->subject_id }}" @selected($subjectId==$p->subject_id)>{{ $p->subject?->name }}</option>@endforeach @endif
                    </select></div>
            @endif
        </form>
    </div></div>

    @if($paper && $students->isNotEmpty())
        <form method="POST" action="{{ route('marks.store') }}">
            @csrf
            <input type="hidden" name="exam_id" value="{{ $exam->id }}">
            <input type="hidden" name="class_id" value="{{ $classId }}">
            <input type="hidden" name="subject_id" value="{{ $subjectId }}">
            <div class="card">
                <div class="card-header">{{ $paper->subject?->name }} — Total: {{ number_format($paper->total_marks) }}, Passing: {{ number_format($paper->passing_marks) }}</div>
                <div class="card-body">
                    <table class="table table-hover align-middle">
                        <thead><tr><th>#</th><th>Student</th><th style="width:160px">Obtained</th><th style="width:120px">Absent</th></tr></thead>
                        <tbody>
                            @foreach($students as $i => $student)
                                @php $rec = $existing->get($student->id); @endphp
                                <tr>
                                    <td>{{ $i + 1 }}</td>
                                    <td><div class="d-flex align-items-center gap-2"><span class="sms-avatar sm">{{ $student->initials }}</span><div><div class="fw-semibold">{{ $student->name }}</div><div class="small text-muted">{{ $student->admission_number }}</div></div></div></td>
                                    <td><input type="number" step="0.01" min="0" max="{{ $paper->total_marks }}" name="marks[{{ $student->id }}]" value="{{ $rec && !$rec->is_absent ? $rec->obtained_marks : '' }}" class="form-control form-control-sm"></td>
                                    <td><div class="form-check"><input type="checkbox" name="absent[{{ $student->id }}]" value="1" class="form-check-input" @checked($rec?->is_absent)><label class="form-check-label small">Absent</label></div></td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="card-footer text-end"><button class="btn btn-primary"><i class="bi bi-save me-1"></i>Save Marks</button></div>
            </div>
        </form>
    @elseif($exam)
        <div class="card"><div class="card-body text-center text-muted py-4">Select a class and subject to enter marks.</div></div>
    @else
        <div class="card"><div class="card-body text-center text-muted py-4">Select an exam to begin.</div></div>
    @endif
@endsection
