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

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

// Ambil data nilai dari database
$query = "SELECT tbl_nilai.id_nilai, tbl_mahasiswa.nama_mahasiswa, tbl_dosen.nama_dosen, tbl_matakuliah.nama_matakuliah, tbl_nilai.nilai, tbl_nilai.semester 
          FROM tbl_nilai 
          JOIN tbl_mahasiswa ON tbl_nilai.id_mahasiswa = tbl_mahasiswa.id_mahasiswa
          JOIN tbl_dosen ON tbl_nilai.id_dosen = tbl_dosen.id_dosen
          JOIN tbl_matakuliah ON tbl_nilai.id_matakuliah = tbl_matakuliah.id_matakuliah";

$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 Nilai Mahasiswa</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- Header Section -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Sistem Nilai</a>
    </nav>

    <!-- Container Section -->
    <div class="container mt-4">
        <h3 class="text-center mb-4">Daftar Nilai Mahasiswa</h3>
        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Nama Mahasiswa</th>
                    <th>Nama Dosen Pengasuh</th>
                    <th>Mata Kuliah</th>
                    <th>Nilai</th>
                    <th>Semester</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_dosen']}</td>";
                        echo "<td>{$row['nama_matakuliah']}</td>";
                        echo "<td>{$row['nilai']}</td>";
                        echo "<td>{$row['semester']}</td>";
                        echo "</tr>";
                        $no++;
                    }
                } else {
                    echo "<tr><td colspan='6' class='text-center'>Belum ada data nilai.</td></tr>";
                }
                ?>
            </tbody>
        </table>

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