@extends('adminlte::page')

@section('title', 'Status Pengesahan')

@section('content_header')
    <h1><i class="fas fa-info-circle mr-2"></i>Status Pengesahan</h1>
@stop

@section('content')

    {{-- Pilihan Tahun --}}
    <div class="d-flex align-items-center mb-3">
        <label class="mr-2 mb-0 font-weight-bold"><i class="fas fa-calendar-alt mr-1"></i>Tahun:</label>
        <form method="GET" action="{{ route('pemohon.pengesahan.status') }}" class="d-flex">
            <select name="tahun" class="form-control form-control-sm mr-2" style="width:auto" onchange="this.form.submit()">
                @for ($y = 2026; $y <= 2040; $y++)
                    <option value="{{ $y }}" {{ $tahun == $y ? 'selected' : '' }}>{{ $y }}</option>
                @endfor
            </select>
        </form>
    </div>

    {{-- Status Pengesahan Semasa --}}
    @if ($pengesahanSemasa)
        <div
            class="card card-outline card-{{ $pengesahanSemasa->status === 'lulus' ? 'success' : ($pengesahanSemasa->status === 'ditolak' ? 'danger' : 'warning') }}">
            <div class="card-header">
                <h3 class="card-title">
                    <i class="fas fa-file-alt mr-1"></i> Pengesahan Tahun {{ $tahun }}
                </h3>
            </div>
            <div class="card-body">
                {{-- Status Utama --}}
                <div class="text-center mb-4">
                    @if ($pengesahanSemasa->status === 'belum_semak')
                        <i class="fas fa-clock fa-4x text-warning mb-3"></i>
                        <h3 class="text-warning font-weight-bold">Menunggu Semakan</h3>
                        <p class="text-muted">Pengesahan anda sedang menunggu semakan oleh checker.</p>
                    @elseif ($pengesahanSemasa->status === 'lulus')
                        <i class="fas fa-check-circle fa-4x text-success mb-3"></i>
                        <h3 class="text-success font-weight-bold">Diluluskan</h3>
                        <p class="text-muted">Pengesahan anda telah diluluskan oleh checker.</p>
                    @elseif ($pengesahanSemasa->status === 'ditolak')
                        <i class="fas fa-times-circle fa-4x text-danger mb-3"></i>
                        <h3 class="text-danger font-weight-bold">Ditolak</h3>
                        <p class="text-muted">Pengesahan anda telah ditolak oleh checker.</p>
                    @endif
                </div>

                {{-- Butiran --}}
                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="font-weight-bold" style="width:40%">Tahun</td>
                                <td><span class="badge badge-info">{{ $pengesahanSemasa->tahun }}</span></td>
                            </tr>
                            <tr>
                                <td class="font-weight-bold">Kod Sekolah</td>
                                <td><span class="badge badge-primary">{{ $pengesahanSemasa->kod_sekolah }}</span></td>
                            </tr>
                            <tr>
                                <td class="font-weight-bold">Tarikh Hantar</td>
                                <td>
                                    {{ $pengesahanSemasa->dihantar_pada ? $pengesahanSemasa->dihantar_pada->format('d/m/Y H:i') : '-' }}
                                </td>
                            </tr>
                            <tr>
                                <td class="font-weight-bold">Status</td>
                                <td>
                                    @if ($pengesahanSemasa->status === 'belum_semak')
                                        <span class="badge badge-warning"><i class="fas fa-clock mr-1"></i>Belum
                                            Disemak</span>
                                    @elseif ($pengesahanSemasa->status === 'lulus')
                                        <span class="badge badge-success"><i class="fas fa-check mr-1"></i>Diluluskan</span>
                                    @elseif ($pengesahanSemasa->status === 'ditolak')
                                        <span class="badge badge-danger"><i class="fas fa-times mr-1"></i>Ditolak</span>
                                    @endif
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="font-weight-bold" style="width:40%">Checker</td>
                                <td>{{ $pengesahanSemasa->checker->name ?? 'Belum ditugaskan' }}</td>
                            </tr>
                            @if ($pengesahanSemasa->disemak_pada)
                                <tr>
                                    <td class="font-weight-bold">Tarikh Semakan</td>
                                    <td>{{ $pengesahanSemasa->disemak_pada->format('d/m/Y H:i') }}</td>
                                </tr>
                            @endif
                            @if ($pengesahanSemasa->catatan)
                                <tr>
                                    <td class="font-weight-bold">Catatan Checker</td>
                                    <td>
                                        <div
                                            class="alert alert-{{ $pengesahanSemasa->status === 'lulus' ? 'success' : ($pengesahanSemasa->status === 'ditolak' ? 'danger' : 'info') }} mb-0 py-2 px-3">
                                            <i class="fas fa-comment mr-1"></i>{{ $pengesahanSemasa->catatan }}
                                        </div>
                                    </td>
                                </tr>
                            @endif
                        </table>
                    </div>
                </div>

                {{-- Timeline --}}
                <hr>
                <h6 class="font-weight-bold text-muted mb-3"><i class="fas fa-stream mr-1"></i> Perjalanan Status</h6>
                <div class="timeline">
                    {{-- Dihantar --}}
                    <div class="time-label">
                        <span
                            class="bg-primary">{{ $pengesahanSemasa->dihantar_pada ? $pengesahanSemasa->dihantar_pada->format('d/m/Y') : '-' }}</span>
                    </div>
                    <div>
                        <i class="fas fa-paper-plane bg-primary"></i>
                        <div class="timeline-item">
                            <span class="time"><i
                                    class="fas fa-clock mr-1"></i>{{ $pengesahanSemasa->dihantar_pada ? $pengesahanSemasa->dihantar_pada->format('H:i') : '' }}</span>
                            <h3 class="timeline-header font-weight-bold">Pengesahan Dihantar</h3>
                            <div class="timeline-body">
                                Permohonan bagi tahun {{ $pengesahanSemasa->tahun }} telah dihantar untuk semakan.
                            </div>
                        </div>
                    </div>

                    {{-- Disemak --}}
                    @if ($pengesahanSemasa->disemak_pada)
                        <div class="time-label">
                            <span
                                class="bg-{{ $pengesahanSemasa->status === 'lulus' ? 'success' : 'danger' }}">{{ $pengesahanSemasa->disemak_pada->format('d/m/Y') }}</span>
                        </div>
                        <div>
                            <i
                                class="fas fa-{{ $pengesahanSemasa->status === 'lulus' ? 'check' : 'times' }} bg-{{ $pengesahanSemasa->status === 'lulus' ? 'success' : 'danger' }}"></i>
                            <div class="timeline-item">
                                <span class="time"><i
                                        class="fas fa-clock mr-1"></i>{{ $pengesahanSemasa->disemak_pada->format('H:i') }}</span>
                                <h3 class="timeline-header font-weight-bold">
                                    {{ $pengesahanSemasa->status === 'lulus' ? 'Diluluskan' : 'Ditolak' }}
                                    oleh {{ $pengesahanSemasa->checker->name ?? 'Checker' }}
                                </h3>
                                @if ($pengesahanSemasa->catatan)
                                    <div class="timeline-body">
                                        {{ $pengesahanSemasa->catatan }}
                                    </div>
                                @endif
                            </div>
                        </div>
                    @else
                        <div>
                            <i class="fas fa-clock bg-warning"></i>
                            <div class="timeline-item">
                                <h3 class="timeline-header text-muted">Menunggu semakan oleh checker...</h3>
                            </div>
                        </div>
                    @endif

                    <div><i class="fas fa-flag bg-gray"></i></div>
                </div>
            </div>
        </div>
    @else
        {{-- Belum ada pengesahan --}}
        <div class="card card-outline card-secondary">
            <div class="card-body text-center py-5">
                <i class="fas fa-folder-open fa-4x text-muted mb-3"></i>
                <h4 class="text-muted">Tiada Pengesahan</h4>
                <p class="text-muted mb-3">
                    Anda belum menghantar pengesahan bagi tahun <strong>{{ $tahun }}</strong>.
                </p>
                <a href="{{ route('pemohon.pengesahan.index', ['tahun' => $tahun]) }}" class="btn btn-primary">
                    <i class="fas fa-paper-plane mr-1"></i>Hantar Pengesahan
                </a>
            </div>
        </div>
    @endif

    {{-- Senarai Semua Tahun --}}
    @if ($pengesahans->isNotEmpty())
        <div class="card card-outline card-info mt-3">
            <div class="card-header">
                <h3 class="card-title"><i class="fas fa-history mr-1"></i> Sejarah Pengesahan</h3>
            </div>
            <div class="card-body p-0">
                <table class="table table-hover table-striped mb-0">
                    <thead class="thead-light">
                        <tr>
                            <th>#</th>
                            <th>Tahun</th>
                            <th>Jenis Sekolah</th>
                            <th class="text-center">Tarikh Hantar</th>
                            <th class="text-center">Status</th>
                            <th>Checker</th>
                            <th class="text-center">Tarikh Semakan</th>
                            <th>Catatan</th>
                            <th class="text-center">Tindakan</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($pengesahans as $i => $p)
                            <tr class="{{ $p->tahun == $tahun ? 'table-active font-weight-bold' : '' }}">
                                <td>{{ $i + 1 }}</td>
                                <td>
                                    <a href="{{ route('pemohon.pengesahan.status', ['tahun' => $p->tahun]) }}">
                                        {{ $p->tahun }}
                                    </a>
                                </td>
                                <td><span class="badge badge-info">{{ $p->jenis_sekolah ?? '-' }}</span></td>
                                <td class="text-center">
                                    {{ $p->dihantar_pada ? $p->dihantar_pada->format('d/m/Y H:i') : '-' }}
                                </td>
                                <td class="text-center">
                                    @if ($p->status === 'belum_semak')
                                        <span class="badge badge-warning">Belum Disemak</span>
                                    @elseif ($p->status === 'lulus')
                                        <span class="badge badge-success">Diluluskan</span>
                                    @elseif ($p->status === 'ditolak')
                                        <span class="badge badge-danger">Ditolak</span>
                                    @endif
                                </td>
                                <td>{{ $p->checker->name ?? '-' }}</td>
                                <td class="text-center">
                                    {{ $p->disemak_pada ? $p->disemak_pada->format('d/m/Y H:i') : '-' }}
                                </td>
                                <td>{{ Str::limit($p->catatan, 50) ?? '-' }}</td>
                                <td class="text-center">
                                    <a href="{{ route('pemohon.pengesahan.cetak', $p->id) }}" target="_blank"
                                        class="btn btn-sm btn-outline-danger" title="Lihat / Cetak PDF">
                                        <i class="fas fa-file-pdf mr-1"></i> PDF
                                    </a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    @endif

@stop
