<?php
namespace App\Controllers;

use Core\Database;
use PDO;

class DosenInputNilaiController
{
    private $db;

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

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

        // 1. Ambil tahun akademik aktif
        $stmt = $this->db->prepare("SELECT tahun_akademik FROM tb_akademik WHERE status='Aktif'");
        $stmt->execute();
        $tahunAkademik = $stmt->fetch(PDO::FETCH_ASSOC)['tahun_akademik'] ?? null;

        // 2. Ambil daftar mata kuliah dosen berdasarkan tahun aktif
        $query = "
            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
        ";
        $stmt = $this->db->prepare($query);
        $stmt->execute([
            'niy' => $niy,
            'tahun' => $tahunAkademik
        ]);
        $mataKuliah = $stmt->fetchAll(PDO::FETCH_ASSOC);

        // Kirim data ke view
        require __DIR__ . '/../Views/dosen/form_nilai.php';
    }
}
