<?php
session_start();
include 'koneksi.php'; // Koneksi ke database

// Validasi agar hanya mahasiswa yang dapat mengakses halaman ini
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'mahasiswa') {
    header("Location: index.php");
    exit;
}

// Ambil ID Mahasiswa dari sesi
$id_mahasiswa = $_SESSION['id_mahasiswa'];

// Proses form submit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id_dosen = $_POST['id_dosen'];
    $tanggal_bimbingan = $_POST['tanggal_bimbingan'];
    $topik_bimbingan = $_POST['topik_bimbingan'];
    $catatan = $_POST['catatan'];
    $status = $_POST['status'];

    // Query untuk menambah bimbingan
    $query = "
        INSERT INTO tbl_bimbingan (id_mahasiswa, id_dosen, tanggal_bimbingan, topik_bimbingan, catatan, status)
        VALUES ('$id_mahasiswa', '$id_dosen', '$tanggal_bimbingan', '$topik_bimbingan', '$catatan', '$status')
    ";

    $result = mysqli_query($conn, $query);

    if ($result) {
        header("Location: bimbingan.php");
        exit;
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}

// Query untuk mengambil data dosen
$query_dosen = "SELECT * FROM tbl_dosen";
$result_dosen = mysqli_query($conn, $query_dosen);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tambah Bimbingan</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- Header -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Bimbingan Akademik</a>
    </nav>

    <!-- Container -->
    <div class="container mt-4">
        <h3 class="text-center mb-4">Tambah Bimbingan</h3>
        <form method="POST">
            <div class="form-group">
                <label for="id_dosen">Dosen Pembimbing</label>
                <select name="id_dosen" id="id_dosen" class="form-control" required>
                    <option value="">Pilih Dosen</option>
                    <?php while ($row_dosen = mysqli_fetch_assoc($result_dosen)): ?>
                        <option value="<?= $row_dosen['id_dosen'] ?>"><?= $row_dosen['nama_dosen'] ?></option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="form-group">
                <label for="tanggal_bimbingan">Tanggal Bimbingan</label>
                <input type="date" name="tanggal_bimbingan" id="tanggal_bimbingan" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="topik_bimbingan">Topik Bimbingan</label>
                <input type="text" name="topik_bimbingan" id="topik_bimbingan" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="catatan">Catatan</label>
                <textarea name="catatan" id="catatan" class="form-control" rows="4"></textarea>
            </div>
            <div class="form-group">
                <label for="status">Status</label>
                <select name="status" id="status" class="form-control" required>
                    <option value="belum">Belum</option>
                    <option value="selesai">Selesai</option>
                </select>
            </div>
            <div class="text-center mt-3">
                <button type="submit" class="btn btn-primary">Simpan Bimbingan</button>
                <a href="bimbingan.php" class="btn btn-secondary">Kembali</a>
            </div>
        </form>
    </div>
</body>
</html>
