@extends('adminlte::page')

@section('title', 'Tetapan Kebenaran Edit Semula')

@section('content_header')
    <h1><i class="fas fa-cog mr-2"></i>Tetapan — Kebenaran Edit Semula</h1>
@stop

@section('content')
    @if (session('success'))
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <i class="fas fa-check-circle mr-1"></i> {{ session('success') }}
        </div>
    @endif
    @if (session('error'))
        <div class="alert alert-danger alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <i class="fas fa-exclamation-circle mr-1"></i> {{ session('error') }}
        </div>
    @endif
    @if (session('warning'))
        <div class="alert alert-warning alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <i class="fas fa-exclamation-triangle mr-1"></i> {{ session('warning') }}
        </div>
    @endif

    <div class="row">
        <div class="col-md-5">
            <div class="card card-outline card-warning">
                <div class="card-header">
                    <h3 class="card-title"><i class="fas fa-key mr-1"></i> Beri Kebenaran Edit Semula</h3>
                </div>
                <form method="POST" action="{{ route('supervisor.tetapan.store') }}">
                    @csrf
                    <div class="card-body">
                        <p class="text-muted small mb-3">
                            Masukkan <strong>Kod Sekolah</strong> bagi pemohon yang dibenarkan untuk mengemaskini
                            semula data pengisian walaupun rekod tersebut telah <em>diluluskan</em> oleh penyemak.
                        </p>
                        <div class="form-group">
                            <label for="kod_sekolah">Kod Sekolah</label>
                            <input type="text" class="form-control @error('kod_sekolah') is-invalid @enderror"
                                id="kod_sekolah" name="kod_sekolah" value="{{ old('kod_sekolah') }}"
                                placeholder="Contoh: ABA1234" required autofocus>
                            @error('kod_sekolah')
                                <span class="invalid-feedback">{{ $message }}</span>
                            @enderror
                            <small class="form-text text-muted">
                                Sekolah mestilah dalam daerah anda ({{ Auth::user()->daerah ?? '-' }}).
                            </small>
                        </div>
                    </div>
                    <div class="card-footer">
                        <button type="submit" class="btn btn-warning">
                            <i class="fas fa-unlock mr-1"></i> Beri Kebenaran
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <div class="col-md-7">
            <div class="card card-outline card-info">
                <div class="card-header">
                    <h3 class="card-title">
                        <i class="fas fa-list-alt mr-1"></i> Senarai Kebenaran Aktif
                        <span class="badge badge-info ml-1">{{ $aktif->count() }}</span>
                    </h3>
                </div>
                <div class="card-body p-0">
                    <table class="table table-striped table-sm mb-0">
                        <thead class="thead-light">
                            <tr>
                                <th>Kod Sekolah</th>
                                <th>Nama Sekolah</th>
                                <th>Jenis</th>
                                <th>Tahun</th>
                                <th>Diberi Pada</th>
                                <th class="text-right">Tindakan</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($aktif as $p)
                                <tr>
                                    <td><code>{{ $p->kod_sekolah }}</code></td>
                                    <td>{{ $p->user->nama_sekolah ?? '-' }}</td>
                                    <td>{{ $p->jenis_sekolah }}</td>
                                    <td>{{ $p->tahun }}</td>
                                    <td>
                                        <small>{{ optional($p->dibenarkan_pada)->format('d/m/Y H:i') ?? '-' }}</small>
                                    </td>
                                    <td class="text-right">
                                        <form method="POST" action="{{ route('supervisor.tetapan.revoke', $p->id) }}"
                                            class="d-inline"
                                            onsubmit="return confirm('Tarik balik kebenaran edit untuk rekod ini?');">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-xs btn-outline-danger">
                                                <i class="fas fa-lock"></i> Tarik Balik
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="6" class="text-center text-muted py-4">
                                        <i class="fas fa-inbox mr-1"></i> Tiada kebenaran edit semula sedang aktif.
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@stop
