<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/functions.php';

// Cek apakah user sudah login dan memiliki role reviewer
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'reviewer') {
    header('Location: ../login.php');
    exit;
}

$error = '';
$success = '';

// Hitung statistik untuk dashboard
try {
    // Total soal teori yang belum direview
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM bank_soal_teori WHERE status_review = 'belum'");
    $stmt->execute();
    $total_belum_review_teori = $stmt->fetchColumn();

    // Total soal teori yang sudah direview oleh user ini
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM bank_soal_teori WHERE direview_oleh = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $total_sudah_review_teori = $stmt->fetchColumn();

    // Total soal teori S1 yang belum direview
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM bank_soal_teori_s1 WHERE status_review = 'belum'");
    $stmt->execute();
    $total_belum_review_s1 = $stmt->fetchColumn();

    // Total soal teori S1 yang sudah direview oleh user ini
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM bank_soal_teori_s1 WHERE direview_oleh = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $total_sudah_review_s1 = $stmt->fetchColumn();

    // Soal yang perlu direview (belum + revisi)
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM bank_soal_teori WHERE status_review IN ('belum', 'revisi')");
    $stmt->execute();
    $total_perlu_review_teori = $stmt->fetchColumn();

    $stmt = $pdo->prepare("SELECT COUNT(*) FROM bank_soal_teori_s1 WHERE status_review IN ('belum', 'revisi')");
    $stmt->execute();
    $total_perlu_review_s1 = $stmt->fetchColumn();

    // Total semua soal
    $total_semua_soal = $total_belum_review_teori + $total_sudah_review_teori + $total_belum_review_s1 + $total_sudah_review_s1;
    $total_belum = $total_belum_review_teori + $total_belum_review_s1;
    $total_sudah = $total_sudah_review_teori + $total_sudah_review_s1;
    $total_perlu = $total_perlu_review_teori + $total_perlu_review_s1;

} catch (PDOException $e) {
    $error = 'Gagal mengambil statistik: ' . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Reviewer - SOCA OSCE System</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* ===== RESET & BASE ===== */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f0f4f8;
            display: flex;
            min-height: 100vh;
        }

        /* ===== SIDEBAR ===== */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, #1a2332 0%, #0f1724 100%);
            color: #fff;
            padding: 0;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            z-index: 100;
            transition: all 0.3s ease;
            box-shadow: 4px 0 20px rgba(0,0,0,0.1);
        }

        .sidebar::-webkit-scrollbar {
            width: 4px;
        }

        .sidebar::-webkit-scrollbar-track {
            background: transparent;
        }

        .sidebar::-webkit-scrollbar-thumb {
            background: rgba(255,255,255,0.2);
            border-radius: 10px;
        }

        .sidebar-header {
            padding: 25px 20px 20px;
            border-bottom: 1px solid rgba(255,255,255,0.08);
            text-align: center;
        }

        .sidebar-header h2 {
            font-size: 20px;
            font-weight: 700;
            letter-spacing: 1px;
            background: linear-gradient(135deg, #60a5fa, #a78bfa);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .sidebar-header p {
            font-size: 12px;
            color: rgba(255,255,255,0.5);
            margin-top: 4px;
            letter-spacing: 2px;
            text-transform: uppercase;
        }

        .sidebar-menu {
            list-style: none;
            padding: 15px 0;
        }

        .sidebar-menu li {
            margin: 2px 12px;
        }

        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 12px 16px;
            color: rgba(255,255,255,0.6);
            text-decoration: none;
            border-radius: 12px;
            transition: all 0.3s ease;
            font-size: 14px;
            font-weight: 500;
        }

        .sidebar-menu li a i {
            width: 20px;
            font-size: 16px;
            text-align: center;
        }

        .sidebar-menu li a:hover {
            background: rgba(255,255,255,0.08);
            color: #fff;
            transform: translateX(4px);
        }

        .sidebar-menu li a.active {
            background: linear-gradient(135deg, rgba(96,165,250,0.2), rgba(167,139,250,0.2));
            color: #60a5fa;
            box-shadow: inset 3px 0 0 #60a5fa;
        }

        .sidebar-menu .menu-divider {
            height: 1px;
            background: rgba(255,255,255,0.06);
            margin: 12px 20px;
        }

        .sidebar-menu .menu-label {
            padding: 10px 20px 6px;
            font-size: 10px;
            text-transform: uppercase;
            letter-spacing: 1.5px;
            color: rgba(255,255,255,0.25);
            font-weight: 600;
        }

        /* ===== MAIN CONTENT ===== */
        .main-content {
            margin-left: 260px;
            flex: 1;
            padding: 30px 35px 40px;
            min-height: 100vh;
        }

        /* ===== HEADER ===== */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            flex-wrap: wrap;
            gap: 15px;
        }

        .header-left h1 {
            font-size: 28px;
            font-weight: 700;
            color: #1a2332;
            letter-spacing: -0.5px;
        }

        .header-left .subtitle {
            font-size: 14px;
            color: #6b7a8f;
            margin-top: 4px;
        }

        .header-right {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .header-right .user-badge {
            display: flex;
            align-items: center;
            gap: 10px;
            background: #fff;
            padding: 8px 16px 8px 12px;
            border-radius: 50px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.06);
        }

        .header-right .user-badge .avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background: linear-gradient(135deg, #60a5fa, #a78bfa);
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-weight: 600;
            font-size: 16px;
        }

        .header-right .user-badge .user-name {
            font-weight: 600;
            font-size: 14px;
            color: #1a2332;
        }

        .header-right .user-badge .user-role {
            font-size: 12px;
            color: #6b7a8f;
        }

        .btn-logout {
            background: #fee2e2;
            color: #dc2626;
            border: none;
            padding: 8px 16px;
            border-radius: 50px;
            font-size: 13px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn-logout:hover {
            background: #fecaca;
            transform: scale(1.02);
        }

        /* ===== ALERT ===== */
        .alert {
            padding: 16px 20px;
            border-radius: 12px;
            margin-bottom: 20px;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .alert-success {
            background: #d1fae5;
            color: #065f46;
            border: 1px solid #a7f3d0;
        }

        .alert-error {
            background: #fee2e2;
            color: #991b1b;
            border: 1px solid #fca5a5;
        }

        .alert i {
            font-size: 18px;
        }

        /* ===== WELCOME CARD ===== */
        .welcome-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            padding: 30px 35px;
            margin-bottom: 30px;
            color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }

        .welcome-card .welcome-text h2 {
            font-size: 24px;
            font-weight: 700;
        }

        .welcome-card .welcome-text p {
            opacity: 0.9;
            margin-top: 4px;
            font-size: 14px;
        }

        .welcome-card .welcome-badge {
            background: rgba(255,255,255,0.2);
            padding: 8px 20px;
            border-radius: 50px;
            font-size: 13px;
            font-weight: 600;
            backdrop-filter: blur(4px);
        }

        /* ===== STATS CARDS ===== */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
            gap: 16px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: #fff;
            border-radius: 14px;
            padding: 20px 22px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.05);
            border: 1px solid rgba(0,0,0,0.04);
            transition: all 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 24px rgba(0,0,0,0.08);
        }

        .stat-card .stat-number {
            font-size: 28px;
            font-weight: 700;
            color: #1a2332;
        }

        .stat-card .stat-label {
            font-size: 13px;
            color: #6b7a8f;
            font-weight: 500;
        }

        .stat-card .stat-icon {
            float: right;
            font-size: 28px;
            opacity: 0.3;
        }

        .stat-card.orange .stat-icon { color: #f59e0b; }
        .stat-card.red .stat-icon { color: #dc2626; }
        .stat-card.green .stat-icon { color: #059669; }
        .stat-card.blue .stat-icon { color: #2563eb; }

        /* ===== ACTION CARDS ===== */
        .action-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
            gap: 16px;
            margin-bottom: 30px;
        }

        .action-card {
            background: #fff;
            border-radius: 14px;
            padding: 22px 24px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.05);
            border: 1px solid rgba(0,0,0,0.04);
            text-align: center;
            transition: all 0.3s ease;
            text-decoration: none;
            color: #1a2332;
        }

        .action-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 30px rgba(0,0,0,0.08);
            border-color: #60a5fa;
        }

        .action-card .action-icon {
            font-size: 32px;
            color: #60a5fa;
            margin-bottom: 10px;
        }

        .action-card .action-title {
            font-size: 15px;
            font-weight: 600;
            color: #1a2332;
        }

        .action-card .action-desc {
            font-size: 13px;
            color: #6b7a8f;
            margin-top: 4px;
        }

        .action-card .action-badge {
            display: inline-block;
            margin-top: 8px;
            padding: 2px 12px;
            background: #fee2e2;
            color: #dc2626;
            border-radius: 50px;
            font-size: 12px;
            font-weight: 600;
        }

        /* ===== DETAIL STATS ===== */
        .detail-card {
            background: #fff;
            border-radius: 14px;
            padding: 24px 28px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.05);
            border: 1px solid rgba(0,0,0,0.04);
        }

        .detail-card .detail-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 16px;
            flex-wrap: wrap;
            gap: 10px;
        }

        .detail-card .detail-header h3 {
            font-size: 16px;
            font-weight: 600;
            color: #1a2332;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .detail-card .detail-header h3 i {
            color: #60a5fa;
        }

        .detail-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }

        .detail-group h4 {
            font-size: 14px;
            font-weight: 600;
            color: #475569;
            margin-bottom: 8px;
        }

        .detail-item {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #f1f5f9;
            font-size: 14px;
        }

        .detail-item:last-child {
            border-bottom: none;
        }

        .detail-item .item-label {
            color: #475569;
        }

        .detail-item .item-value {
            font-weight: 600;
            color: #1a2332;
        }

        .detail-item .item-value.belum { color: #dc2626; }
        .detail-item .item-value.sudah { color: #059669; }
        .detail-item .item-value.perlu { color: #f59e0b; }

        /* ===== RESPONSIVE ===== */
        @media (max-width: 992px) {
            .sidebar {
                width: 220px;
            }
            .main-content {
                margin-left: 220px;
                padding: 20px;
            }
        }

        @media (max-width: 768px) {
            .sidebar {
                width: 0;
                transform: translateX(-100%);
            }
            .sidebar.open {
                width: 280px;
                transform: translateX(0);
            }
            .main-content {
                margin-left: 0;
                padding: 15px;
            }
            .header {
                flex-direction: column;
                align-items: flex-start;
            }
            .welcome-card {
                flex-direction: column;
                text-align: center;
            }
            .stats-grid {
                grid-template-columns: 1fr 1fr;
            }
            .action-grid {
                grid-template-columns: 1fr 1fr;
            }
            .detail-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 480px) {
            .header-left h1 {
                font-size: 22px;
            }
            .stats-grid {
                grid-template-columns: 1fr;
            }
            .action-grid {
                grid-template-columns: 1fr;
            }
        }

        .hamburger {
            display: none;
            background: none;
            border: none;
            font-size: 24px;
            color: #1a2332;
            cursor: pointer;
            padding: 8px;
            margin-bottom: 10px;
        }

        @media (max-width: 768px) {
            .hamburger {
                display: block;
            }
        }

        .sidebar-overlay {
            display: none;
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,0.4);
            z-index: 99;
        }

        .sidebar-overlay.active {
            display: block;
        }

        .scroll-top {
            position: fixed;
            bottom: 30px;
            right: 30px;
            background: #2563eb;
            color: #fff;
            width: 44px;
            height: 44px;
            border-radius: 50%;
            border: none;
            cursor: pointer;
            font-size: 18px;
            box-shadow: 0 4px 16px rgba(37,99,235,0.3);
            transition: all 0.3s ease;
            opacity: 0;
            visibility: hidden;
            z-index: 50;
        }

        .scroll-top.visible {
            opacity: 1;
            visibility: visible;
        }

        .scroll-top:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 24px rgba(37,99,235,0.4);
        }
    </style>
</head>
<body>
    <!-- Overlay untuk mobile -->
    <div class="sidebar-overlay" id="sidebarOverlay"></div>

    <!-- Sidebar -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <h2>SOCA/OSCE</h2>
            <p>Reviewer Panel</p>
        </div>
        <ul class="sidebar-menu">
            <li class="menu-label">Main Menu</li>
            <li><a href="dashboard_reviewer.php" class="active"><i class="fas fa-th-large"></i> Dashboard</a></li>
            
            <li class="menu-divider"></li>
            <li class="menu-label">Review</li>
            <li><a href="review_soal_teori_s1.php"><i class="fas fa-book-open"></i> Review Soal Teori S1</a></li>
            <li><a href="review_soal_teori.php"><i class="fas fa-book"></i> Review Soal Teori Profesi</a></li>
            <li><a href="review_soal_praktik.php"><i class="fas fa-clipboard-check"></i> Review Soal Praktik</a></li>
            <li><a href="review_aspek_penilaian.php"><i class="fas fa-eye"></i> Preview Aspek Penilaian</a></li>
            
            <li class="menu-divider"></li>
            <li class="menu-label">Lainnya</li>
            <li><a href="riwayat_review.php"><i class="fas fa-history"></i> Riwayat Review</a></li>
            <li><a href="panduan_review.php"><i class="fas fa-book"></i> Panduan Review</a></li>
            <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
        </ul>
    </div>

    <!-- Main Content -->
    <div class="main-content">
        <!-- Header -->
        <div class="header">
            <div class="header-left">
                <button class="hamburger" id="hamburgerBtn">
                    <i class="fas fa-bars"></i>
                </button>
                <h1>Dashboard Reviewer</h1>
                <div class="subtitle">Panel review dan validasi soal</div>
            </div>
            <div class="header-right">
                <div class="user-badge">
                    <div class="avatar">
                        <?php echo strtoupper(substr($_SESSION['user_name'] ?? 'R', 0, 1)); ?>
                    </div>
                    <div>
                        <div class="user-name"><?php echo $_SESSION['user_name'] ?? 'Reviewer'; ?></div>
                        <div class="user-role">Reviewer</div>
                    </div>
                </div>
                <a href="../logout.php" class="btn-logout">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </div>
        </div>

        <!-- Alert -->
        <?php if ($error): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-circle"></i>
                <?php echo $error; ?>
            </div>
        <?php endif; ?>
        
        <?php if ($success): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                <?php echo $success; ?>
            </div>
        <?php endif; ?>

        <!-- Welcome Card -->
        <div class="welcome-card">
            <div class="welcome-text">
                <h2>Selamat Datang, <?php echo $_SESSION['user_name'] ?? 'Reviewer'; ?>! 👋</h2>
                <p>Anda login sebagai <strong>Reviewer</strong>. Tugas Anda adalah mereview dan memvalidasi kualitas soal sebelum digunakan dalam ujian.</p>
            </div>
            <div class="welcome-badge">
                <i class="fas fa-user-check"></i> Reviewer
            </div>
        </div>

        <!-- Stats -->
        <div class="stats-grid">
            <div class="stat-card orange">
                <div class="stat-icon"><i class="fas fa-tasks"></i></div>
                <div class="stat-number"><?php echo $total_perlu; ?></div>
                <div class="stat-label">Perlu Review</div>
            </div>
            <div class="stat-card red">
                <div class="stat-icon"><i class="fas fa-clock"></i></div>
                <div class="stat-number"><?php echo $total_belum; ?></div>
                <div class="stat-label">Belum Direview</div>
            </div>
            <div class="stat-card green">
                <div class="stat-icon"><i class="fas fa-check-circle"></i></div>
                <div class="stat-number"><?php echo $total_sudah; ?></div>
                <div class="stat-label">Sudah Direview</div>
            </div>
            <div class="stat-card blue">
                <div class="stat-icon"><i class="fas fa-chart-bar"></i></div>
                <div class="stat-number"><?php echo $total_semua_soal > 0 ? round(($total_sudah / $total_semua_soal) * 100) : 0; ?>%</div>
                <div class="stat-label">Progress Review</div>
            </div>
        </div>

        <!-- Action Cards -->
        <div class="action-grid">
            <a href="review_soal_teori.php" class="action-card">
                <div class="action-icon"><i class="fas fa-book"></i></div>
                <div class="action-title">Review Soal Teori</div>
                <div class="action-desc">Review soal teori reguler</div>
                <span class="action-badge"><?php echo $total_perlu_review_teori; ?> perlu review</span>
            </a>

            <a href="review_soal_teori_s1.php" class="action-card">
                <div class="action-icon"><i class="fas fa-book-open"></i></div>
                <div class="action-title">Review Soal Teori S1</div>
                <div class="action-desc">Review soal teori program S1</div>
                <span class="action-badge"><?php echo $total_perlu_review_s1; ?> perlu review</span>
            </a>

            <a href="riwayat_review.php" class="action-card">
                <div class="action-icon"><i class="fas fa-history"></i></div>
                <div class="action-title">Riwayat Review</div>
                <div class="action-desc">Lihat semua soal yang sudah direview</div>
                <span class="action-badge" style="background:#d1fae5;color:#059669;"><?php echo $total_sudah; ?> sudah direview</span>
            </a>

            <a href="panduan_review.php" class="action-card">
                <div class="action-icon"><i class="fas fa-question-circle"></i></div>
                <div class="action-title">Panduan Review</div>
                <div class="action-desc">Pelajari standar review soal</div>
                <span class="action-badge" style="background:#dbeafe;color:#2563eb;">Panduan</span>
            </a>
        </div>

        <!-- Detail Stats -->
        <div class="detail-card">
            <div class="detail-header">
                <h3><i class="fas fa-chart-pie"></i> Detail Statistik</h3>
            </div>
            <div class="detail-grid">
                <div>
                    <h4>Soal Teori Reguler</h4>
                    <div class="detail-item">
                        <span class="item-label"><i class="fas fa-clock" style="color:#dc2626;"></i> Belum Direview</span>
                        <span class="item-value belum"><?php echo $total_belum_review_teori; ?></span>
                    </div>
                    <div class="detail-item">
                        <span class="item-label"><i class="fas fa-check-circle" style="color:#059669;"></i> Sudah Direview</span>
                        <span class="item-value sudah"><?php echo $total_sudah_review_teori; ?></span>
                    </div>
                    <div class="detail-item">
                        <span class="item-label"><i class="fas fa-tasks" style="color:#f59e0b;"></i> Perlu Review</span>
                        <span class="item-value perlu"><?php echo $total_perlu_review_teori; ?></span>
                    </div>
                </div>
                <div>
                    <h4>Soal Teori S1</h4>
                    <div class="detail-item">
                        <span class="item-label"><i class="fas fa-clock" style="color:#dc2626;"></i> Belum Direview</span>
                        <span class="item-value belum"><?php echo $total_belum_review_s1; ?></span>
                    </div>
                    <div class="detail-item">
                        <span class="item-label"><i class="fas fa-check-circle" style="color:#059669;"></i> Sudah Direview</span>
                        <span class="item-value sudah"><?php echo $total_sudah_review_s1; ?></span>
                    </div>
                    <div class="detail-item">
                        <span class="item-label"><i class="fas fa-tasks" style="color:#f59e0b;"></i> Perlu Review</span>
                        <span class="item-value perlu"><?php echo $total_perlu_review_s1; ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Scroll to Top -->
    <button class="scroll-top" id="scrollTopBtn" title="Kembali ke atas">
        <i class="fas fa-arrow-up"></i>
    </button>

    <script>
        // ===== SIDEBAR TOGGLE =====
        const hamburger = document.getElementById('hamburgerBtn');
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('sidebarOverlay');

        function toggleSidebar() {
            sidebar.classList.toggle('open');
            overlay.classList.toggle('active');
        }

        hamburger.addEventListener('click', toggleSidebar);
        overlay.addEventListener('click', toggleSidebar);

        // ===== CLOSE SIDEBAR ON RESIZE =====
        window.addEventListener('resize', function() {
            if (window.innerWidth > 768) {
                sidebar.classList.remove('open');
                overlay.classList.remove('active');
            }
        });

        // ===== SCROLL TO TOP =====
        const scrollBtn = document.getElementById('scrollTopBtn');
        window.addEventListener('scroll', function() {
            if (window.scrollY > 300) {
                scrollBtn.classList.add('visible');
            } else {
                scrollBtn.classList.remove('visible');
            }
        });

        scrollBtn.addEventListener('click', function() {
            window.scrollTo({ top: 0, behavior: 'smooth' });
        });
    </script>
</body>
</html>