<?php include "../config/koneksi.php"; ?>

<h2>Kelola Modul</h2>

<form method="POST" enctype="multipart/form-data">
    <input type="text" name="nama_modul" placeholder="Nama Modul" required>
    <input type="file" name="file" required>
    <button type="submit" name="upload">Upload</button>
</form>

<hr>

<table class="table">
<tr>
    <th>No</th>
    <th>Nama Modul</th>
    <th>Aksi</th>
</tr>

<?php
$no=1;
$data = mysqli_query($koneksi, "SELECT * FROM modul");
while($d = mysqli_fetch_array($data)){
?>
<tr>
    <td><?= $no++ ?></td>
    <td><?= $d['nama_modul'] ?></td>
    <td>
        <a href="../modul/<?= $d['file'] ?>" target="_blank" class="btn btn-info btn-sm">Lihat</a>
        <a href="hapus_modul.php?id=<?= $d['id'] ?>" class="btn btn-danger btn-sm">Hapus</a>
    </td>
</tr>
<?php } ?>
</table>

<?php
if(isset($_POST['upload'])){
    $nama = $_FILES['file']['name'];
    $tmp = $_FILES['file']['tmp_name'];

    move_uploaded_file($tmp, "../modul/".$nama);

    mysqli_query($koneksi, "INSERT INTO modul VALUES('', '$_POST[nama_modul]', '$nama')");

    echo "<script>location='kelola_modul.php'</script>";
}
?>