<?php
include '../config.php';

if (!isSiswa()) {
    redirect('../login.php');
}

$user_id = $_SESSION['user_id'];

// Handle filter
$filter = isset($_GET['filter']) ? sanitize($_GET['filter']) : 'all';
$search = isset($_GET['search']) ? sanitize($_GET['search']) : '';

// Ambil sertifikat (kursus yang sudah selesai dengan progress 100%)
$sql_certificates = "SELECT c.*, e.progress, e.tanggal_enroll, e.tanggal_selesai,
                            u.nama_lengkap as nama_guru, cat.nama_kategori,
                            DATE_ADD(e.tanggal_enroll, INTERVAL c.durasi_jam HOUR) as estimated_completion
                     FROM lms_enrollments e 
                     JOIN lms_courses c ON e.id_course = c.id 
                     JOIN lms_users u ON c.id_guru = u.id 
                     JOIN lms_categories cat ON c.id_kategori = cat.id 
                     WHERE e.id_siswa = ? AND e.progress = 100 AND e.status = 'active'";

$params = [$user_id];
$types = "i";

// Apply filter
if ($filter === 'recent') {
    $sql_certificates .= " AND e.tanggal_selesai >= DATE_SUB(NOW(), INTERVAL 30 DAY)";
} elseif ($filter === 'popular') {
    $sql_certificates .= " AND c.total_siswa >= 100";
}

// Apply search
if (!empty($search)) {
    $sql_certificates .= " AND (c.judul LIKE ? OR u.nama_lengkap LIKE ? OR cat.nama_kategori LIKE ?)";
    $search_param = "%$search%";
    $params[] = $search_param;
    $params[] = $search_param;
    $params[] = $search_param;
    $types .= "sss";
}

$sql_certificates .= " ORDER BY e.tanggal_selesai DESC";

$stmt = $conn->prepare($sql_certificates);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result_certificates = $stmt->get_result();

// Hitung statistik
$sql_stats = "SELECT 
    COUNT(*) as total_certificates,
    COUNT(DISTINCT c.id_kategori) as total_lms_categories,
    AVG(c.rating) as avg_rating,
    SUM(c.durasi_jam) as total_study_hours
    FROM lms_enrollments e 
    JOIN lms_courses c ON e.id_course = c.id 
    WHERE e.id_siswa = ? AND e.progress = 100 AND e.status = 'active'";
$stmt_stats = $conn->prepare($sql_stats);
$stmt_stats->bind_param('i', $user_id);
$stmt_stats->execute();
$stats = $stmt_stats->get_result()->fetch_assoc();

// Ambil progress menuju sertifikat (kursus yang belum selesai)
$sql_in_progress = "SELECT c.*, e.progress, u.nama_lengkap as nama_guru,
                           cat.nama_kategori
                    FROM lms_enrollments e 
                    JOIN lms_courses c ON e.id_course = c.id 
                    JOIN lms_users u ON c.id_guru = u.id 
                    JOIN lms_categories cat ON c.id_kategori = cat.id 
                    WHERE e.id_siswa = ? AND e.progress < 100 AND e.status = 'active'
                    ORDER BY e.progress DESC 
                    LIMIT 3";
$stmt_progress = $conn->prepare($sql_in_progress);
$stmt_progress->bind_param('i', $user_id);
$stmt_progress->execute();
$result_progress = $stmt_progress->get_result();

