<?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'];

// Query untuk mengambil data bimbingan
$query = "
    SELECT b.id_bimbingan, b.tanggal_bimbingan, b.topik_bimbingan, b.catatan, b.status, d.nama_dosen
    FROM tbl_bimbingan b
    JOIN tbl_dosen d ON b.id_dosen = d.id_dosen
    WHERE b.id_mahasiswa = '$id_mahasiswa'
    ORDER BY b.tanggal_bimbingan DESC
";

$result = mysqli_query($conn, $query);
if (!$result) {
    die("Query Error: " . mysqli_error($conn));
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bimbingan Mahasiswa</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* Kustomisasi tampilan tabel agar lebih responsif */
        .table-responsive {
            overflow-x: auto;
        }
        .table td, .table th {
            text-align: center;
        }
    </style>
</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">Daftar Bimbingan Anda</h3>

        <!-- Tabel Responsif -->
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Tanggal Bimbingan</th>
                        <th>Topik Bimbingan</th>
                        <th>Catatan</th>
                        <th>Dosen Pembimbing</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if (mysqli_num_rows($result) > 0) {
                        $no = 1;
                        while ($row = mysqli_fetch_assoc($result)) {
                            echo "<tr>";
                            echo "<td>{$no}</td>";
                            echo "<td>{$row['tanggal_bimbingan']}</td>";
                            echo "<td>{$row['topik_bimbingan']}</td>";
                            echo "<td>{$row['catatan']}</td>";
                            echo "<td>{$row['nama_dosen']}</td>";
                            echo "<td>" . ucfirst($row['status']) . "</td>";
                            echo "</tr>";
                            $no++;
                        }
                    } else {
                        echo "<tr><td colspan='6' class='text-center'>Belum ada data bimbingan.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>

        <!-- Tombol Kembali -->
        <div class="text-center mt-3">
            <a href="home_mahasiswa.php" class="btn btn-secondary">Kembali ke Home</a>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
