@extends('layouts.app')
@section('title', 'Exams')
@section('page-title', 'Exams & Results')
@section('sidebar-nav')
<div class="nav-item"><a href="{{ route('admin.dashboard') }}"><i class="bi bi-grid"></i> Dashboard</a></div>
<div class="nav-item"><a href="{{ route('admin.students.index') }}"><i class="bi bi-people"></i> Students</a></div>
<div class="nav-item"><a href="{{ route('admin.exams.index') }}"><i class="bi bi-journal-text"></i> Exams & Results</a></div>
<div class="nav-item"><a href="{{ route('admin.attendance.index') }}"><i class="bi bi-calendar-check"></i> Attendance</a></div>
<div class="nav-item"><a href="{{ route('admin.fees.structure') }}"><i class="bi bi-cash-stack"></i> Fee Structure</a></div>
<div class="nav-item"><a href="{{ route('admin.fees.invoices') }}"><i class="bi bi-receipt"></i> Invoices</a></div>
<div class="nav-item"><a href="{{ route('admin.users.index') }}"><i class="bi bi-person-gear"></i> Users</a></div>
@endsection

@section('content')
<div class="page-header">
    <div><h1>Exams & Results</h1></div>
    <button onclick="document.getElementById('createModal').style.display='flex'" class="btn btn-primary">
        <i class="bi bi-plus-lg"></i> Create Exam
    </button>
</div>

<div class="card">
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Name</th><th>Type</th><th>Term</th><th>Weight</th><th>Status</th><th>Actions</th></tr>
            </thead>
            <tbody>
                @forelse($exams as $exam)
                <tr>
                    <td><strong>{{ $exam->name }}</strong></td>
                    <td><span class="badge badge-info">{{ strtoupper($exam->type) }}</span></td>
                    <td>{{ $exam->term->name }}</td>
                    <td>{{ $exam->weight }}%</td>
                    <td>
                        <span class="badge {{ $exam->is_published ? 'badge-success' : 'badge-warning' }}">
                            {{ $exam->is_published ? 'Published' : 'Draft' }}
                        </span>
                    </td>
                    <td style="display:flex;gap:6px;">
                        @if(!$exam->is_published)
                        <form method="POST" action="{{ route('admin.exams.publish', $exam) }}" style="display:inline;">
                            @csrf
                            <button type="submit" class="btn btn-sm btn-primary" title="Publish">
                                <i class="bi bi-send"></i>
                            </button>
                        </form>
                        @endif
                    </td>
                </tr>
                @empty
                <tr><td colspan="6" style="text-align:center;padding:40px;color:var(--gray);">No exams yet</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div style="padding:16px;">{{ $exams->links() }}</div>
</div>

<!-- Create Exam Modal -->
<div id="createModal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:2000;align-items:center;justify-content:center;">
    <div class="card" style="width:100%;max-width:500px;margin:20px;">
        <div class="card-header">
            <h5>Create Exam</h5>
            <button onclick="document.getElementById('createModal').style.display='none'" style="background:none;border:none;cursor:pointer;font-size:20px;">&times;</button>
        </div>
        <div class="card-body">
            <form method="POST" action="{{ route('admin.exams.store') }}">
                @csrf
                <div class="form-group">
                    <label>Exam Name *</label>
                    <input type="text" name="name" placeholder="e.g. End of Term 1 2025" required>
                </div>
                <div class="grid grid-2">
                    <div class="form-group">
                        <label>Type *</label>
                        <select name="type" required>
                            <option value="cat">CAT</option>
                            <option value="midterm">Mid-Term</option>
                            <option value="endterm" selected>End-Term</option>
                            <option value="mock">Mock</option>
                            <option value="kcse">KCSE</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Weight (%)</label>
                        <input type="number" name="weight" value="100" min="1" max="100">
                    </div>
                    <div class="form-group">
                        <label>Term *</label>
                        <select name="term_id" required>
                            <option value="">Select Term</option>
                            @foreach(\App\Models\Term::whereHas('academicYear', fn($q) => $q->where('school_id', auth()->user()->school_id))->get() as $term)
                                <option value="{{ $term->id }}">{{ $term->academicYear->name }} - {{ $term->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Start Date</label>
                        <input type="date" name="start_date">
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Create Exam</button>
            </form>
        </div>
    </div>
</div>
@endsection