// Handle generate certificate
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['generate_certificate'])) {
    $course_id = sanitize($_POST['course_id']);
    
    // Verifikasi bahwa kursus sudah selesai
    $sql_verify = "SELECT * FROM lms_enrollments 
                   WHERE id_siswa = ? AND id_course = ? AND progress = 100";
    $stmt_verify = $conn->prepare($sql_verify);
    $stmt_verify->bind_param('ii', $user_id, $course_id);
    $stmt_verify->execute();
    $result_verify = $stmt_verify->get_result();
    
    if ($result_verify->num_rows > 0) {
        // Update tanggal selesai jika belum ada
        $sql_update = "UPDATE lms_enrollments SET tanggal_selesai = NOW() 
                      WHERE id_siswa = ? AND id_course = ? AND tanggal_selesai IS NULL";
        $stmt_update = $conn->prepare($sql_update);
        $stmt_update->bind_param('ii', $user_id, $course_id);
        $stmt_update->execute();
        
        $_SESSION['success'] = "Sertifikat berhasil digenerate!";
        redirect("certificate_view.php?course_id=" . $course_id);
    } else {
        $_SESSION['error'] = "Kursus belum selesai!";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sertifikat - SMANSAB LMS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #3f51b5;
            --secondary-color: #f5f5f5;
            --gold-color: #ffd700;
        }
        
        .sidebar {
            background: var(--primary-color);
            color: white;
            min-height: 100vh;
            padding: 0;
        }
        
        .sidebar-sticky {
            position: sticky;
            top: 0;
            height: 100vh;
            padding-top: 20px;
            overflow-y: auto;
        }
        
        .sidebar .nav-link {
            color: rgba(255, 255, 255, 0.8);
            padding: 12px 20px;
            margin: 2px 0;
            border-radius: 0;
            transition: all 0.3s;
        }
        
        .sidebar .nav-link:hover {
            color: white;
            background: rgba(255, 255, 255, 0.1);
        }
        
        .sidebar .nav-link.active {
            color: white;
            background: rgba(255, 255, 255, 0.2);
            border-left: 4px solid #fff;
        }
        
        .sidebar .nav-link i {
            width: 20px;
            margin-right: 10px;
        }
        
        .main-content {
            background: #f8f9fa;
            min-height: 100vh;
        }
        
        .navbar-custom {
            background: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .stat-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
        }
        
        .certificate-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s;
            margin-bottom: 20px;
            background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
            border-left: 4px solid var(--gold-color);
        }
        
        .certificate-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 15px rgba(0,0,0,0.1);
        }
        
        .certificate-preview {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 10px;
            padding: 20px;
            color: white;
            text-align: center;
            margin-bottom: 15px;
        }
        
        .certificate-icon {
            font-size: 3rem;
            margin-bottom: 10px;
            color: var(--gold-color);
        }
        
        .progress-card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 15px;
        }
        
        .filter-btn {
            border: 1px solid #dee2e6;
            border-radius: 20px;
            padding: 8px 16px;
            margin: 0 5px 10px 0;
            transition: all 0.3s;
        }
        
        .filter-btn.active {
            background: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }
        
        .badge-certificate {
            background: linear-gradient(45deg, var(--gold-color), #ffed4e);
            color: #856404;
            font-weight: bold;
        }
        
        .achievement-badge {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 10px;
        }
        
        .bg-certificates {
            background: linear-gradient(45deg, #667eea, #764ba2);
        }
        
        .bg-lms_categories {
            background: linear-gradient(45deg, #f093fb, #f5576c);
        }
        
        .bg-rating {
            background: linear-gradient(45deg, #4facfe, #00f2fe);
        }
        
        .bg-hours {
            background: linear-gradient(45deg, #43e97b, #38f9d7);
        }
        
        .certificate-actions .btn {
            margin: 2px;
        }
        
        .empty-certificate {
            text-align: center;
            padding: 60px 20px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 sidebar">
                <div class="sidebar-sticky">
                    <div class="text-center mb-4">
                        <h4 class="fw-bold">
                            <i class="bi bi-book-half"></i><br>
                            SMANSAB LMS
                        </h4>
                        <small class="text-white-50">Siswa Dashboard</small>
                    </div>
                    
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="dashboard.php">
                                <i class="bi bi-speedometer2"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="my_courses.php">
                                <i class="bi bi-journal-bookmark"></i> Kursus Saya
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="progress.php">
                                <i class="bi bi-graph-up"></i> Progress Belajar
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="quizzes.php">
                                <i class="bi bi-pencil-square"></i> Kuis & Ujian
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="certificates.php">
                                <i class="bi bi-award"></i> Sertifikat
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="profile.php">
                                <i class="bi bi-person"></i> Profil Saya
                            </a>
                        </li>
                        <li class="nav-item mt-4">
                            <a class="nav-link" href="../logout.php">
                                <i class="bi bi-box-arrow-right"></i> Logout
                            </a>
                        </li>
                    </ul>
                    
                    <div class="mt-5 px-3">
                        <small class="text-white-50">
                            <i class="bi bi-info-circle"></i> Butuh bantuan?
                            <a href="#" class="text-white">Hubungi Admin</a>
                        </small>
                    </div>
                </div>
            </div>

            <!-- Main Content -->
            <div class="col-md-9 col-lg-10 main-content">
                <!-- Top Navbar -->
                <nav class="navbar navbar-custom">
                    <div class="container-fluid">
                        <div class="d-flex align-items-center">
                            <button class="btn btn-outline-primary me-3 d-md-none" type="button">
                                <i class="bi bi-list"></i>
                            </button>
                            <h5 class="mb-0">Sertifikat Saya</h5>
                        </div>
                        <div class="d-flex align-items-center">
                            <div class="dropdown">
                                <button class="btn btn-outline-primary dropdown-toggle" type="button" 
                                        data-bs-toggle="dropdown">
                                    <i class="bi bi-person-circle me-2"></i>
                                    <?php echo $_SESSION['nama_lengkap']; ?>
                                </button>
                                <ul class="dropdown-menu">
                                    <li><a class="dropdown-item" href="profile.php">
                                        <i class="bi bi-person me-2"></i>Profil
                                    </a></li>
                                    <li><a class="dropdown-item" href="../courses.php">
                                        <i class="bi bi-search me-2"></i>Jelajah Kursus
                                    </a></li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li><a class="dropdown-item" href="../logout.php">
                                        <i class="bi bi-box-arrow-right me-2"></i>Logout
                                    </a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </nav>

                <div class="container-fluid mt-4">
                    <!-- Header Section -->
                    <div class="row mb-4">
                        <div class="col-12">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <h4 class="fw-bold">Sertifikat Penyelesaian</h4>
                                    <p class="text-muted mb-0">Kelola dan unduh sertifikat kursus yang telah diselesaikan</p>
                                </div>
                                <div class="dropdown">
                                    <button class="btn btn-outline-primary dropdown-toggle" type="button" 
                                            data-bs-toggle="dropdown">
                                        <i class="bi bi-download me-2"></i>Export Semua
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li><a class="dropdown-item" href="#"><i class="bi bi-file-pdf me-2"></i>PDF Bundle</a></li>
                                        <li><a class="dropdown-item" href="#"><i class="bi bi-file-zip me-2"></i>ZIP Archive</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Statistics Cards -->
                    <div class="row mb-4">
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-certificates">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Total Sertifikat</h6>
                                            <h2 class="fw-bold"><?php echo $stats['total_certificates']; ?></h2>
                                            <small>Kursus diselesaikan</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-award" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-lms_categories">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Kategori</h6>
                                            <h2 class="fw-bold"><?php echo $stats['total_lms_categories']; ?></h2>
                                            <small>Bidang keahlian</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-tags" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-rating">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Rating Rata-rata</h6>
                                            <h2 class="fw-bold"><?php echo number_format($stats['avg_rating'] ?? 0, 1); ?></h2>
                                            <small>Kualitas kursus</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-star" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-hours">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Jam Belajar</h6>
                                            <h2 class="fw-bold"><?php echo $stats['total_study_hours'] ?? 0; ?>h</h2>
                                            <small>Total pembelajaran</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-clock" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Filter dan Search -->
                    <div class="card border-0 shadow-sm mb-4">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <h6 class="mb-3">Filter Sertifikat:</h6>
                                    <div class="d-flex flex-wrap">
                                        <a href="?filter=all<?php echo !empty($search) ? '&search=' . urlencode($search) : ''; ?>" 
                                           class="filter-btn <?php echo $filter === 'all' ? 'active' : ''; ?>">
                                            Semua Sertifikat
                                        </a>
                                        <a href="?filter=recent<?php echo !empty($search) ? '&search=' . urlencode($search) : ''; ?>" 
                                           class="filter-btn <?php echo $filter === 'recent' ? 'active' : ''; ?>">
                                            Terbaru (30 hari)
                                        </a>
                                        <a href="?filter=popular<?php echo !empty($search) ? '&search=' . urlencode($search) : ''; ?>" 
                                           class="filter-btn <?php echo $filter === 'popular' ? 'active' : ''; ?>">
                                            Populer
                                        </a>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <h6 class="mb-3">Cari Sertifikat:</h6>
                                    <form method="GET" class="d-flex">
                                        <input type="text" name="search" class="form-control me-2" 
                                               placeholder="Cari kursus, guru, atau kategori..." 
                                               value="<?php echo htmlspecialchars($search); ?>">
                                        <button type="submit" class="btn btn-primary">
                                            <i class="bi bi-search"></i>
                                        </button>
                                        <?php if (!empty($filter) && $filter !== 'all'): ?>
                                            <input type="hidden" name="filter" value="<?php echo $filter; ?>">
                                        <?php endif; ?>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <!-- Daftar Sertifikat -->
                        <div class="col-lg-8 mb-4">
                            <?php if ($result_certificates->num_rows > 0): ?>
                                <div class="row">
                                    <?php while($certificate = $result_certificates->fetch_assoc()): 
                                        $completion_date = $certificate['tanggal_selesai'] ?: $certificate['estimated_completion'];
                                    ?>
                                        <div class="col-lg-6 mb-4">
                                            <div class="certificate-card card">
                                                <div class="card-body">
                                                    <div class="certificate-preview">
                                                        <i class="bi bi-award certificate-icon"></i>
                                                        <h6 class="mb-0">Sertifikat Penyelesaian</h6>
                                                        <small>SMANSAB LMS</small>
                                                    </div>
                                                    
                                                    <h6 class="card-title fw-bold"><?php echo $certificate['judul']; ?></h6>
                                                    <p class="card-text small text-muted mb-2">
                                                        <i class="bi bi-person"></i> Oleh: <?php echo $certificate['nama_guru']; ?>
                                                    </p>
                                                    <p class="card-text small text-muted mb-3">
                                                        <i class="bi bi-tag"></i> <?php echo $certificate['nama_kategori']; ?>
                                                    </p>
                                                    
                                                    <div class="row text-center mb-3">
                                                        <div class="col-4">
                                                            <small class="text-muted">Durasi</small>
                                                            <h6 class="fw-bold"><?php echo $certificate['durasi_jam']; ?>h</h6>
                                                        </div>
                                                        <div class="col-4">
                                                            <small class="text-muted">Rating</small>
                                                            <h6 class="fw-bold"><?php echo $certificate['rating']; ?></h6>
                                                        </div>
                                                        <div class="col-4">
                                                            <small class="text-muted">Selesai</small>
                                                            <h6 class="fw-bold"><?php echo date('d M Y', strtotime($completion_date)); ?></h6>
                                                        </div>
                                                    </div>
                                                    
                                                    <div class="certificate-actions d-grid gap-2">
                                                        <form method="POST" class="d-inline">
                                                            <input type="hidden" name="course_id" value="<?php echo $certificate['id']; ?>">
                                                            <button type="submit" name="generate_certificate" 
                                                                    class="btn btn-primary btn-sm w-100">
                                                                <i class="bi bi-download me-1"></i>Unduh Sertifikat
                                                            </button>
                                                        </form>
                                                        <div class="btn-group w-100" role="group">
                                                            <a href="certificate_view.php?course_id=<?php echo $certificate['id']; ?>" 
                                                               class="btn btn-outline-primary btn-sm">
                                                                <i class="bi bi-eye"></i>
                                                            </a>
                                                            <button type="button" class="btn btn-outline-success btn-sm" 
                                                                    onclick="shareCertificate(<?php echo $certificate['id']; ?>, '<?php echo $certificate['judul']; ?>')">
                                                                <i class="bi bi-share"></i>
                                                            </button>
                                                            <button type="button" class="btn btn-outline-info btn-sm"
                                                                    data-bs-toggle="modal" 
                                                                    data-bs-target="#certificateModal"
                                                                    data-course-title="<?php echo htmlspecialchars($certificate['judul']); ?>"
                                                                    data-instructor="<?php echo htmlspecialchars($certificate['nama_guru']); ?>"
                                                                    data-completion-date="<?php echo date('d F Y', strtotime($completion_date)); ?>"
                                                                    data-duration="<?php echo $certificate['durasi_jam']; ?> jam">
                                                                <i class="bi bi-info-circle"></i>
                                                            </button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    <?php endwhile; ?>
                                </div>
                            <?php else: ?>
                                <div class="empty-certificate">
                                    <i class="bi bi-award text-muted" style="font-size: 4rem;"></i>
                                    <h4 class="text-muted mt-3">Belum Ada Sertifikat</h4>
                                    <p class="text-muted mb-4">
                                        <?php if (!empty($search) || $filter !== 'all'): ?>
                                            Tidak ada sertifikat yang sesuai dengan pencarian Anda
                                        <?php else: ?>
                                            Selesaikan kursus untuk mendapatkan sertifikat pertama Anda!
                                        <?php endif; ?>
                                    </p>
                                    <div class="d-grid gap-2 d-md-flex justify-content-center">
                                        <a href="my_courses.php" class="btn btn-primary">
                                            <i class="bi bi-journal-bookmark me-2"></i>Lanjutkan Belajar
                                        </a>
                                        <a href="../courses.php" class="btn btn-outline-primary">
                                            <i class="bi bi-search me-2"></i>Jelajah Kursus Baru
                                        </a>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>

                        <!-- Sidebar -->
                        <div class="col-lg-4 mb-4">
                            <!-- Progress Menuju Sertifikat -->
                            <div class="card border-0 shadow-sm mb-4">
                                <div class="card-header bg-white border-0">
                                    <h6 class="mb-0">
                                        <i class="bi bi-rocket-takeoff me-2"></i>
                                        Menuju Sertifikat Berikutnya
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <?php if ($result_progress->num_rows > 0): ?>
                                        <?php while($course = $result_progress->fetch_assoc()): ?>
                                            <div class="progress-card card mb-3">
                                                <div class="card-body">
                                                    <h6 class="card-title small"><?php echo $course['judul']; ?></h6>
                                                    <div class="d-flex justify-content-between mb-2">
                                                        <small class="text-muted">Progress</small>
                                                        <small class="text-muted"><?php echo $course['progress']; ?>%</small>
                                                    </div>
                                                    <div class="progress" style="height: 6px;">
                                                        <div class="progress-bar" style="width: <?php echo $course['progress']; ?>%"></div>
                                                    </div>
                                                    <div class="text-center mt-2">
                                                        <a href="course_learn.php?id=<?php echo $course['id']; ?>" 
                                                           class="btn btn-outline-primary btn-sm w-100">
                                                            <i class="bi bi-play-circle me-1"></i>Lanjutkan
                                                        </a>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <div class="text-center py-3">
                                            <i class="bi bi-check-circle text-success" style="font-size: 2rem;"></i>
                                            <p class="text-muted mt-2 small">Semua kursus sudah selesai!</p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <!-- Pencapaian -->
                            <div class="card border-0 shadow-sm mb-4">
                                <div class="card-header bg-white border-0">
                                    <h6 class="mb-0">
                                        <i class="bi bi-trophy me-2"></i>
                                        Pencapaian Sertifikat
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <div class="row text-center">
                                        <div class="col-4">
                                            <div class="achievement-badge bg-warning">
                                                <i class="bi bi-award text-white"></i>
                                            </div>
                                            <small>Pembelajar</small>
                                            <h6 class="fw-bold mb-0">1+</h6>
                                        </div>
                                        <div class="col-4">
                                            <div class="achievement-badge bg-success">
                                                <i class="bi bi-stars text-white"></i>
                                            </div>
                                            <small>Ahli</small>
                                            <h6 class="fw-bold mb-0">5+</h6>
                                        </div>
                                        <div class="col-4">
                                            <div class="achievement-badge bg-danger">
                                                <i class="bi bi-trophy text-white"></i>
                                            </div>
                                            <small>Master</small>
                                            <h6 class="fw-bold mb-0">10+</h6>
                                        </div>
                                    </div>
                                    <div class="progress mt-3" style="height: 8px;">
                                        <div class="progress-bar" 
                                             style="width: <?php echo min(100, ($stats['total_certificates'] / 10) * 100); ?>%">
                                        </div>
                                    </div>
                                    <small class="text-muted text-center d-block mt-2">
                                        <?php echo $stats['total_certificates']; ?> dari 10 sertifikat menuju Master
                                    </small>
                                </div>
                            </div>

                            <!-- Tips -->
                            <div class="card border-0 shadow-sm">
                                <div class="card-header bg-white border-0">
                                    <h6 class="mb-0">
                                        <i class="bi bi-lightbulb me-2"></i>
                                        Tips Sertifikat
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <div class="alert alert-info alert-dismissible fade show mb-3" role="alert">
                                        <small>
                                            <strong>Bagikan di LinkedIn!</strong> Tambahkan sertifikat ke profil profesional Anda.
                                        </small>
                                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                                    </div>
                                    <div class="alert alert-warning alert-dismissible fade show mb-3" role="alert">
                                        <small>
                                            <strong>Simpan backup!</strong> Unduh dan simpan salinan sertifikat Anda.
                                        </small>
                                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                                    </div>
                                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                                        <small>
                                            <strong>Lanjutkan belajar!</strong> Setiap sertifikat membuka peluang baru.
                                        </small>
                                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Certificate Detail Modal -->
    <div class="modal fade" id="certificateModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Detail Sertifikat</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div id="certificateDetails">
                        <!-- Details will be populated by JavaScript -->
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Certificate detail modal
        document.addEventListener('DOMContentLoaded', function() {
            const certificateModal = document.getElementById('certificateModal');
            if (certificateModal) {
                certificateModal.addEventListener('show.bs.modal', function(event) {
                    const button = event.relatedTarget;
                    const courseTitle = button.getAttribute('data-course-title');
                    const instructor = button.getAttribute('data-instructor');
                    const completionDate = button.getAttribute('data-completion-date');
                    const duration = button.getAttribute('data-duration');
                    
                    const details = `
                        <div class="row">
                            <div class="col-12">
                                <h6>Kursus: ${courseTitle}</h6>
                                <p><strong>Instruktur:</strong> ${instructor}</p>
                                <p><strong>Tanggal Penyelesaian:</strong> ${completionDate}</p>
                                <p><strong>Durasi Belajar:</strong> ${duration}</p>
                                <p><strong>Status:</strong> <span class="badge badge-certificate">SELESAI</span></p>
                                <hr>
                                <p class="small text-muted">
                                    Sertifikat ini menyatakan bahwa pemegangnya telah berhasil menyelesaikan 
                                    kursus dengan memenuhi semua persyaratan yang ditetapkan.
                                </p>
                            </div>
                        </div>
                    `;
                    
                    document.getElementById('certificateDetails').innerHTML = details;
                });
            }
        });
        
        // Share certificate function
        function shareCertificate(courseId, courseTitle) {
            if (navigator.share) {
                navigator.share({
                    title: 'Sertifikat Penyelesaian: ' + courseTitle,
                    text: 'Saya telah menyelesaikan kursus ' + courseTitle + ' di SMANSAB LMS',
                    url: window.location.origin + '/siswa/certificate_view.php?course_id=' + courseId
                })
                .then(() => console.log('Berbagi berhasil'))
                .catch((error) => console.log('Error sharing', error));
            } else {
                // Fallback untuk browser yang tidak support Web Share API
                const shareUrl = window.location.origin + '/siswa/certificate_view.php?course_id=' + courseId;
                navigator.clipboard.writeText(shareUrl).then(() => {
                    alert('Link sertifikat berhasil disalin ke clipboard!');
                });
            }
        }
        
        // Konfirmasi sebelum generate certificate
        document.addEventListener('DOMContentLoaded', function() {
            const generateButtons = document.querySelectorAll('button[name="generate_certificate"]');
            generateButtons.forEach(button => {
                button.addEventListener('click', function(e) {
                    const courseTitle = this.closest('.certificate-card').querySelector('.card-title').textContent;
                    if (!confirm(`Generate sertifikat untuk:\n"${courseTitle}"?`)) {
                        e.preventDefault();
                    }
                });
            });
        });
    </script>
</body>
</html>