@extends('adminlte::page')

@section('title', "Detail Lebihan Buku — {{ $darjah }}")

@section('content_header')
    <h1><i class="fas fa-boxes mr-2"></i>Detail Lebihan Buku — {{ $darjah }}</h1>
@stop

@section('content')
    <div class="mb-3">
        <a href="{{ route('admin.laporan.lebih-buku', ['daerah' => $daerah, 'tahun' => $tahun]) }}"
            class="btn btn-outline-secondary btn-sm">
            <i class="fas fa-arrow-left mr-1"></i> Kembali ke Laporan Lebih Buku
        </a>
    </div>

    <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap">
        <h5 class="text-muted mb-2">
            <i class="fas fa-school mr-1"></i> Senarai Sekolah Yang Ada Lebihan Buku ({{ $darjah }})
            @if ($daerah)
                — Daerah: <span class="badge badge-primary">{{ $daerah }}</span>
            @else
                — <span class="badge badge-secondary">Semua Daerah</span>
            @endif
            — Tahun: <span class="badge badge-info">{{ $tahun }}</span>
        </h5>
        <form method="GET" action="{{ route('admin.laporan.lebih-buku-detail') }}" class="form-inline no-print">
            <input type="hidden" name="darjah" value="{{ $darjah }}">
            <label class="mr-2 text-muted"><i class="fas fa-map-marker-alt mr-1"></i>Daerah:</label>
            <select name="daerah" class="form-control form-control-sm mr-3" onchange="this.form.submit()">
                <option value="">Semua Daerah</option>
                @foreach ($senarai_daerah as $d)
                    <option value="{{ $d }}" {{ $d == $daerah ? 'selected' : '' }}>{{ $d }}</option>
                @endforeach
            </select>
            <label class="mr-2 text-muted"><i class="fas fa-calendar-alt mr-1"></i>Tahun:</label>
            <select name="tahun" class="form-control form-control-sm" onchange="this.form.submit()">
                @foreach ($senarai_tahun as $t)
                    <option value="{{ $t }}" {{ $t == $tahun ? 'selected' : '' }}>{{ $t }}</option>
                @endforeach
            </select>
        </form>
    </div>

    @if ($senarai->isEmpty())
        <div class="card">
            <div class="card-body text-center text-muted py-4">
                <i class="fas fa-info-circle mr-1"></i> Tiada sekolah yang ada lebihan buku untuk {{ $darjah }}.
            </div>
        </div>
    @else
        <div class="card card-outline card-success mb-3">
            <div class="card-header">
                <h3 class="card-title"><i class="fas fa-list-ol mr-1"></i> Ringkasan Lebihan — {{ $senarai->count() }}
                    sekolah</h3>
            </div>
            <div class="card-body p-0">
                <table class="table table-hover table-striped mb-0">
                    <thead class="thead-light">
                        <tr>
                            <th>Bil</th>
                            <th>Nama Sekolah</th>
                            <th>Kod Sekolah</th>
                            <th>Daerah</th>
                            <th class="text-center">Jumlah Lebihan</th>
                            <th class="text-center">Tindakan</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($senarai as $i => $item)
                            @php $user = $item['user']; @endphp
                            <tr>
                                <td>{{ $i + 1 }}</td>
                                <td>{{ $user->nama_sekolah ?? '-' }}</td>
                                <td><span class="badge badge-primary">{{ $user->kod_sekolah ?? '-' }}</span></td>
                                <td>{{ $user->daerah ?? '-' }}</td>
                                <td
                                    class="text-center font-weight-bold {{ $item['total_lebihan'] < 0 ? 'text-danger' : 'text-success' }}">
                                    {{ number_format($item['total_lebihan']) }}
                                </td>
                                <td class="text-center">
                                    <button class="btn btn-success btn-sm" data-toggle="collapse"
                                        data-target="#detail{{ $user->id }}">
                                        <i class="fas fa-eye mr-1"></i>Detail
                                    </button>
                                </td>
                            </tr>
                            <tr class="collapse" id="detail{{ $user->id }}">
                                <td colspan="6" class="p-0">
                                    <table class="table table-sm table-bordered mb-0 bg-light">
                                        <thead>
                                            <tr class="bg-secondary text-white">
                                                <th>Bil</th>
                                                <th>Kod Buku</th>
                                                <th>Judul Buku</th>
                                                <th class="text-center">Lebihan</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach ($item['buku'] as $j => $buku)
                                                <tr>
                                                    <td>{{ $j + 1 }}</td>
                                                    <td>{{ $buku->kod_buku }}</td>
                                                    <td>{{ $buku->judul }}</td>
                                                    <td
                                                        class="text-center font-weight-bold {{ $buku->lebihan_buku < 0 ? 'text-danger' : 'text-success' }}">
                                                        {{ number_format($buku->lebihan_buku) }}
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                    <tfoot class="thead-light">
                        <tr>
                            @php $grandTotal = $senarai->sum('total_lebihan'); @endphp
                            <th colspan="4" class="text-right">Jumlah Keseluruhan:</th>
                            <th class="text-center {{ $grandTotal < 0 ? 'text-danger' : 'text-success' }}">
                                {{ number_format($grandTotal) }}</th>
                            <th></th>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    @endif
@stop
