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

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

if(isset($_POST['simpan'])){
    mysqli_query($conn,"INSERT INTO jadwal 
    (nama_kelas,tanggal,durasi,kuota,status)
    VALUES(
        '$_POST[nama]',
        '$_POST[tanggal]',
        '$_POST[durasi]',
        '$_POST[kuota]',
        '$_POST[status]'
    )");
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Kelola Jadwal</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>Tambah Jadwal</h3>

<form method="POST">
<input type="text" name="nama" class="form-control mb-2" placeholder="Nama Kelas" required>
<input type="date" name="tanggal" class="form-control mb-2" required>
<input type="text" name="durasi" class="form-control mb-2" placeholder="Durasi" required>
<input type="number" name="kuota" class="form-control mb-2" placeholder="Kuota" required>
<input type="text" name="status" class="form-control mb-2" placeholder="Status" required>

<button name="simpan" class="btn btn-primary">Simpan</button>
</form>

<hr>

<h4>Data Jadwal</h4>

<table class="table table-bordered">
<tr>
<th>No</th>
<th>Kelas</th>
<th>Tanggal</th>
<th>Aksi</th>
</tr>

<?php
$no=1;
$data=mysqli_query($conn,"SELECT * FROM jadwal");
while($d=mysqli_fetch_array($data)){
?>

<tr>
<td><?= $no++ ?></td>
<td><?= $d['nama_kelas'] ?></td>
<td><?= $d['tanggal'] ?></td>
<td>
    <a href="?hapus=<?= $d['id'] ?>" class="btn btn-danger btn-sm">Hapus</a>
</td>
</tr>

<?php } ?>

</table>

<?php
if(isset($_GET['hapus'])){
    mysqli_query($conn,"DELETE FROM jadwal WHERE id='$_GET[hapus]'");
    header("Location: tambah_jadwal.php");
}
?>

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