<?php
session_start();
// Pastikan hanya admin yang bisa akses
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    die("Akses ditolak. Silakan login terlebih dahulu.");
}

require 'config/db.php';

// Validasi ID course
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("ID Course tidak valid.");
}
$course_id = intval($_GET['id']);

// Ambil data course dari DB
$stmt = $conn->prepare("SELECT title, launch_url FROM courses WHERE id = ?");
$stmt->bind_param("i", $course_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows !== 1) {
    die("Course tidak ditemukan.");
}

$course = $result->fetch_assoc();
$launch_url = $course['launch_url'];
$course_title = $course['title'];

// Cek apakah file launch benar-benar ada
if (!file_exists($launch_url)) {
    die("Error: File 'launch' tidak ditemukan di server: " . $launch_url);
}

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Playing: <?php echo htmlspecialchars($course_title); ?></title>
    <style>
        body, html { margin: 0; padding: 0; height: 100%; overflow: hidden; }
        iframe { border: none; width: 100%; height: 100%; }
    </style>
</head>
<body>

    <iframe src="<?php echo htmlspecialchars($launch_url); ?>"></iframe>

    <script>
        // Membuat object API palsu di window utama
        const API = {
            LMSInitialize: function(param) {
                console.log("SCORM: LMSInitialize - Parameter:", param);
                return "true";
            },
            LMSFinish: function(param) {
                console.log("SCORM: LMSFinish - Parameter:", param);
                return "true";
            },
            LMSGetValue: function(param) {
                console.log("SCORM: LMSGetValue - Meminta:", param);
                // Kita bisa memalsukan data balikan jika perlu
                if (param === 'cmi.core.student_name') {
                    return "<?php echo htmlspecialchars($_SESSION['username']); ?>";
                }
                return "";
            },
            LMSSetValue: function(param, value) {
                console.log(`SCORM: LMSSetValue - Mengatur '${param}' menjadi '${value}'`);
                return "true";
            },
            LMSCommit: function(param) {
                console.log("SCORM: LMSCommit - Parameter:", param);
                return "true";
            },
            LMSGetLastError: function() {
                console.log("SCORM: LMSGetLastError");
                return "0";
            },
            LMSGetErrorString: function(errorCode) {
                console.log("SCORM: LMSGetErrorString - Kode:", errorCode);
                return "No error";
            },
            LMSGetDiagnostic: function(errorCode) {
                console.log("SCORM: LMSGetDiagnostic - Kode:", errorCode);
                return "No diagnostic information";
            }
        };

        // Juga dikenal sebagai API_1484_11 untuk SCORM 2004
        const API_1484_11 = API;

        console.log("Fake SCORM API berhasil dimuat.");
    </script>

</body>
</html>