@extends('layouts.student')
@section('title', 'Results')
@section('page_title', 'My Results')

@section('content')
    <div class="card mb-3"><div class="card-body py-2">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-6"><label class="form-label small">Exam</label>
                <select name="exam_id" class="form-select form-select-sm" onchange="this.form.submit()">
                    @forelse($exams as $e)<option value="{{ $e->id }}" @selected($exam && $exam->id==$e->id)>{{ $e->name }}</option>@empty<option>No published results</option>@endforelse
                </select></div>
        </form>
    </div></div>

    @if($exam && $result && $result['has_marks'])
        <div class="card"><div class="card-body">
            <div class="d-flex justify-content-between border-bottom pb-2 mb-3">
                <h5 class="mb-0">{{ $exam->name }}</h5>
                <button onclick="window.print()" class="btn btn-sm btn-outline-primary no-print"><i class="bi bi-printer me-1"></i>Print</button>
            </div>
            <table class="table table-bordered align-middle">
                <thead class="table-light"><tr><th>Subject</th><th class="text-end">Total</th><th class="text-end">Obtained</th><th class="text-end">%</th><th class="text-center">Grade</th><th class="text-center">Result</th></tr></thead>
                <tbody>
                    @foreach($result['rows'] as $row)
                        <tr>
                            <td>{{ $row['subject'] }}</td>
                            <td class="text-end">{{ number_format($row['total']) }}</td>
                            <td class="text-end">{{ $row['absent'] ? 'ABS' : number_format($row['obtained']) }}</td>
                            <td class="text-end">{{ $row['percentage'] }}%</td>
                            <td class="text-center"><span class="badge bg-soft-{{ $row['pass'] ? 'success' : 'danger' }}">{{ $row['grade'] }}</span></td>
                            <td class="text-center">{{ $row['pass'] ? 'Pass' : 'Fail' }}</td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot class="table-light fw-bold"><tr><td>Total</td><td class="text-end">{{ number_format($result['total_max']) }}</td><td class="text-end">{{ number_format($result['total_obtained']) }}</td><td class="text-end">{{ $result['percentage'] }}%</td><td class="text-center">{{ $result['grade'] }}</td><td class="text-center">{{ $result['status'] }}</td></tr></tfoot>
            </table>
            <div class="row g-2 mt-1">
                <div class="col"><div class="border rounded p-2 text-center"><div class="small text-muted">Percentage</div><div class="h5 mb-0">{{ $result['percentage'] }}%</div></div></div>
                <div class="col"><div class="border rounded p-2 text-center"><div class="small text-muted">Grade</div><div class="h5 mb-0">{{ $result['grade'] }}</div></div></div>
                <div class="col"><div class="border rounded p-2 text-center"><div class="small text-muted">GPA</div><div class="h5 mb-0">{{ $result['gpa'] }}</div></div></div>
                <div class="col"><div class="border rounded p-2 text-center"><div class="small text-muted">Position</div><div class="h5 mb-0">{{ $position ?? '—' }}</div></div></div>
                <div class="col"><div class="border rounded p-2 text-center"><div class="small text-muted">Result</div><div class="h5 mb-0 text-{{ $result['status']==='Pass' ? 'success' : 'danger' }}">{{ $result['status'] }}</div></div></div>
            </div>
        </div></div>
    @else
        <div class="card"><div class="card-body text-center text-muted py-4">No published results available yet.</div></div>
    @endif
@endsection
@push('styles')<style>@media print { .sms-topbar, .page-head, footer, .no-print, .d-md-none { display:none !important; } }</style>@endpush
