<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: login.php");
    exit();
}
require 'config/db.php';
$courses_result = $conn->query("SELECT * FROM courses ORDER BY upload_date DESC");
$page_title = "Admin LMS";
include 'templates/header.php';
?>

<main class="bg-gray-100 pt-16">
    <div class="bg-gray-800 text-white py-8">
        <div class="container mx-auto px-4 flex justify-between items-center">
            <div>
                <h1 class="text-3xl font-bold">LMS Dashboard</h1>
                <p class="mt-1 text-gray-300">Selamat datang, <?php echo htmlspecialchars($_SESSION['username']); ?>!</p>
            </div>
            <a href="logout.php" class="bg-red-500 hover:bg-red-600 text-white font-bold py-2 px-4 rounded-md transition duration-300">
                <i class="fas fa-sign-out-alt mr-2"></i>Logout
            </a>
        </div>
    </div>
    <div class="container mx-auto px-4 py-10">
        <div class="grid lg:grid-cols-3 gap-8">
            <div class="lg:col-span-1">
                <div class="bg-white p-6 rounded-lg shadow-md">
                    <h2 class="text-2xl font-semibold mb-4">Unggah Materi Baru</h2>
                    
                    <form action="upload_handler.php" method="POST" enctype="multipart/form-data">

                        <div class="mb-4">
                            <label for="title" class="block text-sm font-medium text-gray-700 mb-1">Judul Materi</label>
                            <input type="text" name="title" id="title" required class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                        </div>
                        <div class="mb-4">
                            <label for="description" class="block text-sm font-medium text-gray-700 mb-1">Deskripsi Singkat</label>
                            <textarea name="description" id="description" rows="3" class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"></textarea>
                        </div>
                        <div class="mb-6">
                            <label for="scorm_file" class="block text-sm font-medium text-gray-700 mb-1">File SCORM (.zip)</label>
                            <input type="file" name="scorm_file" id="scorm_file" required class="w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100" accept=".zip">
                        </div>
                        <button type="submit" class="w-full bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 text-white font-bold py-3 px-4 rounded-md transition duration-300">
                            Unggah Materi
                        </button>
                    </form>
                </div>
            </div>
            <div class="lg:col-span-2">
                <div class="bg-white p-6 rounded-lg shadow-md">
                    <h2 class="text-2xl font-semibold mb-4">Daftar Materi Terunggah</h2>
                    <div class="space-y-4">
                        <?php if ($courses_result && $courses_result->num_rows > 0): ?>
                            <?php while($course = $courses_result->fetch_assoc()): ?>
                                <div class="border p-4 rounded-md flex justify-between items-center">
                                    <div>
                                        <h4 class="font-bold text-lg"><?php echo htmlspecialchars($course['title']); ?></h4>
                                        <p class="text-sm text-gray-500"><i class="fas fa-file-archive text-blue-500"></i> <?php echo htmlspecialchars($course['scorm_filename']); ?></p>
                                    </div>
                                    <div class="flex items-center space-x-3">
                                        <a href="play.php?id=<?php echo $course['id']; ?>" target="_blank" class="text-green-500 hover:text-green-700" title="Jalankan Materi">
                                            <i class="fas fa-play-circle fa-lg"></i>
                                        </a>
                                        <button class="text-red-500 hover:text-red-700" title="Hapus">
                                            <i class="fas fa-trash fa-lg"></i>
                                        </button>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <p class="text-center text-gray-500 py-4">Belum ada materi yang diunggah.</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

<?php
include 'templates/footer.php';
$conn->close();
?>