<?php
namespace App\Controllers;

use Core\Database;
use PDO;

class DosenViewUTSUASKelasController
{
    private $db;

    public function __construct()
    {
        $this->db = Database::getConnection();
    }

    public function index()
    {
        $niy = $_SESSION['user']['niy'] ?? null;
        if (!$niy) {
            header("Location: /login");
            exit();
        }

        // Ambil tahun akademik aktif
        $stmtTA = $this->db->query("SELECT tahun_akademik FROM tb_akademik WHERE status='Aktif' LIMIT 1");
        $tahun_akademik = $stmtTA->fetchColumn();

        // Ambil daftar MK dosen
        $stmtMK = $this->db->prepare("
            SELECT dm.id_dmk, mk.singkatan, dm.kode_kelas 
            FROM tb_dosen_mk dm
            INNER JOIN tb_mk mk ON dm.kode_mk = mk.kode_mk
            WHERE dm.niy = :niy AND dm.tahun_akademik = :tahun_akademik
        ");
        $stmtMK->execute([
            ':niy' => $niy,
            ':tahun_akademik' => $tahun_akademik
        ]);
        $listMK = $stmtMK->fetchAll(PDO::FETCH_ASSOC);
		
		    // Ambil id_j_soal dari GET (saat awal akses) atau dari POST (saat form dikirim ulang)
			$id_j_soal = $_SERVER['REQUEST_METHOD'] === 'POST'
				? ($_POST['id_j_soal'] ?? null)
				: ($_GET['id_j_soal'] ?? null);
				
        // Kirim data ke view
        require __DIR__ . '/../Views/dosen/nilai_uas_uts.php';
    }
}
