<?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 data KRS dari database
$query = "
    SELECT tbl_krs.id_krs, tbl_mahasiswa.nama_mahasiswa, tbl_kelas.nama_kelas, tbl_matakuliah.nama_matakuliah, 
           tbl_krs.jumlah_sks, tbl_krs.semester, tbl_krs.tahun_ajaran 
    FROM tbl_krs
    JOIN tbl_mahasiswa ON tbl_krs.id_mahasiswa = tbl_mahasiswa.id_mahasiswa
    JOIN tbl_kelas ON tbl_krs.id_kelas = tbl_kelas.id_kelas
    JOIN tbl_matakuliah ON tbl_krs.id_matakuliah = tbl_matakuliah.id_matakuliah
    WHERE tbl_krs.id_mahasiswa = {$_SESSION['id_mahasiswa']}
";

$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>Data KRS</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="#">Sistem KRS</a>
    </nav>

    <!-- Container -->
    <div class="container mt-4">
        <h3 class="text-center mb-4">Data KRS</h3>
        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Nama Mahasiswa</th>
                    <th>Kelas</th>
                    <th>Mata Kuliah</th>
                    <th>Jumlah SKS</th>
                    <th>Semester</th>
                    <th>Tahun Ajaran</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['nama_mahasiswa']}</td>";
                        echo "<td>{$row['nama_kelas']}</td>";
                        echo "<td>{$row['nama_matakuliah']}</td>";
                        echo "<td>{$row['jumlah_sks']}</td>";
                        echo "<td>{$row['semester']}</td>";
                        echo "<td>{$row['tahun_ajaran']}</td>";
                        echo "</tr>";
                        $no++;
                    }
                } else {
                    echo "<tr><td colspan='7' class='text-center'>Belum ada data KRS.</td></tr>";
                }
                ?>
            </tbody>
        </table>

        <!-- Tombol Tambah KRS -->
        <div class="text-center mt-3">
            <a href="krs_tambah.php" class="btn btn-primary">Tambah KRS</a>
             <a href="home_mahasiswa.php" class="btn btn-secondary">Kembali</a>
        </div>
    </div>
</body>
</html>
