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

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

$user_id = $_SESSION['user_id'];
$course_id = isset($_GET['course_id']) ? sanitize($_GET['course_id']) : null;

if (!$course_id) {
    $_SESSION['error'] = "Course ID tidak valid!";
    redirect('certificates.php');
}

// Ambil data sertifikat
$sql_certificate = "SELECT c.*, u.nama_lengkap as nama_guru, us.nama_lengkap as nama_siswa,
                           e.tanggal_selesai, e.tanggal_enroll, 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_users us ON e.id_siswa = us.id 
                    JOIN lms_categories cat ON c.id_kategori = cat.id 
                    WHERE e.id_siswa = ? AND e.id_course = ? AND e.progress = 100";
$stmt = $conn->prepare($sql_certificate);
$stmt->bind_param('ii', $user_id, $course_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 0) {
    $_SESSION['error'] = "Sertifikat tidak ditemukan atau kursus belum selesai!";
    redirect('certificates.php');
}

$certificate = $result->fetch_assoc();
$completion_date = $certificate['tanggal_selesai'] ?: date('Y-m-d H:i:s');
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sertifikat - <?php echo $certificate['judul']; ?> - SMANSAB LMS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .certificate-container {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 40px 20px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .certificate {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            max-width: 800px;
            margin: 0 auto;
            border: 10px solid #ffd700;
            position: relative;
        }
        
        .certificate-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .certificate-title {
            font-size: 2.5rem;
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 10px;
        }
        
        .certificate-subtitle {
            font-size: 1.2rem;
            color: #7f8c8d;
            margin-bottom: 20px;
        }
        
        .certificate-body {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .certificate-student {
            font-size: 2rem;
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 10px;
        }
        
        .certificate-course {
            font-size: 1.5rem;
            color: #34495e;
            margin-bottom: 20px;
        }
        
        .certificate-details {
            display: flex;
            justify-content: space-around;
            margin: 30px 0;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 10px;
        }
        
        .detail-item {
            text-align: center;
        }
        
        .detail-label {
            font-size: 0.9rem;
            color: #7f8c8d;
            margin-bottom: 5px;
        }
        
        .detail-value {
            font-size: 1.1rem;
            font-weight: bold;
            color: #2c3e50;
        }
        
        .certificate-footer {
            text-align: center;
            margin-top: 40px;
            padding-top: 20px;
            border-top: 2px solid #ecf0f1;
        }
        
        .signature-area {
            display: flex;
            justify-content: space-around;
            margin-top: 30px;
        }
        
        .signature {
            text-align: center;
        }
        
        .signature-line {
            width: 200px;
            border-top: 1px solid #2c3e50;
            margin: 40px auto 10px;
        }
        
        .certificate-actions {
            margin-top: 30px;
            text-align: center;
        }
        
        .watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-45deg);
            font-size: 6rem;
            color: rgba(0,0,0,0.05);
            font-weight: bold;
            pointer-events: none;
            z-index: 1;
        }
    </style>
</head>
<body>
    <div class="certificate-container">
        <div class="certificate">
            <div class="watermark">SMANSAB LMS</div>
            
            <div class="certificate-header">
                <h1 class="certificate-title">SERTIFIKAT PENYELESAIAN</h1>
                <p class="certificate-subtitle">Diberikan kepada</p>
            </div>
            
            <div class="certificate-body">
                <h2 class="certificate-student"><?php echo $certificate['nama_siswa']; ?></h2>
                <p class="certificate-course"><?php echo $certificate['judul']; ?></p>
                <p>telah berhasil menyelesaikan kursus dengan memenuhi semua persyaratan yang ditetapkan.</p>
            </div>
            
            <div class="certificate-details">
                <div class="detail-item">
                    <div class="detail-label">Instruktur</div>
                    <div class="detail-value"><?php echo $certificate['nama_guru']; ?></div>
                </div>
                <div class="detail-item">
                    <div class="detail-label">Kategori</div>
                    <div class="detail-value"><?php echo $certificate['nama_kategori']; ?></div>
                </div>
                <div class="detail-item">
                    <div class="detail-label">Durasi</div>
                    <div class="detail-value"><?php echo $certificate['durasi_jam']; ?> jam</div>
                </div>
            </div>
            
            <div class="certificate-footer">
                <p>Diselesaikan pada: <?php echo date('d F Y', strtotime($completion_date)); ?></p>
                
                <div class="signature-area">
                    <div class="signature">
                        <div class="signature-line"></div>
                        <p><?php echo $certificate['nama_guru']; ?></p>
                        <small>Instruktur</small>
                    </div>
                    <div class="signature">
                        <div class="signature-line"></div>
                        <p>SMANSAB LMS</p>
                        <small>Lembaga Pendidikan</small>
                    </div>
                </div>
                
                <div class="certificate-actions mt-4">
                    <button onclick="window.print()" class="btn btn-primary me-2">
                        <i class="bi bi-printer"></i> Cetak Sertifikat
                    </button>
                    <a href="certificates.php" class="btn btn-outline-primary">
                        <i class="bi bi-arrow-left"></i> Kembali
                    </a>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Auto print option
        const urlParams = new URLSearchParams(window.location.search);
        if (urlParams.get('print') === 'true') {
            window.print();
        }
    </script>
</body>
</html>