<?php
/**
 * admin/kelola_autokoding.php - Manajemen Project Autokoding
 */
session_start();
include '../config/database.php';
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../auth/login_page.php"); exit;
}

// Buat kolom jadwal jika belum ada (kompatibel semua versi MySQL)
$_cols = [];
$_cr = mysqli_query($conn, "SHOW COLUMNS FROM settings");
while ($_row = mysqli_fetch_assoc($_cr)) $_cols[] = $_row['Field'];
if (!in_array('auto_clean_days', $_cols))    @mysqli_query($conn, "ALTER TABLE settings ADD COLUMN auto_clean_days INT DEFAULT 7");
if (!in_array('auto_clean_enabled', $_cols)) @mysqli_query($conn, "ALTER TABLE settings ADD COLUMN auto_clean_enabled TINYINT(1) DEFAULT 0");
if (!in_array('auto_clean_last_run', $_cols))@mysqli_query($conn, "ALTER TABLE settings ADD COLUMN auto_clean_last_run DATETIME DEFAULT NULL");

$cfg = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM settings WHERE id = 1"));
$clean_days    = (int)($cfg['auto_clean_days'] ?? 7);
$clean_enabled = (int)($cfg['auto_clean_enabled'] ?? 0);
$clean_last    = $cfg['auto_clean_last_run'] ?? null;
$projects_path = realpath(__DIR__ . '/../autokoding/projects');

