<?php
session_start();
include "../config/koneksi.php";

if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit;
}

// UPLOAD
if(isset($_POST['upload'])){
    $nama = $_FILES['file']['name'];
    $tmp  = $_FILES['file']['tmp_name'];
    $ext  = strtolower(pathinfo($nama, PATHINFO_EXTENSION));

    if($ext == "pdf"){
        move_uploaded_file($tmp, "../modul/".$nama);
        mysqli_query($conn,"INSERT INTO modul (nama_file,nama_modul) 
        VALUES ('$nama','$_POST[nama_modul]')");
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Kelola Modul</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
<div class="container mt-5">

<h3>Kelola Modul</h3>

<!-- FORM -->
<form method="POST" enctype="multipart/form-data" class="mb-4">
<input type="text" name="nama_modul" class="form-control mb-2" placeholder="Nama Modul" required>
<input type="file" name="file" class="form-control mb-2" required>
<button name="upload" class="btn btn-primary">Upload</button>
</form>

<!-- DATA -->
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Nama Modul</th>
<th>Aksi</th>
</tr>

<?php
$no=1;
$data=mysqli_query($conn,"SELECT * FROM modul ORDER BY id DESC");
while($d=mysqli_fetch_array($data)){
?>

<tr>
<td><?= $no++ ?></td>
<td><?= $d['nama_modul'] ?></td>
<td>
    <a href="../modul/<?= $d['nama_file'] ?>" class="btn btn-success btn-sm">Download</a>
    <a href="hapus_modul.php?id=<?= $d['id'] ?>" class="btn btn-danger btn-sm">Hapus</a>
</td>
</tr>

<?php } ?>

</table>

</div>
</body>
</html>