

<?php $__env->startSection('title', 'Rekapitulasi Kehadiran'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $type  = request('type', 'siswa');

    $daftar_kelas = [
        'VII-A','VII-B','VII-C','VII-D',
        'VIII-A','VIII-B','VIII-C','VIII-D',
        'IX-A','IX-B','IX-C','IX-D',
    ];

    // ⚠️ FIX: SEBELUMNYA di sini ada kode yang menghitung ulang $tahun/$bulan
    // sendiri dari request('periode') secara mentah (explode('-', ...)),
    // terpisah dari $tahun/$bulan yang sudah divalidasi & di-fallback oleh
    // resolvePeriode() di controller. Akibatnya kalau ada query string yang
    // aneh (mis. ?periode=abc atau bulan di luar 1-12), TEKS JUDUL di
    // halaman ini bisa sempat menampilkan nilai mentah yang belum tervalidasi,
    // padahal DATA ($data_rekap) yang ditampilkan tetap pakai versi yang
    // sudah di-fallback controller — jadi bisa beda antara judul & isi tabel.
    //
    // Sekarang blade ini TIDAK menghitung ulang apa-apa. $tahun, $bulan, dan
    // $minggu dipakai persis seperti yang dikirim controller (rekapBulanan()),
    // supaya satu-satunya sumber kebenaran ada di controller.
    $namaBulan = [
        '01'=>'Januari','02'=>'Februari','03'=>'Maret','04'=>'April',
        '05'=>'Mei','06'=>'Juni','07'=>'Juli','08'=>'Agustus',
        '09'=>'September','10'=>'Oktober','11'=>'November','12'=>'Desember',
    ];

    // Batas maksimal minggu yang boleh dipilih, dikirim controller. Fallback
    // ke 5 kalau entah kenapa tidak dikirim, supaya view tetap tidak error.
    $mingguMaksimal = $mingguMaksimal ?? 5;
    $ambangAlpaMingguan = $ambangAlpaMingguan ?? 4;

    // $minggu di sini adalah properti yang DIKIRIM CONTROLLER (sudah
    // divalidasi & di-null-kan kalau minggunya belum boleh dipilih),
    // BUKAN request('minggu') mentah — supaya label & isi tabel selalu
    // menunjukkan periode yang sama persis.
    $labelMinggu = $minggu ? "Minggu ke-$minggu" : 'Full sebulan';
?>

<style>
    :root {
        --primary:      #4f46e5;
        --primary-dark: #4338ca;
        --bg-soft:      #f8fafc;
        --border:       #e2e8f0;
        --text-dark:    #1e293b;
        --text-muted:   #64748b;
    }

    .report-card {
        background: #fff;
        border-radius: 16px;
        border: 1px solid var(--border);
        box-shadow: 0 1px 4px rgba(0,0,0,.04);
        overflow: hidden;
    }

    .report-header {
        padding: 22px 26px;
        border-bottom: 1px solid var(--border);
    }

    .filter-section {
        padding: 18px 24px;
        border-bottom: 1px solid var(--border);
        background: var(--bg-soft);
    }

    /* Tab pills */
    .nav-tabs-custom {
        display: flex;
        gap: 4px;
        background: #fff;
        border: 1px solid var(--border);
        border-radius: 10px;
        padding: 4px;
    }
    .nav-tabs-custom .nav-link {
        border: none; border-radius: 7px;
        padding: 7px 16px; font-size: 13px; font-weight: 600;
        color: var(--text-muted); transition: background .15s, color .15s;
        white-space: nowrap;
    }
    .nav-tabs-custom .nav-link:hover:not(.active) { color: var(--primary); background: var(--bg-soft); }
    .nav-tabs-custom .nav-link.active { background: #fff; color: var(--primary); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

    /* Form */
    .form-label-sm {
        display: block; font-size: 11px; font-weight: 700;
        text-transform: uppercase; letter-spacing: .05em;
        color: var(--text-muted); margin-bottom: 5px;
    }
    .form-control, .form-select {
        border-radius: 8px; border: 1px solid var(--border);
        font-size: 13px; padding: 8px 12px;
        background: #fff; color: var(--text-dark);
        transition: border-color .2s, box-shadow .2s;
    }
    .form-control:focus, .form-select:focus {
        border-color: var(--primary);
        box-shadow: 0 0 0 3px rgba(79,70,229,.1);
        outline: none; background: #fff;
    }
    .form-select:disabled { background: #f1f5f9; color: var(--text-muted); cursor: not-allowed; }

    /* Buttons */
    .btn-filter {
        background: var(--primary); color: #fff; border: none;
        border-radius: 8px; padding: 8px 14px; font-size: 13px; font-weight: 600;
        transition: background .15s; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
        white-space: nowrap; cursor: pointer;
    }
    .btn-filter:hover { background: var(--primary-dark); color: #fff; }

    .btn-print {
        background: var(--text-dark); color: #fff; border-radius: 8px;
        padding: 7px 16px; font-size: 13px; font-weight: 600;
        text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
        transition: background .15s; white-space: nowrap; border: none; cursor: pointer;
    }
    .btn-print:hover { background: #0f172a; color: #fff; }

    .btn-evaluasi {
        background: #fff; color: var(--primary); border: 1px solid var(--primary);
        border-radius: 20px; padding: 3px 10px; font-size: 11px; font-weight: 700;
        display: inline-flex; align-items: center; gap: 4px; cursor: pointer;
        white-space: nowrap; margin-left: 6px; margin-top: 4px;
    }
    .btn-evaluasi:hover { background: var(--primary); color: #fff; }

    /* Flag info bar */
    .flag-bar {
        padding: 12px 24px;
        border-bottom: 1px solid var(--border);
        background: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        gap: 10px;
    }
    .flag-legend {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 12px;
        color: var(--text-muted);
        flex-wrap: wrap;
    }

    /* Table */
    .table-report { margin: 0; width: 100%; border-collapse: collapse; }
    .table-report thead th {
        background: var(--bg-soft); font-size: 11px; text-transform: uppercase;
        letter-spacing: .05em; font-weight: 700; color: var(--text-muted);
        padding: 12px 16px; border-bottom: 1px solid var(--border); white-space: nowrap;
    }
    .table-report tbody td {
        padding: 13px 16px; vertical-align: middle;
        border-bottom: 1px solid #f1f5f9; color: var(--text-dark); font-size: 13px;
    }
    .table-report tbody tr:last-child td { border-bottom: none; }
    .table-report tbody tr:hover td { background: #fafbff; }
    .table-report tbody tr.row-flag td { background: #fef9f9; }
    .table-report tbody tr.row-flag:hover td { background: #fef2f2; }
    .table-report tbody tr.row-eskalasi td { background: #fef3f2; }
    .table-report tbody tr.row-eskalasi:hover td { background: #fee4e2; }

    /* Badges */
    .badge-id {
        display: inline-block; background: var(--bg-soft);
        border: 1px solid var(--border); padding: 3px 8px;
        border-radius: 6px; font-family: 'Consolas','Monaco',monospace;
        font-size: 11px; font-weight: 600; color: var(--text-muted);
    }
    .badge-pct {
        display: inline-block; padding: 4px 12px; border-radius: 7px;
        font-size: 12px; font-weight: 700; min-width: 56px; text-align: center;
    }
    .badge-pct.ok  { background: #f0fdf4; color: #166534; }
    .badge-pct.bad { background: #fef2f2; color: #991b1b; }

    .badge-flag, .badge-eskalasi, .badge-dievaluasi, .badge-pemantauan {
        display: inline-flex; align-items: center; gap: 4px;
        font-size: 10px; font-weight: 700; padding: 2px 8px; border-radius: 20px;
        vertical-align: middle;
    }
    
    .badge-flag { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; margin-left: 6px; white-space: nowrap; }
    .badge-eskalasi { background: #7f1d1d; color: #fff; border: 1px solid #7f1d1d; margin-left: 6px; white-space: nowrap; }
    .badge-dievaluasi { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; margin-top: 4px; }
    .badge-pemantauan { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; margin-top: 4px; }

    /* Stat cells */
    .stat-hadir { color: #16a34a; font-weight: 600; display: inline-flex; align-items: center; gap: 4px; }
    .stat-izin  { color: #d97706; font-weight: 600; display: inline-flex; align-items: center; gap: 4px; }
    .stat-alpa  { color: #dc2626; font-weight: 600; display: inline-flex; align-items: center; gap: 4px; }

    /* Empty state */
    .empty-state { padding: 56px 20px; text-align: center; }
    .empty-state i { font-size: 36px; color: #cbd5e1; display: block; margin-bottom: 12px; }

    /* ==========================================================
       TAMPILAN MOBILE (ANTI-SCROLL HORIZONTAL)
    ========================================================== */
    @media (max-width: 768px) {
        .report-header, .filter-section, .flag-bar { padding: 14px 16px; }
        
        /* Layout Header: tombol sejajar dengan tab */
        .report-header .d-flex.gap-2 { width: 100%; justify-content: space-between; }
        .nav-tabs-custom { flex-grow: 1; }
        
        /* Hapus fungsi scroll bawaan container di mobile */
        .table-responsive { overflow-x: visible !important; }

        .table-report thead { display: none; }
        .table-report tr {
            display: block; border: 1px solid var(--border);
            border-radius: 12px; margin-bottom: 16px; padding: 0; background: #fff;
            box-shadow: 0 1px 3px rgba(0,0,0,.04);
        }
        
        .table-report tbody tr.row-flag { background: #fff; border-color: #fecaca; }
        .table-report tbody tr.row-eskalasi { background: #fff; border-color: #fca5a5; }

        .table-report td {
            display: flex; justify-content: space-between; align-items: flex-start;
            font-size: 13px; border: none; padding: 12px 16px;
            border-bottom: 1px solid #f1f5f9; text-align: right; gap: 12px;
        }
        .table-report td:last-child { border-bottom: none; }
        
        /* Labeling baris kartu */
        .table-report td::before {
            content: attr(data-label); font-size: 11px; font-weight: 700;
            text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.05em;
            text-align: left; flex-shrink: 0; padding-top: 2px;
        }

        /* Khusus Baris Nama (Header Kartu) */
        .table-report td[data-label="Nama"] {
            flex-direction: column; align-items: flex-start; background: #f8fafc;
            border-radius: 12px 12px 0 0; text-align: left; padding-bottom: 16px;
        }
        .table-report tbody tr.row-flag td[data-label="Nama"] { background: #fef9f9; }
        .table-report tbody tr.row-eskalasi td[data-label="Nama"] { background: #fef3f2; }
        
        .table-report td[data-label="Nama"]::before { display: none; }
        
        /* Penyesuaian Badge & Tombol di Mobile */
        .badge-flag, .badge-eskalasi { margin-left: 0; margin-top: 6px; }
        .badge-dievaluasi, .badge-pemantauan { white-space: normal; text-align: left; }
        .btn-evaluasi {
            width: 100%; justify-content: center; margin-left: 0; margin-top: 12px;
            padding: 8px; white-space: normal; line-height: 1.4;
        }

        /* Khusus Baris Persentase */
        .table-report td[data-label="Persentase"] { align-items: center; }

        /* Penanganan Data Kosong di Layar Kecil */
        .table-report tbody tr.empty-row { border: none; box-shadow: none; background: transparent; }
        .table-report tbody tr.empty-row td { display: block; text-align: center; padding: 40px 16px; border: none; }
        .table-report tbody tr.empty-row td::before { display: none; }
        
        /* Khusus Kolom No - Disembunyikan di layout kartu */
        .table-report td[data-label="No"] { display: none; }
    }
</style>

<div class="container-fluid py-4">
    <div class="report-card">

        
        <div class="report-header d-flex flex-wrap justify-content-between align-items-center gap-3">
            <div>
                <h5 class="fw-bold mb-1">Rekapitulasi <?php echo e(ucfirst($type)); ?></h5>
                <p class="text-muted small mb-0 d-flex align-items-center gap-2 flex-wrap">
                    <i class="bi bi-calendar3"></i>
                    Periode: <?php echo e($namaBulan[sprintf('%02d', $bulan)]); ?> <?php echo e($tahun); ?>

                    <span class="badge bg-primary bg-opacity-10 text-primary border border-primary-subtle rounded-pill px-2 py-1"
                          style="font-size: 11px;">
                        <?php echo e($labelMinggu); ?>

                    </span>
                </p>
            </div>

            <div class="d-flex align-items-center gap-2 flex-wrap w-sm-100 mt-2 mt-md-0">
                <ul class="nav nav-tabs-custom mb-0 flex-grow-1">
                    <li class="nav-item w-50 text-center">
                        <a href="<?php echo e(request()->fullUrlWithQuery(['type' => 'siswa'])); ?>"
                           class="nav-link w-100 <?php echo e($type === 'siswa' ? 'active' : ''); ?>">
                            <i class="bi bi-people-fill me-1"></i>Siswa
                        </a>
                    </li>
                    <li class="nav-item w-50 text-center">
                        <a href="<?php echo e(request()->fullUrlWithQuery(['type' => 'guru'])); ?>"
                           class="nav-link w-100 <?php echo e($type === 'guru' ? 'active' : ''); ?>">
                            <i class="bi bi-person-badge-fill me-1"></i>Guru
                        </a>
                    </li>
                </ul>
                <a href="<?php echo e(route('admin.laporan.cetak', request()->all())); ?>" class="btn-print mt-2 mt-sm-0 w-sm-100">
                    <i class="bi bi-printer"></i>Cetak
                </a>
            </div>
        </div>

        
        <div class="filter-section">
            <form method="GET" action="<?php echo e(route('admin.laporan.bulanan')); ?>">
                <input type="hidden" name="type" value="<?php echo e($type); ?>">
                <div class="row g-2 align-items-end">

                    <?php if($type === 'siswa'): ?>
                    <div class="col-12 col-sm-4 col-xl-2">
                        <label class="form-label-sm">Kelas</label>
                        <select name="kelas" id="kelasSelect" class="form-select">
                            <option value="">Semua Kelas</option>
                            <?php $__currentLoopData = $daftar_kelas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $k): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($k); ?>" <?php echo e(request('kelas') === $k ? 'selected' : ''); ?>><?php echo e($k); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-12 col-sm-8 col-xl-3">
                        <label class="form-label-sm">Nama Siswa</label>
                        <select name="siswa_id" id="siswaSelect" class="form-select">
                            <option value="">Semua Siswa</option>
                            <?php $__currentLoopData = $siswas ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($s->id); ?>"
                                        data-kelas="<?php echo e($s->kelas); ?>"
                                        <?php echo e(request('siswa_id') == $s->id ? 'selected' : ''); ?>>
                                    <?php echo e($s->nama); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <?php else: ?>
                    <div class="col-12 col-xl-4">
                        <label class="form-label-sm">Nama Guru</label>
                        <select name="guru_id" class="form-select">
                            <option value="">Semua Guru</option>
                            <?php $__currentLoopData = $gurus ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $g): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($g->id); ?>" <?php echo e(request('guru_id') == $g->id ? 'selected' : ''); ?>>
                                    <?php echo e($g->nama); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <?php endif; ?>

                    <div class="col-12 col-sm-4 col-xl-2">
                        <label class="form-label-sm">Kegiatan</label>
                        <select name="kategori" class="form-select">
                            <option value="">Semua</option>
                            <?php $__currentLoopData = $daftar_kategori ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $kat): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($kat); ?>" <?php echo e(request('kategori') === $kat ? 'selected' : ''); ?>><?php echo e($kat); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    <div class="col-12 col-sm-4 col-xl-2">
                        <label class="form-label-sm">Periode</label>
                        
                        <input type="month" name="periode" class="form-control"
                               value="<?php echo e($tahun); ?>-<?php echo e(sprintf('%02d', $bulan)); ?>">
                    </div>

                    
                    <div class="col-12 col-sm-4 col-xl-2">
                        <label class="form-label-sm">Minggu</label>
                        <select name="minggu" class="form-select" <?php echo e($mingguMaksimal < 1 ? 'disabled' : ''); ?>>
                            <option value="" <?php echo e(!$minggu ? 'selected' : ''); ?>>Full Sebulan</option>
                            <?php if($mingguMaksimal < 1): ?>
                                <option value="" disabled>Belum ada minggu berjalan</option>
                            <?php else: ?>
                                <?php $__currentLoopData = range(1, $mingguMaksimal); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $w): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($w); ?>" <?php echo e((int) $minggu === $w ? 'selected' : ''); ?>>
                                        Minggu ke-<?php echo e($w); ?><?php echo e($w == $mingguMaksimal ? ' (berjalan)' : ''); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endif; ?>
                        </select>
                    </div>

                    <div class="col-12 col-xl-1 mt-3 mt-xl-0">
                        <button type="submit" class="btn-filter w-100">
                            <i class="bi bi-search"></i>Cari
                        </button>
                    </div>

                </div>
            </form>
        </div>

        
        <div class="flag-bar">
            <div class="flag-legend">
                <span class="badge-flag"><i class="bi bi-exclamation-triangle-fill"></i> Perlu Perhatian</span>
                <span style="font-size:12px; line-height: 1.5;">
                    = Alpa ≥ <?php echo e($ambangAlpaMingguan); ?> kali dalam satu minggu <strong>yang sudah selesai</strong>
                    (minggu yang masih berjalan tidak dihitung).
                    <em>Berulang</em> = terjadi >1 minggu bulan ini.
                    <span class="badge-eskalasi d-inline-flex mt-1 mt-md-0" style="margin-left:4px;">Perlu Tindak Lanjut</span>
                    = sudah pernah ditandai dievaluasi tapi tetap terulang lagi.
                </span>
            </div>
            <div class="form-check form-switch mb-0 mt-2 mt-md-0">
                <input class="form-check-input" type="checkbox" id="togglePerluPerhatian" role="switch">
                <label class="form-check-label" for="togglePerluPerhatian" style="font-size:12px;cursor:pointer;">
                    Perlu perhatian saja
                </label>
            </div>
        </div>

        
        <div class="table-responsive">
            <table class="table-report" id="tabelRekap">
                <thead>
                    <tr>
                        <th style="width:4%;padding-left:20px;">No</th>
                        <th style="width:<?php echo e($type === 'siswa' ? '22%' : '28%'); ?>">Nama</th>
                        <th style="width:18%">Identitas</th>
                        <?php if($type === 'siswa'): ?>
                        <th style="width:10%">Kelas</th>
                        <?php endif; ?>
                        <th style="width:10%">Hadir</th>
                        <th style="width:10%">Izin/Sakit</th>
                        <th style="width:10%">Alpa</th>
                        <th style="width:12%;text-align:center;">Persentase</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $data_rekap ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <?php
                        $flag = ($d['minggu_kena_flag'] ?? 0) > 0;
                        $eskalasi = $d['perlu_tindak_lanjut'] ?? false;
                    ?>
                    <tr data-flag="<?php echo e($flag ? 1 : 0); ?>" class="<?php echo e($eskalasi ? 'row-eskalasi' : ($flag ? 'row-flag' : '')); ?>">

                        <td data-label="No" class="text-muted fw-semibold" style="padding-left:20px;">
                            <?php echo e($i + 1); ?>

                        </td>

                        <td data-label="Nama" class="fw-semibold">
                            <?php echo e($d['nama']); ?>

                            <?php if($eskalasi): ?>
                                <span class="badge-eskalasi d-inline-flex mt-1">
                                    <i class="bi bi-exclamation-octagon-fill"></i> Perlu Tindak Lanjut
                                </span>
                            <?php elseif($flag): ?>
                                <span class="badge-flag d-inline-flex mt-1">
                                    <i class="bi bi-exclamation-triangle-fill"></i>
                                    <?php echo e(($d['minggu_kena_flag'] ?? 0) > 1 ? 'Berulang ' . $d['minggu_kena_flag'] . 'x' : 'Perlu Perhatian'); ?>

                                </span>
                            <?php endif; ?>

                            <?php if(!empty($d['minggu_dalam_pemantauan'])): ?>
                                <br>
                                <span class="badge-pemantauan d-inline-flex">
                                    <i class="bi bi-eye-fill"></i>
                                    Pemantauan (minggu ke-<?php echo e(implode(', ', $d['minggu_dalam_pemantauan'])); ?> berjalan)
                                </span>
                            <?php endif; ?>

                            
                            <?php $__currentLoopData = $d['minggu_kena_flag_list'] ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $w): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php $eval = $d['evaluasi_per_minggu'][$w] ?? ['status' => 'belum']; ?>
                                <?php if($eval['status'] === 'sudah'): ?>
                                    <br>
                                    <span class="badge-dievaluasi d-inline-flex">
                                        <i class="bi bi-check-circle-fill"></i>
                                        Mg-<?php echo e($w); ?>: dievaluasi (<?php echo e($eval['tanggal_evaluasi']); ?>)
                                    </span>
                                <?php else: ?>
                                    <br>
                                    <button type="button" class="btn-evaluasi"
                                        onclick="bukaModalEvaluasi(<?php echo e($d['item_id']); ?>, '<?php echo e($type); ?>', <?php echo e($tahun); ?>, <?php echo e((int) $bulan); ?>, <?php echo e($w); ?>, '<?php echo e(addslashes($d['nama'])); ?>')">
                                        <i class="bi bi-megaphone-fill"></i> Tandai mg-<?php echo e($w); ?> sudah dievaluasi
                                    </button>
                                <?php endif; ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </td>

                        <td data-label="Identitas">
                            <div class="d-md-none fw-semibold text-muted mb-1" style="font-size: 11px;">
                                <?php echo e($type === 'siswa' ? 'NISN' : ($d['jenis_identitas'] ?? 'Identitas')); ?>

                            </div>
                            <span class="badge-id">
                                <?php echo e($type === 'siswa' ? ($d['nisn'] ?? '-') : ($d['nomor_identitas'] ?? '-')); ?>

                            </span>
                        </td>

                        <?php if($type === 'siswa'): ?>
                        <td data-label="Kelas">
                            <span class="badge bg-light text-secondary border px-2 py-1 rounded-3" style="font-size:12px;">
                                <?php echo e($d['kelas']); ?>

                            </span>
                        </td>
                        <?php endif; ?>

                        <td data-label="Hadir">
                            <span class="stat-hadir">
                                <i class="bi bi-check-circle-fill"></i><?php echo e($d['hadir']); ?> hr
                            </span>
                        </td>

                        <td data-label="Izin/Sakit">
                            <span class="stat-izin">
                                <i class="bi bi-info-circle-fill"></i><?php echo e($d['izin_sakit'] ?? 0); ?> hr
                            </span>
                        </td>

                        <td data-label="Alpa">
                            <span class="stat-alpa">
                                <i class="bi bi-x-circle-fill"></i><?php echo e($d['alpa']); ?> hr
                            </span>
                        </td>

                        <td data-label="Persentase" style="text-align:center;">
                            <span class="badge-pct <?php echo e($d['skor'] >= 80 ? 'ok' : 'bad'); ?>">
                                <?php echo e($d['skor']); ?>%
                            </span>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr class="empty-row">
                        <td colspan="<?php echo e($type === 'siswa' ? 8 : 7); ?>">
                            <div class="empty-state">
                                <i class="bi bi-inbox"></i>
                                <p class="fw-semibold mb-1">Data tidak ditemukan</p>
                                <p class="text-muted small mb-0">Tidak ada rekap kehadiran pada periode ini.</p>
                            </div>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

    </div>
</div>


<div class="modal fade" id="modalEvaluasi" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <form method="POST" action="<?php echo e(route('admin.laporan.evaluasi')); ?>">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="tipe" id="evalTipe">
                <input type="hidden" name="siswa_id" id="evalSiswaId">
                <input type="hidden" name="guru_id" id="evalGuruId">
                <input type="hidden" name="tahun" id="evalTahun">
                <input type="hidden" name="bulan" id="evalBulan">
                <input type="hidden" name="minggu_ke" id="evalMinggu">

                <div class="modal-header border-0 bg-light pb-2">
                    <h6 class="modal-title fw-bold" id="modalEvaluasiTitle">Tandai Sudah Dievaluasi</h6>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <label class="form-label-sm text-muted">Catatan Evaluasi (Opsional)</label>
                    <textarea name="catatan" class="form-control bg-light" rows="3"
                        placeholder="Contoh: Sudah dipanggil dan diberi peringatan, orang tua sudah dihubungi, dsb."></textarea>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn-filter">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $__env->startPush('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', () => {

    // Filter kelas → hide siswa yang bukan kelasnya
    const kelasEl = document.getElementById('kelasSelect');
    const siswaEl = document.getElementById('siswaSelect');
    if (kelasEl && siswaEl) {
        kelasEl.addEventListener('change', function () {
            const kelas = this.value;
            Array.from(siswaEl.options).forEach(opt => {
                if (!opt.value) return;
                opt.hidden = kelas && opt.dataset.kelas !== kelas;
            });
            if (siswaEl.options[siswaEl.selectedIndex]?.hidden) siswaEl.value = '';
        });
        kelasEl.dispatchEvent(new Event('change'));
    }

    // Toggle tampilkan hanya baris bermasalah
    document.getElementById('togglePerluPerhatian')?.addEventListener('change', function () {
        const onlyFlag = this.checked;
        document.querySelectorAll('#tabelRekap tbody tr[data-flag]').forEach(row => {
            row.style.display = (onlyFlag && row.dataset.flag !== '1') ? 'none' : 'block';
            
            // Re-apply display table-row for desktop after toggle
            if(window.innerWidth > 768 && row.style.display === 'block') {
                row.style.display = 'table-row';
            }
        });
    });
    
    // Listen for resize to fix table row display issues when resizing window back and forth
    window.addEventListener('resize', function() {
        const toggleChecked = document.getElementById('togglePerluPerhatian')?.checked;
        document.querySelectorAll('#tabelRekap tbody tr[data-flag]').forEach(row => {
            if (toggleChecked && row.dataset.flag !== '1') {
                row.style.display = 'none';
            } else {
                row.style.display = window.innerWidth > 768 ? 'table-row' : 'block';
            }
        });
    });
});

// Isi modal evaluasi lalu tampilkan. Dipanggil dari tombol "Tandai minggu
// ke-X sudah dievaluasi" di setiap baris yang kena flag.
function bukaModalEvaluasi(itemId, tipe, tahun, bulan, minggu, nama) {
    document.getElementById('evalTipe').value = tipe;
    document.getElementById('evalSiswaId').value = tipe === 'siswa' ? itemId : '';
    document.getElementById('evalGuruId').value = tipe === 'guru' ? itemId : '';
    document.getElementById('evalTahun').value = tahun;
    document.getElementById('evalBulan').value = bulan;
    document.getElementById('evalMinggu').value = minggu;
    document.getElementById('modalEvaluasiTitle').textContent =
        'Tandai Evaluasi — ' + nama + ' (Mg-' + minggu + ')';

    const modalEl = document.getElementById('modalEvaluasi');
    const modal = bootstrap.Modal.getOrCreateInstance(modalEl);
    modal.show();
}
</script>
<?php $__env->stopPush(); ?>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\absensi_muttaqin_v2\resources\views/absensi/rekap_bulanan.blade.php ENDPATH**/ ?>