// Hitung statistik
$total_folders = 0; $total_size = 0;
if ($projects_path && is_dir($projects_path)) {
    $dirs = glob($projects_path . '/ses_*', GLOB_ONLYDIR);
    $total_folders = count($dirs ?? []);
    foreach (($dirs ?? []) as $d) {
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($d, RecursiveDirectoryIterator::SKIP_DOTS));
        foreach ($it as $f) if ($f->isFile()) $total_size += $f->getSize();
    }
}
function fmt($b){ if($b>=1048576) return round($b/1048576,2).' MB'; if($b>=1024) return round($b/1024,1).' KB'; return $b.' B'; }
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kelola Autokoding | Admin</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700&display=swap" rel="stylesheet">
<style>
:root{--sidebar-w:260px;--primary:#0d6efd;--bg-body:#f8fafc}
body{font-family:'Plus Jakarta Sans',sans-serif;background:var(--bg-body);color:#1e293b}
.sidebar{width:var(--sidebar-w);height:100vh;background:white;position:fixed;border-right:1px solid #e2e8f0;padding:24px;z-index:1000;overflow-y:auto}
.main-content{margin-left:var(--sidebar-w);padding:36px;min-height:100vh}
.nav-link{color:#64748b;padding:11px 16px;border-radius:12px;margin-bottom:6px;font-weight:600;transition:.3s;display:flex;align-items:center;text-decoration:none}
.nav-link i{font-size:1.1rem;margin-right:11px}
.nav-link:hover{background:#f1f5f9;color:var(--primary)}
.nav-link.active{background:var(--primary);color:white;box-shadow:0 4px 12px rgba(13,110,253,.2)}
.stat-card{background:white;padding:22px;border-radius:18px;border:1px solid #e2e8f0}
.icon-box{width:46px;height:46px;border-radius:12px;display:flex;align-items:center;justify-content:center;margin-bottom:14px}
.schedule-card{background:linear-gradient(135deg,#1e293b,#0f172a);color:white;border-radius:18px;padding:28px}
.mobile-header{display:none;background:white;padding:14px 20px;border-bottom:1px solid #e2e8f0;position:sticky;top:0;z-index:999;align-items:center;justify-content:space-between}
.sidebar-overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:1040}
.sidebar-overlay.show{display:block}
@media(max-width:992px){.sidebar{transform:translateX(-100%);transition:.3s;z-index:1050}.sidebar.show{transform:translateX(0)}.main-content{margin-left:0;padding:18px}.mobile-header{display:flex}}
.age-bar{height:5px;border-radius:3px;background:#e2e8f0;overflow:hidden;margin-top:4px}
.age-bar-fill{height:100%;border-radius:3px}
tr.deleting{opacity:.4;pointer-events:none;transition:.3s}
#toast-wrap{position:fixed;bottom:24px;right:24px;z-index:9999}
.toast-msg{background:#1e293b;color:white;padding:12px 20px;border-radius:12px;margin-top:8px;font-size:13px;font-weight:600;display:flex;align-items:center;gap:8px;animation:slideIn .3s ease;box-shadow:0 8px 24px rgba(0,0,0,.2)}
.toast-msg.success{border-left:4px solid #22c55e}
.toast-msg.error{border-left:4px solid #ef4444}
.toast-msg.info{border-left:4px solid #3b82f6}
@keyframes slideIn{from{transform:translateX(60px);opacity:0}to{transform:translateX(0);opacity:1}}
</style>
</head>
<body>

<div class="mobile-header">
    <div class="d-flex align-items-center">
        <div class="bg-primary p-2 rounded-3 me-3 text-white" style="width:40px;height:40px;display:flex;align-items:center;justify-content:center"><i class="bi bi-shield-lock-fill fs-5"></i></div>
        <h5 class="fw-bold m-0">Admin Panel</h5>
    </div>
    <button class="btn btn-light border-0" id="sidebarToggle"><i class="bi bi-list fs-3"></i></button>
</div>

<div class="sidebar-overlay" id="sidebarOverlay"></div>

<aside class="sidebar" id="sidebarMenu">
    <div class="d-flex align-items-center mb-5 px-2 justify-content-between">
        <div class="d-flex align-items-center">
            <div class="bg-primary p-2 rounded-3 me-3 text-white"><i class="bi bi-shield-lock-fill fs-4"></i></div>
            <h5 class="fw-bold m-0">Admin Panel</h5>
        </div>
        <button class="btn btn-light d-lg-none border-0 p-1" id="sidebarClose"><i class="bi bi-x-lg fs-5"></i></button>
    </div>
    <nav class="nav flex-column">
        <a class="nav-link" href="index.php"><i class="bi bi-grid-fill"></i> Dashboard</a>
        <a class="nav-link" href="kelola_siswa.php"><i class="bi bi-people-fill"></i> Kelola Siswa</a>
        <a class="nav-link" href="kelola_live.php"><i class="bi bi-person-bounding-box"></i> Kelola Live</a>
        <a class="nav-link" href="../llm/admin/dashboard_llm.php"><i class="bi bi-cpu-fill"></i> LLM Dashboard</a>
        <a class="nav-link" href="konfigurasi.php"><i class="bi bi-gear-fill"></i> Pengaturan</a>
        <a class="nav-link" href="statistik.php"><i class="bi bi-bar-chart-fill"></i> Statistik</a>
        <a class="nav-link" href="setting_akun.php"><i class="bi bi-person-badge-fill"></i> Setting Akun</a>
        <a class="nav-link active" href="kelola_autokoding.php"><i class="bi bi-folder-x"></i> Kelola Autokoding</a>
        <hr class="my-3 text-muted">
        <a class="nav-link text-danger" href="../auth/logout.php"><i class="bi bi-box-arrow-left"></i> Logout</a>
    </nav>
</aside>

<main class="main-content">
    <div class="d-flex align-items-center justify-content-between mb-4 mt-4 mt-lg-0">
        <div>
            <h3 class="fw-bold mb-1">Kelola Project Autokoding</h3>
            <p class="text-muted mb-0">Manajemen folder project hasil generate AI di <code>autokoding/projects/</code></p>
        </div>
        <button class="btn btn-primary rounded-pill px-4 fw-bold shadow-sm" onclick="loadProjects()">
            <i class="bi bi-arrow-clockwise me-2"></i>Refresh
        </button>
    </div>

    <!-- STATISTIK -->
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="icon-box bg-primary-subtle text-primary"><i class="bi bi-folder2-open fs-4"></i></div>
                <div class="text-muted small fw-bold">TOTAL FOLDER</div>
                <h2 class="fw-bold mt-1" id="stat-total"><?= $total_folders ?></h2>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="icon-box bg-warning-subtle text-warning"><i class="bi bi-hdd fs-4"></i></div>
                <div class="text-muted small fw-bold">TOTAL UKURAN</div>
                <h2 class="fw-bold mt-1" id="stat-size"><?= fmt($total_size) ?></h2>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="icon-box bg-danger-subtle text-danger"><i class="bi bi-exclamation-triangle fs-4"></i></div>
                <div class="text-muted small fw-bold">FOLDER LAMA (>7hr)</div>
                <h2 class="fw-bold mt-1" id="stat-old">—</h2>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="icon-box bg-success-subtle text-success"><i class="bi bi-clock-history fs-4"></i></div>
                <div class="text-muted small fw-bold">AUTO-CLEAN</div>
                <h2 class="fw-bold mt-1" style="font-size:1.1rem"><?= $clean_enabled ? '<span id="stat-clean-badge" class="badge bg-success">Aktif</span>' : '<span id="stat-clean-badge" class="badge bg-secondary">Nonaktif</span>' ?></h2>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <!-- TABEL PROJECT -->
        <div class="col-lg-8">
            <div class="card border-0 rounded-4 shadow-sm">
                <div class="card-header bg-white border-0 rounded-4 pt-4 px-4 pb-0">
                    <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
                        <h5 class="fw-bold mb-0"><i class="bi bi-folder2 text-primary me-2"></i>Daftar Project</h5>
                        <div class="d-flex gap-2 flex-wrap">
                            <div class="input-group input-group-sm" style="width:160px">
                                <span class="input-group-text bg-light border-0">Hapus ></span>
                                <input type="number" id="filter-days" class="form-control border-0 bg-light" value="7" min="1" max="365" style="width:55px">
                                <span class="input-group-text bg-light border-0">hari</span>
                            </div>
                            <button class="btn btn-sm btn-outline-warning rounded-pill fw-bold px-3" onclick="deleteOld()">
                                <i class="bi bi-calendar-x me-1"></i>Hapus Lama
                            </button>
                            <button class="btn btn-sm btn-danger rounded-pill fw-bold px-3" onclick="deleteAll()">
                                <i class="bi bi-trash3 me-1"></i>Hapus Semua
                            </button>
                        </div>
                    </div>
                </div>
                <div class="card-body px-4">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th style="min-width:180px">Nama Sesi</th>
                                    <th>Dibuat</th>
                                    <th class="text-center">File</th>
                                    <th>Ukuran</th>
                                    <th>Umur</th>
                                    <th class="text-end">Aksi</th>
                                </tr>
                            </thead>
                            <tbody id="project-table-body">
                                <tr><td colspan="6" class="text-center text-muted py-5">
                                    <div class="spinner-border spinner-border-sm text-primary me-2"></div> Memuat data...
                                </td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- PANEL JADWAL -->
        <div class="col-lg-4">
            <div class="schedule-card mb-4">
                <div class="d-flex align-items-center gap-3 mb-4">
                    <div class="bg-white bg-opacity-10 rounded-3 d-flex align-items-center justify-content-center" style="width:46px;height:46px">
                        <i class="bi bi-robot text-white fs-4"></i>
                    </div>
                    <div>
                        <h5 class="fw-bold mb-0">Auto-Clean</h5>
                        <small class="text-white text-opacity-60">Jadwal hapus otomatis</small>
                    </div>
                </div>

                <div class="mb-3">
                    <label class="text-white text-opacity-75 small fw-bold mb-2 d-block">HAPUS FOLDER LEBIH TUA DARI</label>
                    <div class="d-flex gap-2 flex-wrap">
                        <?php foreach([1,3,7,14,30] as $d): ?>
                        <button class="btn btn-sm rounded-pill fw-bold day-btn <?= $clean_days==$d?'btn-primary':'btn-outline-light' ?>" data-days="<?= $d ?>"><?= $d ?> hari</button>
                        <?php endforeach; ?>
                    </div>
                    <input type="hidden" id="schedule-days" value="<?= $clean_days ?>">
                </div>

                <div class="mb-4">
                    <label class="text-white text-opacity-75 small fw-bold mb-2 d-block">STATUS JADWAL</label>
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" id="schedule-toggle" <?= $clean_enabled?'checked':'' ?> style="width:2.5em;height:1.3em">
                        <label class="form-check-label text-white fw-bold ms-2" for="schedule-toggle">
                            <?= $clean_enabled ? 'Aktif — 1x per hari' : 'Nonaktif' ?>
                        </label>
                    </div>
                </div>

                <?php if($clean_last): ?>
                <div class="mb-4 p-3 rounded-3 bg-white bg-opacity-10">
                    <div class="small text-white text-opacity-60 mb-1">TERAKHIR DIJALANKAN</div>
                    <div class="fw-bold text-white" style="font-size:13px"><?= date('d M Y, H:i', strtotime($clean_last)) ?></div>
                </div>
                <?php endif; ?>

                <div class="d-grid gap-2">
                    <button class="btn btn-primary rounded-pill fw-bold py-2" onclick="saveSchedule()">
                        <i class="bi bi-save me-2"></i>Simpan Jadwal
                    </button>
                    <button class="btn btn-outline-light rounded-pill fw-bold py-2" onclick="runSchedule()">
                        <i class="bi bi-play-circle me-2"></i>Jalankan Sekarang
                    </button>
                </div>
            </div>

            <!-- LEGENDA -->
            <div class="card border-0 rounded-4 shadow-sm p-4">
                <h6 class="fw-bold mb-3"><i class="bi bi-info-circle text-primary me-2"></i>Keterangan Warna</h6>
                <div class="d-flex align-items-center gap-3 mb-2">
                    <span class="badge bg-success px-3 py-2">Baru</span>
                    <small class="text-muted">Dibuat < 3 hari lalu</small>
                </div>
                <div class="d-flex align-items-center gap-3 mb-2">
                    <span class="badge bg-warning text-dark px-3 py-2">Sedang</span>
                    <small class="text-muted">Dibuat 3–7 hari lalu</small>
                </div>
                <div class="d-flex align-items-center gap-3">
                    <span class="badge bg-danger px-3 py-2">Lama</span>
                    <small class="text-muted">Dibuat > 7 hari lalu</small>
                </div>
            </div>
        </div>
    </div>
</main>

<!-- TOAST -->
<div id="toast-wrap"></div>

<!-- MODAL KONFIRMASI UNIVERSAL -->
<div class="modal fade" id="modalKonfirmasi" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" style="max-width:420px">
    <div class="modal-content border-0 shadow-lg rounded-4 overflow-hidden">
      <div class="modal-header border-0 px-4 pt-4 pb-0">
        <div class="d-flex align-items-center gap-3">
          <div id="modal-icon-wrap" class="rounded-3 d-flex align-items-center justify-content-center" style="width:48px;height:48px;background:#fef2f2">
            <i id="modal-icon" class="bi bi-trash3-fill text-danger fs-4"></i>
          </div>
          <div>
            <h5 class="fw-bold mb-0" id="modal-title">Konfirmasi Hapus</h5>
            <small class="text-muted">Tindakan ini tidak bisa dibatalkan</small>
          </div>
        </div>
      </div>
      <div class="modal-body px-4 py-3">
        <p class="text-secondary mb-0" id="modal-body-text" style="font-size:14px;line-height:1.7"></p>
      </div>
      <div class="modal-footer border-0 px-4 pb-4 gap-2">
        <button type="button" class="btn btn-light rounded-pill px-4 fw-bold" data-bs-dismiss="modal">Batal</button>
        <button type="button" class="btn btn-danger rounded-pill px-4 fw-bold" id="modal-confirm-btn">Ya, Hapus</button>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
const API = 'api_clean_projects.php';
let _modalCallback = null;
let _bsModal = null;

function toast(msg, type='info') {
    const icons = {success:'bi-check-circle-fill',error:'bi-x-circle-fill',info:'bi-info-circle-fill'};
    const el = document.createElement('div');
    el.className = `toast-msg ${type}`;
    el.innerHTML = `<i class="bi ${icons[type]||icons.info}"></i>${msg}`;
    document.getElementById('toast-wrap').appendChild(el);
    setTimeout(() => el.remove(), 4000);
}

function konfirmasi(title, body, iconClass, onConfirm) {
    document.getElementById('modal-title').textContent      = title;
    document.getElementById('modal-body-text').innerHTML    = body;
    document.getElementById('modal-icon').className         = 'bi ' + iconClass + ' fs-4';
    _modalCallback = onConfirm;
    if (!_bsModal) _bsModal = new bootstrap.Modal(document.getElementById('modalKonfirmasi'));
    _bsModal.show();
}

document.addEventListener('DOMContentLoaded', () => {
    document.getElementById('modal-confirm-btn').addEventListener('click', () => {
        _bsModal.hide();
        if (typeof _modalCallback === 'function') _modalCallback();
    });
});

function post(data) {
    const fd = new FormData();
    Object.keys(data).forEach(k => fd.append(k, data[k]));
    return fetch(API, {method:'POST',body:fd}).then(r=>r.json());
}

async function loadProjects() {
    const tbody = document.getElementById('project-table-body');
    tbody.innerHTML = '<tr><td colspan="6" class="text-center text-muted py-4"><div class="spinner-border spinner-border-sm text-primary me-2"></div> Memuat...</td></tr>';
    const res = await post({action:'list'});
    if (!res.success) { tbody.innerHTML='<tr><td colspan="6" class="text-center text-danger py-4">Gagal memuat data.</td></tr>'; return; }

    document.getElementById('stat-total').textContent = res.total;
    document.getElementById('stat-size').textContent  = res.total_size;
    const old = res.projects.filter(p => p.age_days >= 7).length;
    document.getElementById('stat-old').textContent   = old;

    if (res.projects.length === 0) {
        tbody.innerHTML = '<tr><td colspan="6" class="text-center text-muted py-5 fst-italic"><i class="bi bi-folder-check me-2 text-success fs-5"></i>Folder projects bersih, tidak ada sesi tersimpan.</td></tr>';
        return;
    }

    const badgeMap = {success:'bg-success', warning:'bg-warning text-dark', danger:'bg-danger'};
    const labelMap = {success:'Baru', warning:'Sedang', danger:'Lama'};
    const maxAge   = Math.max(...res.projects.map(p=>p.age_days), 1);
    const colorMap = {success:'#22c55e', warning:'#f59e0b', danger:'#ef4444'};

    tbody.innerHTML = res.projects.map(p => `
        <tr id="row-${p.name}">
            <td>
                <div class="fw-bold small text-break" style="max-width:180px">${p.name}</div>
                <span class="badge ${badgeMap[p.badge]||'bg-secondary'} mt-1">${labelMap[p.badge]||''}</span>
            </td>
            <td class="text-muted small">${p.created}</td>
            <td class="text-center"><span class="badge bg-light text-dark border">${p.files}</span></td>
            <td class="text-muted small">${p.size}</td>
            <td>
                <span class="fw-bold small">${p.age_days} hr</span>
                <div class="age-bar" style="width:80px"><div class="age-bar-fill" style="width:${Math.min(100,p.age_days/maxAge*100)}%;background:${colorMap[p.badge]||'#94a3b8'}"></div></div>
            </td>
            <td class="text-end">
                <button class="btn btn-sm btn-danger rounded-pill px-3 fw-bold shadow-sm" onclick="deleteOne('${p.name}')">
                    <i class="bi bi-trash3 me-1"></i>Hapus
                </button>
            </td>
        </tr>
    `).join('');
}

function deleteOne(name) {
    konfirmasi(
        'Hapus Folder Project',
        `Yakin ingin menghapus folder:<br><code class="bg-light px-2 py-1 rounded">${name}</code><br><br>Semua file di dalamnya akan <strong class="text-danger">terhapus permanen</strong>.`,
        'bi-trash3-fill text-danger',
        async () => {
            const row = document.getElementById('row-'+name);
            if (row) row.classList.add('deleting');
            const res = await post({action:'delete_one', folder:name});
            toast(res.message, res.success?'success':'error');
            if (res.success) setTimeout(loadProjects, 600);
            else if (row) row.classList.remove('deleting');
        }
    );
}

function deleteAll() {
    const n = document.getElementById('stat-total').textContent;
    konfirmasi(
        'Hapus SEMUA Project',
        `Anda akan menghapus <strong class="text-danger">${n} folder</strong> sekaligus.<br><br>Semua file kode hasil generate AI akan hilang permanen dan tidak bisa dikembalikan.`,
        'bi-exclamation-triangle-fill text-danger',
        async () => {
            toast('Menghapus semua folder...', 'info');
            const res = await post({action:'delete_all'});
            toast(res.message, res.success?'success':'error');
            if (res.success) setTimeout(loadProjects, 800);
        }
    );
}

function deleteOld() {
    const days = document.getElementById('filter-days').value;
    konfirmasi(
        `Hapus Folder Lebih Tua ${days} Hari`,
        `Semua folder project yang berusia lebih dari <strong>${days} hari</strong> akan dihapus permanen.<br><br>Folder yang lebih baru akan tetap aman.`,
        'bi-calendar-x-fill text-warning',
        async () => {
            toast('Memproses...', 'info');
            const res = await post({action:'delete_old', days});
            toast(res.message, res.success?'success':'error');
            if (res.success) setTimeout(loadProjects, 800);
        }
    );
}

async function saveSchedule() {
    const days    = document.getElementById('schedule-days').value;
    const enabled = document.getElementById('schedule-toggle').checked ? 1 : 0;
    const res = await post({action:'save_schedule', days, enabled});
    toast(res.message, res.success?'success':'error');
    if (res.success) {
        // Update label toggle
        document.querySelector('label[for="schedule-toggle"]').textContent = res.enabled ? 'Aktif — 1x per hari' : 'Nonaktif';
        // Update badge stat-card AUTO-CLEAN
        const statClean = document.querySelector('#stat-clean-badge');
        if (statClean) {
            statClean.className = res.enabled ? 'badge bg-success' : 'badge bg-secondary';
            statClean.textContent = res.enabled ? 'Aktif' : 'Nonaktif';
        }
        loadProjects();
    }
}

function runSchedule() {
    konfirmasi(
        'Jalankan Auto-Clean Sekarang',
        'Folder project yang melewati batas hari yang ditentukan akan segera dihapus sekarang.',
        'bi-play-circle-fill text-primary',
        async () => {
            toast('Menjalankan auto-clean...', 'info');
            const res = await post({action:'run_schedule'});
            toast(res.message, res.success?'success':'error');
            if (res.success) setTimeout(loadProjects, 800);
        }
    );
}

// Tombol pilihan hari
document.querySelectorAll('.day-btn').forEach(btn => {
    btn.addEventListener('click', function() {
        document.querySelectorAll('.day-btn').forEach(b => b.classList.replace('btn-primary','btn-outline-light'));
        this.classList.replace('btn-outline-light','btn-primary');
        document.getElementById('schedule-days').value = this.dataset.days;
    });
});

// Toggle label
document.getElementById('schedule-toggle').addEventListener('change', function() {
    document.querySelector('label[for="schedule-toggle"]').textContent = this.checked ? 'Aktif — 1x per hari' : 'Nonaktif';
});

// Sidebar mobile
const sidebar = document.getElementById('sidebarMenu');
const overlay = document.getElementById('sidebarOverlay');
function toggleSB() { sidebar.classList.toggle('show'); overlay.classList.toggle('show'); }
document.getElementById('sidebarToggle')?.addEventListener('click', toggleSB);
document.getElementById('sidebarClose')?.addEventListener('click', toggleSB);
overlay.addEventListener('click', toggleSB);

// Auto-check jadwal
post({action:'auto_check'}).then(r => { if(r.triggered) toast('Auto-clean berjalan: '+r.message,'info'); });

// Load tabel saat halaman dibuka
loadProjects();
</script>
</body>
</html>
