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

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

// Ambil jumlah soal teori dari database
$total_soal_teori = 0;
try {
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM bank_soal_teori");
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $total_soal_teori = $result['total'];
} catch (PDOException $e) {
    $error_teori = 'Gagal mengambil data soal teori';
}

// Ambil jumlah soal praktik dari database
$total_soal_praktik = 0;
try {
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM bank_soal_praktik");
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $total_soal_praktik = $result['total'];
} catch (PDOException $e) {
    $error_praktik = 'Gagal mengambil data soal praktik';
}

// Ambil data soal terbaru untuk ditampilkan
$soal_teori_terbaru = [];
$soal_praktik_terbaru = [];
try {
    // Ambil 5 soal teori terbaru
    $stmt = $pdo->query("SELECT * FROM bank_soal_teori ORDER BY created_at DESC LIMIT 5");
    $soal_teori_terbaru = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Ambil 5 soal praktik terbaru
    $stmt = $pdo->query("SELECT * FROM bank_soal_praktik ORDER BY created_at DESC LIMIT 5");
    $soal_praktik_terbaru = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $error_recent = 'Gagal mengambil data soal terbaru';
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - SOCA OSCE System</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="../assets/css/dashboard.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>SOCA/OSCE System</h2>
                <p>Admin Panel</p>
            </div>
            <ul class="sidebar-menu">
                <li><a href="dashboard.php" class="active">
                    <i class="fas fa-tachometer-alt"></i> Dashboard
                </a></li>
                <li><a href="bank_soal_teori_s1.php">
                    <i class="fas fa-question-circle"></i> Bank Soal Teori S1
                </a></li>
                <li><a href="list_soal_teori_s1.php">
                    <i class="fas fa-eye"></i> Lihat Soal Teori S1
                </a></li>
                <li><a href="bank_soal_teori.php">
                    <i class="fas fa-question-circle"></i> Bank Soal Teori
                </a></li>
                <li><a href="list_soal_teori.php">
                    <i class="fas fa-eye"></i> Lihat Soal Teori
                </a></li>
                <li><a href="bank_soal_praktik.php">
                    <i class="fas fa-stethoscope"></i> Bank Soal Praktik
                </a></li>
                <li><a href="list_soal_praktik.php">
                    <i class="fas fa-eye"></i> Lihat Soal Praktik
                </a></li>
                <li><a href="aspek_penilaian.php">
                    <i class="fas fa-clipboard-check"></i> Aspek Penilaian
                </a></li>
                <li><a href="preview_aspek_penilaian.php">
                    <i class="fas fa-eye"></i> Preview Aspek Penilaian
                </a></li>
                 <li><a href="users.php">
                    <i class="fas fa-users"></i> Kelola Users
                </a></li>
                <li><a href="../logout.php">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a></li>
            </ul>
        </div>

        <div class="main-content">
            <div class="header">
                <h1>Dashboard Admin</h1>
                <div class="user-info">
                    <span><i class="fas fa-user"></i> Halo, <?php echo $_SESSION['user_name'] ?? 'Admin'; ?></span>
                    <a href="../logout.php" class="btn btn-danger btn-sm">
                        <i class="fas fa-sign-out-alt"></i> Logout
                    </a>
                </div>
            </div>

            <div class="content">
                <!-- Statistik Cards -->
                <div class="dashboard-cards">
                    <div class="card">
                        <div class="card-header">
                            <div class="card-icon icon-teori">
                                <i class="fas fa-book"></i>
                            </div>
                            <div>
                                <div class="card-title">Total Soal Teori</div>
                                <div class="card-value"><?php echo $total_soal_teori; ?></div>
                            </div>
                        </div>
                        <div class="card-actions">
                            <a href="bank_soal_teori.php" class="btn btn-primary btn-block">
                                <i class="fas fa-plus"></i> Tambah Soal Teori
                            </a>
                            <a href="list_soal_teori.php" class="btn btn-secondary btn-block">
                                <i class="fas fa-eye"></i> Lihat Soal Teori
                            </a>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header">
                            <div class="card-icon icon-praktik">
                                <i class="fas fa-clipboard-check"></i>
                            </div>
                            <div>
                                <div class="card-title">Total Soal Praktik</div>
                                <div class="card-value"><?php echo $total_soal_praktik; ?></div>
                            </div>
                        </div>
                        <div class="card-actions">
                            <a href="bank_soal_praktik.php" class="btn btn-primary btn-block">
                                <i class="fas fa-plus"></i> Tambah Soal Praktik
                            </a>
                            <a href="list_soal_praktik.php" class="btn btn-secondary btn-block">
                                <i class="fas fa-eye"></i> Lihat Soal Praktik
                            </a>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-header">
                            <div class="card-icon icon-total">
                                <i class="fas fa-database"></i>
                            </div>
                            <div>
                                <div class="card-title">Total Semua Soal</div>
                                <div class="card-value"><?php echo $total_soal_teori + $total_soal_praktik; ?></div>
                            </div>
                        </div>
                        <div class="card-actions">
                            <a href="soal.php" class="btn btn-primary btn-block">
                                <i class="fas fa-cog"></i> Kelola Semua Soal
                            </a>
                        </div>
                    </div>
                </div>

                <!-- Recent Activity -->
                <div class="dashboard-sections">
                    <!-- Soal Teori Terbaru -->
                    <div class="section">
                        <div class="section-header">
                            <h2><i class="fas fa-clock"></i> Soal Teori Terbaru</h2>
                            <a href="list_soal_teori.php" class="btn btn-sm btn-outline">Lihat Semua</a>
                        </div>
                        <div class="section-content">
                            <?php if (empty($soal_teori_terbaru)): ?>
                                <div class="empty-state">
                                    <i class="fas fa-inbox"></i>
                                    <p>Belum ada soal teori</p>
                                </div>
                            <?php else: ?>
                                <div class="recent-items">
                                    <?php foreach ($soal_teori_terbaru as $soal): ?>
                                        <div class="recent-item">
                                            <div class="recent-item-header">
                                                <h4><?php echo htmlspecialchars($soal['nomor_registrasi']); ?></h4>
                                                <span class="badge badge-teori">Teori</span>
                                            </div>
                                            <p class="recent-item-preview">
                                                <?php echo strip_tags(substr($soal['pertanyaan'], 0, 100)); ?>...
                                            </p>
                                            <div class="recent-item-meta">
                                                <span><i class="fas fa-calendar"></i> <?php echo date('d/m/Y', strtotime($soal['created_at'])); ?></span>
                                                <span><i class="fas fa-building"></i> <?php echo htmlspecialchars($soal['department'] ?? '-'); ?></span>
                                            </div>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <!-- Soal Praktik Terbaru -->
                    <div class="section">
                        <div class="section-header">
                            <h2><i class="fas fa-clock"></i> Soal Praktik Terbaru</h2>
                            <a href="list_soal_praktik.php" class="btn btn-sm btn-outline">Lihat Semua</a>
                        </div>
                        <div class="section-content">
                            <?php if (empty($soal_praktik_terbaru)): ?>
                                <div class="empty-state">
                                    <i class="fas fa-stethoscope"></i>
                                    <p>Belum ada soal praktik</p>
                                </div>
                            <?php else: ?>
                                <div class="recent-items">
                                    <?php foreach ($soal_praktik_terbaru as $soal): ?>
                                        <div class="recent-item">
                                            <div class="recent-item-header">
                                                <h4><?php echo htmlspecialchars($soal['nomor_registrasi']); ?></h4>
                                                <span class="badge badge-praktik">Praktik</span>
                                            </div>
                                            <p class="recent-item-preview">
                                                <?php echo strip_tags(substr($soal['skenario_klinik'], 0, 100)); ?>...
                                            </p>
                                            <div class="recent-item-meta">
                                                <span><i class="fas fa-calendar"></i> <?php echo date('d/m/Y', strtotime($soal['created_at'])); ?></span>
                                                <span><i class="fas fa-building"></i> <?php echo htmlspecialchars($soal['department'] ?? '-'); ?></span>
                                            </div>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Quick Actions 
                <div class="quick-actions">
                    <h2><i class="fas fa-bolt"></i> Quick Actions</h2>
                    <div class="action-buttons">
                        <a href="bank_soal_teori.php" class="action-btn">
                            <i class="fas fa-plus-circle"></i>
                            <span>Tambah Soal Teori</span>
                        </a>
                        <a href="bank_soal_praktik.php" class="action-btn">
                            <i class="fas fa-plus-circle"></i>
                            <span>Tambah Soal Praktik</span>
                        </a>
                        <a href="ujian.php" class="action-btn">
                            <i class="fas fa-clipboard-list"></i>
                            <span>Buat Ujian Baru</span>
                        </a>
                        <a href="peserta.php" class="action-btn">
                            <i class="fas fa-user-plus"></i>
                            <span>Tambah Peserta</span>
                        </a>
                        <a href="penguji.php" class="action-btn">
                            <i class="fas fa-user-check"></i>
                            <span>Kelola Penguji</span>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div> -->

    <script src="../assets/js/dashboard.js"></script>
</body>
</html>