<?php
$judul = "Dashboard Direktur";
$menu  = "dashboard";

include 'layout/header.php';

/* ==========================
   QUERY DASHBOARD
========================== */

$total_user = mysqli_num_rows(mysqli_query($conn,
    "SELECT * FROM users"
));

$total_checklist = mysqli_num_rows(mysqli_query($conn,
    "SELECT * FROM checklist_master"
));

$total_mod = mysqli_num_rows(mysqli_query($conn,
    "SELECT mh.id_petugas
     FROM mod_harian mh
     JOIN users u ON mh.id_petugas = u.id_user
     WHERE mh.tanggal = CURDATE()"
));

$q_mod_names = mysqli_query($conn,
    "SELECT u.nama, mh.shift
     FROM mod_harian mh
     JOIN users u ON mh.id_petugas = u.id_user
     WHERE mh.tanggal = CURDATE()
     ORDER BY mh.shift ASC"
);
$mod_names = mysqli_fetch_all($q_mod_names, MYSQLI_ASSOC);

$total_urgent = mysqli_num_rows(mysqli_query($conn,
    "SELECT up.id
     FROM urgent_penerima up
     WHERE up.status != 'Selesai'"
));

/* ══════════════════════════════════════
   ── DAILY (jenis = 'daily') ──
══════════════════════════════════════ */
$total_harian = mysqli_num_rows(mysqli_query($conn,"
    SELECT id_checklist FROM checklist_master
    WHERE LOWER(jenis) = 'daily' AND status = 'aktif'
"));

$q_selesai = mysqli_num_rows(mysqli_query($conn,"
    SELECT DISTINCT ch.id_checklist
    FROM checklist_harian ch
    JOIN checklist_master cm ON ch.id_checklist = cm.id_checklist
    WHERE ch.tanggal      = CURDATE()
    AND   ch.status       = 'Selesai'
    AND   cm.status       = 'aktif'
    AND   LOWER(cm.jenis) = 'daily'
"));

$q_belum = mysqli_num_rows(mysqli_query($conn,"
    SELECT cm.id_checklist
    FROM checklist_master cm
    WHERE LOWER(cm.jenis) = 'daily'
    AND   cm.status = 'aktif'
    AND   cm.id_checklist NOT IN (
        SELECT id_checklist FROM checklist_harian
        WHERE tanggal = CURDATE() AND status = 'Selesai'
    )
"));

$progress = ($total_harian > 0)
    ? round(($q_selesai / $total_harian) * 100) : 0;

/* ══════════════════════════════════════
   ── WEEKLY (jenis = 'weekly') ──
══════════════════════════════════════ */
$week_start = date('Y-m-d', strtotime('monday this week'));
$week_end   = date('Y-m-d');

$total_weekly = mysqli_num_rows(mysqli_query($conn,"
    SELECT id_checklist FROM checklist_master
    WHERE LOWER(jenis) = 'weekly' AND status = 'aktif'
"));

$week_selesai = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
    SELECT COUNT(DISTINCT CONCAT(ch.id_checklist,'-',ch.tanggal)) AS n
    FROM checklist_harian ch
    JOIN checklist_master cm ON ch.id_checklist = cm.id_checklist
    WHERE ch.tanggal BETWEEN '$week_start' AND '$week_end'
    AND   ch.status       = 'Selesai'
    AND   cm.status       = 'aktif'
    AND   LOWER(cm.jenis) = 'weekly'
"))['n'] ?? 0);

$week_belum = max(0, $total_weekly - $week_selesai);
$week_pct   = $total_weekly > 0 ? round(($week_selesai / $total_weekly) * 100) : 0;

/* ══════════════════════════════════════
   ── MONTHLY (jenis = 'monthly') ──
══════════════════════════════════════ */
$month_start = date('Y-m-01');
$month_end   = date('Y-m-d');

$total_monthly = mysqli_num_rows(mysqli_query($conn,"
    SELECT id_checklist FROM checklist_master
    WHERE LOWER(jenis) = 'monthly' AND status = 'aktif'
"));

$month_selesai = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
    SELECT COUNT(DISTINCT CONCAT(ch.id_checklist,'-',ch.tanggal)) AS n
    FROM checklist_harian ch
    JOIN checklist_master cm ON ch.id_checklist = cm.id_checklist
    WHERE ch.tanggal BETWEEN '$month_start' AND '$month_end'
    AND   ch.status       = 'Selesai'
    AND   cm.status       = 'aktif'
    AND   LOWER(cm.jenis) = 'monthly'
"))['n'] ?? 0);

$month_belum = max(0, $total_monthly - $month_selesai);
$month_pct   = $total_monthly > 0 ? round(($month_selesai / $total_monthly) * 100) : 0;

/* ══════════════════════════════════════
   ── PROGRESS PER ROLE ──
   Gabungkan role dari users + jabatan
   dari checklist_master (support CSV)
══════════════════════════════════════ */

$q_roles = mysqli_query($conn, "
    SELECT DISTINCT TRIM(j.jabatan) AS role
    FROM (
        SELECT role AS jabatan FROM users
        WHERE role IS NOT NULL AND role != ''
        UNION
        SELECT TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(cm.jabatan, ',', n.n), ',', -1)) AS jabatan
        FROM checklist_master cm
        JOIN (
            SELECT 1 n UNION SELECT 2 UNION SELECT 3
            UNION SELECT 4 UNION SELECT 5
        ) n ON CHAR_LENGTH(cm.jabatan)
             - CHAR_LENGTH(REPLACE(cm.jabatan, ',', '')) >= n.n - 1
        WHERE cm.jabatan IS NOT NULL AND cm.jabatan != ''
        AND   cm.status  = 'aktif'
    ) j
    WHERE TRIM(j.jabatan) != ''
    ORDER BY role ASC
");
$roles_list = mysqli_fetch_all($q_roles, MYSQLI_ASSOC);

$role_progress = [];

foreach ($roles_list as $r) {
    $role     = trim($r['role']);
    $role_esc = mysqli_real_escape_string($conn, $role);

    /* ── DAILY per role ── */
    $rd_total = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT COUNT(DISTINCT cm.id_checklist) AS n
        FROM checklist_master cm
        WHERE LOWER(cm.jenis) = 'daily'
        AND   cm.status = 'aktif'
        AND   FIND_IN_SET('$role_esc', REPLACE(REPLACE(cm.jabatan, ' ', ''), ', ', ','))
    "))['n'] ?? 0);

    $rd_selesai = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT COUNT(DISTINCT ch.id_checklist) AS n
        FROM checklist_harian ch
        JOIN checklist_master cm ON ch.id_checklist = cm.id_checklist
        WHERE ch.tanggal      = CURDATE()
        AND   ch.status       = 'Selesai'
        AND   cm.status       = 'aktif'
        AND   LOWER(cm.jenis) = 'daily'
        AND   FIND_IN_SET('$role_esc', REPLACE(REPLACE(cm.jabatan, ' ', ''), ', ', ','))
    "))['n'] ?? 0);

    $rd_belum = max(0, $rd_total - $rd_selesai);
    $rd_pct   = $rd_total > 0 ? round(($rd_selesai / $rd_total) * 100) : 0;

    /* ── WEEKLY per role ── */
    $rw_total = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT COUNT(DISTINCT cm.id_checklist) AS n
        FROM checklist_master cm
        WHERE LOWER(cm.jenis) = 'weekly'
        AND   cm.status = 'aktif'
        AND   FIND_IN_SET('$role_esc', REPLACE(REPLACE(cm.jabatan, ' ', ''), ', ', ','))
    "))['n'] ?? 0);

    $rw_selesai = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT COUNT(DISTINCT CONCAT(ch.id_checklist,'-',ch.tanggal)) AS n
        FROM checklist_harian ch
        JOIN checklist_master cm ON ch.id_checklist = cm.id_checklist
        WHERE ch.tanggal BETWEEN '$week_start' AND '$week_end'
        AND   ch.status       = 'Selesai'
        AND   cm.status       = 'aktif'
        AND   LOWER(cm.jenis) = 'weekly'
        AND   FIND_IN_SET('$role_esc', REPLACE(REPLACE(cm.jabatan, ' ', ''), ', ', ','))
    "))['n'] ?? 0);

    $rw_belum = max(0, $rw_total - $rw_selesai);
    $rw_pct   = $rw_total > 0 ? round(($rw_selesai / $rw_total) * 100) : 0;

    /* ── MONTHLY per role ── */
    $rm_total = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT COUNT(DISTINCT cm.id_checklist) AS n
        FROM checklist_master cm
        WHERE LOWER(cm.jenis) = 'monthly'
        AND   cm.status = 'aktif'
        AND   FIND_IN_SET('$role_esc', REPLACE(REPLACE(cm.jabatan, ' ', ''), ', ', ','))
    "))['n'] ?? 0);

    $rm_selesai = (int)(mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT COUNT(DISTINCT CONCAT(ch.id_checklist,'-',ch.tanggal)) AS n
        FROM checklist_harian ch
        JOIN checklist_master cm ON ch.id_checklist = cm.id_checklist
        WHERE ch.tanggal BETWEEN '$month_start' AND '$month_end'
        AND   ch.status       = 'Selesai'
        AND   cm.status       = 'aktif'
        AND   LOWER(cm.jenis) = 'monthly'
        AND   FIND_IN_SET('$role_esc', REPLACE(REPLACE(cm.jabatan, ' ', ''), ', ', ','))
    "))['n'] ?? 0);

    $rm_belum = max(0, $rm_total - $rm_selesai);
    $rm_pct   = $rm_total > 0 ? round(($rm_selesai / $rm_total) * 100) : 0;

    // Hanya tampilkan role yang punya minimal 1 checklist
    if ($rd_total > 0 || $rw_total > 0 || $rm_total > 0) {
        $role_progress[] = [
            'role'    => $role,
            'daily'   => ['total'=>$rd_total,'selesai'=>$rd_selesai,'belum'=>$rd_belum,'pct'=>$rd_pct],
            'weekly'  => ['total'=>$rw_total,'selesai'=>$rw_selesai,'belum'=>$rw_belum,'pct'=>$rw_pct],
            'monthly' => ['total'=>$rm_total,'selesai'=>$rm_selesai,'belum'=>$rm_belum,'pct'=>$rm_pct],
        ];
    }
}

include 'layout/sidebar.php';
?>

<style>
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&display=swap');

.dash-wrapper {
    padding: 24px 24px 48px;
    max-width: 1100px;
    margin: 0 auto;
}

.dash-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 26px;
    padding-bottom: 18px;
    border-bottom: 2px solid var(--green-100);
    flex-wrap: wrap;
    gap: 10px;
}

.dash-title {
    font-size: 1.35rem;
    font-weight: 800;
    color: var(--text-dark);
    letter-spacing: -0.02em;
    margin: 0;
}

.dash-title span { color: var(--green-600); }

.dash-date {
    font-size: 0.7rem;
    font-family: 'Space Mono', monospace;
    color: var(--text-muted);
    background: var(--green-50);
    border: 1.5px solid var(--green-100);
    padding: 5px 12px;
    border-radius: 20px;
}

.section-label {
    font-size: 0.67rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.section-label::after {
    content: '';
    flex: 1;
    height: 1.5px;
    background: var(--green-100);
}

/* ── Stat Grid ── */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 14px;
    margin-bottom: 24px;
}

.stat-grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    margin-bottom: 24px;
}

@media (max-width: 900px) {
    .stat-grid   { grid-template-columns: repeat(2, 1fr); }
    .stat-grid-3 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 480px) {
    .stat-grid   { grid-template-columns: 1fr; }
    .stat-grid-3 { grid-template-columns: 1fr; }
    .dash-wrapper { padding: 16px 16px 40px; }
}

/* ── Stat Card ── */
.stat-card {
    background: #ffffff;
    border: 1.5px solid var(--green-100);
    border-radius: 14px;
    padding: 18px 16px 14px;
    position: relative;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
    animation: fadeUp 0.4s ease both;
}

.stat-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--green-600), var(--green-500));
    opacity: 0;
    transition: opacity 0.2s;
}

.stat-card:hover {
    transform: translateY(-3px);
    border-color: var(--green-200);
    box-shadow: 0 8px 24px rgba(34,197,94,0.12);
}

.stat-card:hover::before { opacity: 1; }

.stat-card .orb {
    position: absolute;
    bottom: -20px; right: -20px;
    width: 70px; height: 70px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(34,197,94,0.08) 0%, transparent 70%);
    pointer-events: none;
}

.stat-card .card-icon {
    width: 36px; height: 36px;
    border-radius: 9px;
    background: var(--green-50);
    border: 1.5px solid var(--green-100);
    display: flex; align-items: center; justify-content: center;
    font-size: 16px;
    margin-bottom: 14px;
    transition: background 0.2s;
}

.stat-card:hover .card-icon { background: var(--green-100); }

.stat-card .card-value {
    font-size: 2.1rem;
    font-weight: 800;
    line-height: 1;
    color: var(--text-dark);
    letter-spacing: -0.04em;
    margin-bottom: 5px;
    font-family: 'Space Mono', monospace;
}

.stat-card .card-label {
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--text-muted);
}

.stat-card.success {
    border-color: #bbf7d0;
    background: linear-gradient(135deg, #ffffff 55%, #f0fdf4);
}
.stat-card.success .card-value { color: var(--green-600); }
.stat-card.success .card-icon  { background: #dcfce7; border-color: #bbf7d0; }
.stat-card.success::before      { background: linear-gradient(90deg, #16a34a, #4ade80); }
.stat-card.success:hover         { box-shadow: 0 8px 24px rgba(34,197,94,0.15); }

.stat-card.danger {
    border-color: #fecaca;
    background: linear-gradient(135deg, #ffffff 55%, #fff5f5);
}
.stat-card.danger .card-value { color: #dc2626; }
.stat-card.danger .card-icon  { background: #fef2f2; border-color: #fecaca; }
.stat-card.danger::before      { background: linear-gradient(90deg, #dc2626, #f87171); }
.stat-card.danger:hover         { box-shadow: 0 8px 24px rgba(239,68,68,0.12); border-color: #fca5a5; }

.stat-card.urgent {
    border-color: #fca5a5;
    background: linear-gradient(135deg, #ffffff 55%, #fff5f5);
    animation: fadeUp 0.4s ease both, urgentPulse 2.4s ease-in-out infinite;
}
.stat-card.urgent .card-value { color: #dc2626; }
.stat-card.urgent .card-icon  { background: #fef2f2; border-color: #fecaca; }
.stat-card.urgent::before      { background: linear-gradient(90deg, #dc2626, #f87171); opacity: 1; }
.stat-card.urgent:hover         { box-shadow: 0 8px 24px rgba(239,68,68,0.2); border-color: #f87171; }

@keyframes urgentPulse {
    0%, 100% { box-shadow: 0 2px 8px rgba(0,0,0,0.04); }
    50%       { box-shadow: 0 0 20px 4px rgba(239,68,68,0.18); }
}

.stat-card.mod {
    border-color: #bae6fd;
    background: linear-gradient(135deg, #ffffff 55%, #f0f9ff);
}
.stat-card.mod .card-value { color: #0369a1; }
.stat-card.mod .card-icon  { background: #e0f2fe; border-color: #bae6fd; }
.stat-card.mod::before      { background: linear-gradient(90deg, #0369a1, #38bdf8); }
.stat-card.mod:hover         { box-shadow: 0 8px 24px rgba(14,165,233,0.15); border-color: #7dd3fc; }

.mod-names-list {
    margin-top: 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}

.mod-name-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.68rem;
    font-weight: 600;
    background: #e0f2fe;
    color: #0369a1;
    border: 1px solid #bae6fd;
    border-radius: 999px;
    padding: 2px 8px;
    white-space: nowrap;
}

.mod-name-chip.pagi   { background: #fef9c3; color: #92400e; border-color: #fde68a; }
.mod-name-chip.siang  { background: #fff7ed; color: #c2410c; border-color: #fed7aa; }
.mod-name-chip.sore   { background: #ede9fe; color: #6d28d9; border-color: #ddd6fe; }
.mod-name-chip.malam  { background: #1e1b4b; color: #c7d2fe; border-color: #3730a3; }

.stat-grid   .stat-card:nth-child(1) { animation-delay: 0.05s; }
.stat-grid   .stat-card:nth-child(2) { animation-delay: 0.10s; }
.stat-grid   .stat-card:nth-child(3) { animation-delay: 0.15s; }
.stat-grid   .stat-card:nth-child(4) { animation-delay: 0.20s; }
.stat-grid-3 .stat-card:nth-child(1) { animation-delay: 0.25s; }
.stat-grid-3 .stat-card:nth-child(2) { animation-delay: 0.30s; }
.stat-grid-3 .stat-card:nth-child(3) { animation-delay: 0.35s; }

/* ── Progress Card ── */
.progress-card {
    background: #ffffff;
    border: 1.5px solid var(--green-100);
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    margin-bottom: 24px;
    animation: fadeUp 0.45s 0.45s ease both;
}

.progress-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 20px;
    background: linear-gradient(135deg, #f0fdf4, #e8faf1);
    border-bottom: 1.5px solid var(--green-100);
    flex-wrap: wrap;
    gap: 8px;
}

.progress-card-head h6 {
    font-size: 0.86rem;
    font-weight: 700;
    color: var(--text-dark);
    margin: 0;
}

.progress-card-head .head-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.progress-pct-badge {
    font-family: 'Space Mono', monospace;
    font-size: 0.73rem;
    font-weight: 700;
    color: var(--green-700);
    background: #dcfce7;
    border: 1.5px solid #bbf7d0;
    padding: 3px 11px;
    border-radius: 20px;
}

.progress-period-badge {
    font-family: 'Space Mono', monospace;
    font-size: 0.68rem;
    color: var(--text-muted);
    background: var(--green-50);
    border: 1.5px solid var(--green-100);
    padding: 3px 10px;
    border-radius: 20px;
}

.progress-card-body { padding: 20px; }

.progress-track {
    width: 100%;
    height: 13px;
    background: var(--green-50);
    border: 1px solid var(--green-100);
    border-radius: 999px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    border-radius: 999px;
    background-size: 200% 100%;
    animation: shimmer 2.4s linear infinite;
    transition: width 1s cubic-bezier(.4,0,.2,1);
}

.progress-fill.daily   { background: linear-gradient(90deg, #16a34a, #22c55e, #4ade80); box-shadow: 0 0 8px rgba(34,197,94,0.3); }
.progress-fill.weekly  { background: linear-gradient(90deg, #0369a1, #0ea5e9, #38bdf8); box-shadow: 0 0 8px rgba(14,165,233,0.3); }
.progress-fill.monthly { background: linear-gradient(90deg, #7c3aed, #a855f7, #c084fc); box-shadow: 0 0 8px rgba(168,85,247,0.3); }
.progress-fill.warning { background: linear-gradient(90deg, #d97706, #f59e0b, #fbbf24); box-shadow: 0 0 8px rgba(245,158,11,0.3); }
.progress-fill.danger  { background: linear-gradient(90deg, #dc2626, #ef4444, #f87171); box-shadow: 0 0 8px rgba(239,68,68,0.3); }

.progress-meta {
    display: flex;
    justify-content: space-between;
    margin-top: 12px;
    flex-wrap: wrap;
    gap: 6px;
}

.progress-meta .meta-item {
    font-size: 0.72rem;
    color: var(--text-muted);
    font-family: 'Space Mono', monospace;
    background: var(--green-50);
    border: 1px solid var(--green-100);
    padding: 3px 10px;
    border-radius: 7px;
}

.progress-meta .meta-item strong { color: var(--text-dark); font-weight: 700; }

.progress-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 24px;
}

@media (max-width: 640px) {
    .progress-grid { grid-template-columns: 1fr; }
}

.progress-card.weekly .progress-card-head  { background: linear-gradient(135deg, #eff6ff, #dbeafe); border-bottom-color: #bfdbfe; }
.progress-card.weekly .progress-pct-badge  { background: #dbeafe; border-color: #bfdbfe; color: #1d4ed8; }
.progress-card.weekly .progress-track      { background: #eff6ff; border-color: #bfdbfe; }

.progress-card.monthly .progress-card-head { background: linear-gradient(135deg, #faf5ff, #ede9fe); border-bottom-color: #ddd6fe; }
.progress-card.monthly .progress-pct-badge { background: #ede9fe; border-color: #ddd6fe; color: #7c3aed; }
.progress-card.monthly .progress-track     { background: #faf5ff; border-color: #ddd6fe; }

/* ══════════════════════════════════════
   ── ROLE PROGRESS STYLES ──
══════════════════════════════════════ */
.role-progress-card {
    background: #ffffff;
    border: 1.5px solid var(--green-100);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    margin-bottom: 16px;
    animation: fadeUp 0.4s ease both;
    transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

.role-progress-card:hover {
    box-shadow: 0 6px 20px rgba(0,0,0,0.08);
    border-color: var(--green-200);
}

.role-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 13px 18px;
    background: linear-gradient(135deg, #f8fafc, #f1f5f9);
    border-bottom: 1.5px solid #e2e8f0;
    cursor: pointer;
    user-select: none;
    gap: 10px;
    flex-wrap: wrap;
}

.role-card-header:hover { background: linear-gradient(135deg, #f1f5f9, #e8edf5); }

.role-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.role-avatar {
    width: 34px; height: 34px;
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    font-size: 13px;
    font-weight: 800;
    color: #fff;
    flex-shrink: 0;
    letter-spacing: -0.5px;
}

.role-name {
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--text-dark);
    text-transform: capitalize;
}

.role-header-badges {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.role-mini-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.65rem;
    font-weight: 700;
    font-family: 'Space Mono', monospace;
    padding: 3px 8px;
    border-radius: 20px;
    border: 1.5px solid;
    white-space: nowrap;
}

.role-mini-badge.type-daily   { background: #dcfce7; color: #15803d; border-color: #bbf7d0; }
.role-mini-badge.type-weekly  { background: #dbeafe; color: #1d4ed8; border-color: #bfdbfe; }
.role-mini-badge.type-monthly { background: #ede9fe; color: #6d28d9; border-color: #ddd6fe; }
.role-mini-badge.pct-done     { background: #dcfce7; color: #15803d; border-color: #bbf7d0; }
.role-mini-badge.pct-warn     { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.role-mini-badge.pct-danger   { background: #fef2f2; color: #dc2626; border-color: #fecaca; }

.role-toggle-icon {
    font-size: 0.75rem;
    color: var(--text-muted);
    transition: transform 0.3s ease;
    flex-shrink: 0;
}

.role-toggle-icon.open { transform: rotate(180deg); }

.role-card-body {
    display: none;
    padding: 18px;
    gap: 14px;
    flex-direction: column;
}

.role-card-body.open { display: flex; }

.role-progress-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

@media (max-width: 640px) {
    .role-progress-row { grid-template-columns: 1fr; }
}

.rp-item {
    background: #f8fafc;
    border: 1.5px solid #e2e8f0;
    border-radius: 11px;
    padding: 13px 14px;
    transition: border-color 0.2s;
}

.rp-item:hover { border-color: #cbd5e1; }

.rp-item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.rp-item-label {
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    gap: 5px;
}

.rp-item-label.daily   { color: #15803d; }
.rp-item-label.weekly  { color: #1d4ed8; }
.rp-item-label.monthly { color: #6d28d9; }

.rp-item-pct {
    font-family: 'Space Mono', monospace;
    font-size: 0.75rem;
    font-weight: 700;
}

.rp-track {
    width: 100%;
    height: 8px;
    background: #e2e8f0;
    border-radius: 999px;
    overflow: hidden;
    margin-bottom: 10px;
}

.rp-fill {
    height: 100%;
    border-radius: 999px;
    transition: width 1s cubic-bezier(.4,0,.2,1);
    background-size: 200% 100%;
    animation: shimmer 2.4s linear infinite;
}

.rp-fill.daily   { background: linear-gradient(90deg, #16a34a, #22c55e, #4ade80); }
.rp-fill.weekly  { background: linear-gradient(90deg, #0369a1, #0ea5e9, #38bdf8); }
.rp-fill.monthly { background: linear-gradient(90deg, #7c3aed, #a855f7, #c084fc); }
.rp-fill.warning { background: linear-gradient(90deg, #d97706, #f59e0b, #fbbf24); }
.rp-fill.danger  { background: linear-gradient(90deg, #dc2626, #ef4444, #f87171); }

.rp-stats {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.rp-stat-chip {
    font-size: 0.65rem;
    font-family: 'Space Mono', monospace;
    color: #64748b;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    padding: 2px 7px;
    border-radius: 5px;
}

.rp-stat-chip strong { color: #1e293b; }

.rp-empty {
    font-size: 0.7rem;
    color: #94a3b8;
    font-style: italic;
    text-align: center;
    padding: 6px 0;
}

.role-avatar.color-0 { background: linear-gradient(135deg, #16a34a, #22c55e); }
.role-avatar.color-1 { background: linear-gradient(135deg, #0369a1, #0ea5e9); }
.role-avatar.color-2 { background: linear-gradient(135deg, #7c3aed, #a855f7); }
.role-avatar.color-3 { background: linear-gradient(135deg, #c2410c, #f97316); }
.role-avatar.color-4 { background: linear-gradient(135deg, #0f766e, #14b8a6); }
.role-avatar.color-5 { background: linear-gradient(135deg, #be185d, #ec4899); }
.role-avatar.color-6 { background: linear-gradient(135deg, #b45309, #f59e0b); }
.role-avatar.color-7 { background: linear-gradient(135deg, #1d4ed8, #60a5fa); }

@keyframes fadeUp {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}
</style>

<div class="dash-wrapper">

    <div class="dash-header">
        <h1 class="dash-title">Dashboard <span>Direktur</span></h1>
        <span class="dash-date"><?= strtoupper(date('D, d M Y')) ?></span>
    </div>

    <!-- Ringkasan Sistem -->
    <p class="section-label">Ringkasan Sistem</p>

    <div class="stat-grid">
        <div class="stat-card">
            <div class="orb"></div>
            <div class="card-icon">👤</div>
            <div class="card-value"><?= $total_user ?></div>
            <div class="card-label">Total User</div>
        </div>

        <div class="stat-card">
            <div class="orb"></div>
            <div class="card-icon">📋</div>
            <div class="card-value"><?= $total_checklist ?></div>
            <div class="card-label">Total Checklist</div>
        </div>

        <div class="stat-card mod">
            <div class="orb" style="background:radial-gradient(circle,rgba(14,165,233,0.1) 0%,transparent 70%);"></div>
            <div class="card-icon">🛡️</div>
            <div class="card-value"><?= $total_mod ?></div>
            <div class="card-label">MOD Hari Ini</div>
            <?php if(count($mod_names) > 0): ?>
            <div class="mod-names-list">
                <?php foreach($mod_names as $mn):
                    $wl = strtolower($mn['waktu'] ?? '');
                    $chip_class = '';
                    $chip_icon  = '🛡️';
                    if(str_contains($wl,'pagi'))       { $chip_class='pagi';  $chip_icon='🌅'; }
                    elseif(str_contains($wl,'siang'))  { $chip_class='siang'; $chip_icon='☀️'; }
                    elseif(str_contains($wl,'sore'))   { $chip_class='sore';  $chip_icon='🌇'; }
                    elseif(str_contains($wl,'malam'))  { $chip_class='malam'; $chip_icon='🌙'; }
                ?>
                <span class="mod-name-chip <?= $chip_class ?>">
                    <?= $chip_icon ?> <?= htmlspecialchars($mn['nama']) ?>
                </span>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>

        <div class="stat-card urgent">
            <div class="orb" style="background:radial-gradient(circle,rgba(239,68,68,0.1) 0%,transparent 70%);"></div>
            <div class="card-icon">🚨</div>
            <div class="card-value"><?= $total_urgent ?></div>
            <div class="card-label">Urgent Belum Selesai</div>
        </div>
    </div>

    <!-- Status Checklist Harian -->
    <p class="section-label">Status Checklist Harian</p>

    <div class="stat-grid-3">
        <div class="stat-card success">
            <div class="orb"></div>
            <div class="card-icon">✅</div>
            <div class="card-value"><?= $q_selesai ?></div>
            <div class="card-label">Checklist Selesai</div>
        </div>
        <div class="stat-card danger">
            <div class="orb"></div>
            <div class="card-icon">⏳</div>
            <div class="card-value"><?= $q_belum ?></div>
            <div class="card-label">Checklist Belum</div>
        </div>
        <div class="stat-card">
            <div class="orb"></div>
            <div class="card-icon">📊</div>
            <div class="card-value"><?= $progress ?>%</div>
            <div class="card-label">Progress Hari Ini</div>
        </div>
    </div>

    <!-- Progress Checklist -->
    <p class="section-label">Progress Checklist</p>

    <div class="progress-card">
        <div class="progress-card-head">
            <h6>📅 Progress Harian</h6>
            <div class="head-right">
                <span class="progress-period-badge"><?= date('d/m/Y') ?></span>
                <span class="progress-pct-badge"><?= $progress ?>%</span>
            </div>
        </div>
        <div class="progress-card-body">
            <?php $dc = $progress==100?'daily':($progress>=50?'warning':'danger'); ?>
            <div class="progress-track">
                <div class="progress-fill <?= $dc ?>" style="width:<?= $progress ?>%;"></div>
            </div>
            <div class="progress-meta">
                <div class="meta-item">Selesai: <strong><?= $q_selesai ?></strong></div>
                <div class="meta-item">Total: <strong><?= $total_harian ?></strong></div>
                <div class="meta-item">Belum: <strong><?= $q_belum ?></strong></div>
            </div>
        </div>
    </div>

    <!-- Progress Weekly & Monthly -->
    <div class="progress-grid">

        <div class="progress-card weekly">
            <div class="progress-card-head">
                <h6>📆 Progress Mingguan</h6>
                <div class="head-right">
                    <span class="progress-period-badge">
                        <?= date('d/m', strtotime($week_start)) ?> – <?= date('d/m/Y', strtotime($week_end)) ?>
                    </span>
                    <span class="progress-pct-badge"><?= $week_pct ?>%</span>
                </div>
            </div>
            <div class="progress-card-body">
                <?php $wc = $week_pct==100?'weekly':($week_pct>=50?'warning':'danger'); ?>
                <div class="progress-track">
                    <div class="progress-fill <?= $wc ?>" style="width:<?= $week_pct ?>%;"></div>
                </div>
                <div class="progress-meta">
                    <div class="meta-item">Selesai: <strong><?= $week_selesai ?></strong></div>
                    <div class="meta-item">Total: <strong><?= $total_weekly ?></strong></div>
                    <div class="meta-item">Belum: <strong><?= $week_belum ?></strong></div>
                </div>
            </div>
        </div>

        <div class="progress-card monthly">
            <div class="progress-card-head">
                <h6>🗓️ Progress Bulanan</h6>
                <div class="head-right">
                    <span class="progress-period-badge">
                        <?= date('d/m', strtotime($month_start)) ?> – <?= date('d/m/Y', strtotime($month_end)) ?>
                    </span>
                    <span class="progress-pct-badge"><?= $month_pct ?>%</span>
                </div>
            </div>
            <div class="progress-card-body">
                <?php $mc = $month_pct==100?'monthly':($month_pct>=50?'warning':'danger'); ?>
                <div class="progress-track">
                    <div class="progress-fill <?= $mc ?>" style="width:<?= $month_pct ?>%;"></div>
                </div>
                <div class="progress-meta">
                    <div class="meta-item">Selesai: <strong><?= $month_selesai ?></strong></div>
                    <div class="meta-item">Total: <strong><?= $total_monthly ?></strong></div>
                    <div class="meta-item">Belum: <strong><?= $month_belum ?></strong></div>
                </div>
            </div>
        </div>

    </div>

    <!-- Progress Per Role -->
    <p class="section-label">Progress Per Role</p>

    <?php if (empty($role_progress)): ?>
        <div style="text-align:center;padding:32px;color:var(--text-muted);font-size:0.85rem;
                    background:#f8fafc;border:1.5px dashed #e2e8f0;border-radius:14px;margin-bottom:24px;">
            🗂️ Belum ada data checklist per role yang ditemukan.
        </div>
    <?php else: ?>

    <?php foreach ($role_progress as $idx => $rp):
        $color_idx = $idx % 8;
        $initials  = strtoupper(substr(preg_replace('/[^a-zA-Z]/', '', $rp['role']), 0, 2));
        if (empty($initials)) $initials = '??';

        $active_types = 0; $sum_pct = 0;
        if ($rp['daily']['total'] > 0)   { $sum_pct += $rp['daily']['pct'];   $active_types++; }
        if ($rp['weekly']['total'] > 0)  { $sum_pct += $rp['weekly']['pct'];  $active_types++; }
        if ($rp['monthly']['total'] > 0) { $sum_pct += $rp['monthly']['pct']; $active_types++; }
        $overall_pct   = $active_types > 0 ? round($sum_pct / $active_types) : 0;
        $overall_class = $overall_pct == 100 ? 'pct-done' : ($overall_pct >= 50 ? 'pct-warn' : 'pct-danger');

        $fc = fn($pct, $type) => $pct == 100 ? $type : ($pct >= 50 ? 'warning' : 'danger');
    ?>
    <div class="role-progress-card" style="animation-delay:<?= 0.05*($idx+1) ?>s;">

        <div class="role-card-header" onclick="toggleRole(this)">
            <div class="role-header-left">
                <div class="role-avatar color-<?= $color_idx ?>"><?= $initials ?></div>
                <span class="role-name"><?= htmlspecialchars($rp['role']) ?></span>
            </div>
            <div class="role-header-badges">
                <?php if ($rp['daily']['total'] > 0): ?>
                <span class="role-mini-badge type-daily">📅 D: <?= $rp['daily']['pct'] ?>%</span>
                <?php endif; ?>
                <?php if ($rp['weekly']['total'] > 0): ?>
                <span class="role-mini-badge type-weekly">📆 W: <?= $rp['weekly']['pct'] ?>%</span>
                <?php endif; ?>
                <?php if ($rp['monthly']['total'] > 0): ?>
                <span class="role-mini-badge type-monthly">🗓️ M: <?= $rp['monthly']['pct'] ?>%</span>
                <?php endif; ?>
                <span class="role-mini-badge <?= $overall_class ?>">⚡ <?= $overall_pct ?>% avg</span>
                <span class="role-toggle-icon">▼</span>
            </div>
        </div>

        <div class="role-card-body">
            <div class="role-progress-row">

                <!-- Daily -->
                <div class="rp-item">
                    <div class="rp-item-head">
                        <span class="rp-item-label daily">📅 Daily</span>
                        <span class="rp-item-pct" style="color:<?= $rp['daily']['pct']==100?'#16a34a':($rp['daily']['pct']>=50?'#d97706':'#dc2626') ?>">
                            <?= $rp['daily']['total'] > 0 ? $rp['daily']['pct'].'%' : '—' ?>
                        </span>
                    </div>
                    <?php if ($rp['daily']['total'] > 0): ?>
                    <div class="rp-track">
                        <div class="rp-fill <?= $fc($rp['daily']['pct'],'daily') ?>"
                             style="width:<?= $rp['daily']['pct'] ?>%;"></div>
                    </div>
                    <div class="rp-stats">
                        <div class="rp-stat-chip">✅ <strong><?= $rp['daily']['selesai'] ?></strong></div>
                        <div class="rp-stat-chip">⏳ <strong><?= $rp['daily']['belum'] ?></strong></div>
                        <div class="rp-stat-chip">📋 <strong><?= $rp['daily']['total'] ?></strong></div>
                    </div>
                    <?php else: ?>
                        <div class="rp-empty">Tidak ada checklist daily</div>
                    <?php endif; ?>
                </div>

                <!-- Weekly -->
                <div class="rp-item">
                    <div class="rp-item-head">
                        <span class="rp-item-label weekly">📆 Weekly</span>
                        <span class="rp-item-pct" style="color:<?= $rp['weekly']['pct']==100?'#1d4ed8':($rp['weekly']['pct']>=50?'#d97706':'#dc2626') ?>">
                            <?= $rp['weekly']['total'] > 0 ? $rp['weekly']['pct'].'%' : '—' ?>
                        </span>
                    </div>
                    <?php if ($rp['weekly']['total'] > 0): ?>
                    <div class="rp-track">
                        <div class="rp-fill <?= $fc($rp['weekly']['pct'],'weekly') ?>"
                             style="width:<?= $rp['weekly']['pct'] ?>%;"></div>
                    </div>
                    <div class="rp-stats">
                        <div class="rp-stat-chip">✅ <strong><?= $rp['weekly']['selesai'] ?></strong></div>
                        <div class="rp-stat-chip">⏳ <strong><?= $rp['weekly']['belum'] ?></strong></div>
                        <div class="rp-stat-chip">📋 <strong><?= $rp['weekly']['total'] ?></strong></div>
                    </div>
                    <?php else: ?>
                        <div class="rp-empty">Tidak ada checklist weekly</div>
                    <?php endif; ?>
                </div>

                <!-- Monthly -->
                <div class="rp-item">
                    <div class="rp-item-head">
                        <span class="rp-item-label monthly">🗓️ Monthly</span>
                        <span class="rp-item-pct" style="color:<?= $rp['monthly']['pct']==100?'#6d28d9':($rp['monthly']['pct']>=50?'#d97706':'#dc2626') ?>">
                            <?= $rp['monthly']['total'] > 0 ? $rp['monthly']['pct'].'%' : '—' ?>
                        </span>
                    </div>
                    <?php if ($rp['monthly']['total'] > 0): ?>
                    <div class="rp-track">
                        <div class="rp-fill <?= $fc($rp['monthly']['pct'],'monthly') ?>"
                             style="width:<?= $rp['monthly']['pct'] ?>%;"></div>
                    </div>
                    <div class="rp-stats">
                        <div class="rp-stat-chip">✅ <strong><?= $rp['monthly']['selesai'] ?></strong></div>
                        <div class="rp-stat-chip">⏳ <strong><?= $rp['monthly']['belum'] ?></strong></div>
                        <div class="rp-stat-chip">📋 <strong><?= $rp['monthly']['total'] ?></strong></div>
                    </div>
                    <?php else: ?>
                        <div class="rp-empty">Tidak ada checklist monthly</div>
                    <?php endif; ?>
                </div>

            </div>
        </div>

    </div>
    <?php endforeach; ?>
    <?php endif; ?>

</div>

<script>
function toggleRole(header) {
    const body = header.nextElementSibling;
    const icon = header.querySelector('.role-toggle-icon');
    const isOpen = body.classList.contains('open');
    body.classList.toggle('open', !isOpen);
    icon.classList.toggle('open', !isOpen);
}

// Buka kartu pertama secara default
document.addEventListener('DOMContentLoaded', function() {
    const firstHeader = document.querySelector('.role-card-header');
    if (firstHeader) toggleRole(firstHeader);
});
</script>

<?php include 'layout/footer.php'; ?>