<?php
namespace App\Controllers;

use Core\Database;
use PDO;

class PegawaiInputNilaiController
{
    private $db;

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

    public function index()
    {
        $user = $_SESSION['user']['username'] ?? null;
        if (!$user) {
            header("Location: /login");
            exit();
        }
    
        // Ambil daftar tahun akademik dari tb_dosen_mk (tanpa filter niy)
        $stmt = $this->db->query("
            SELECT DISTINCT tahun_akademik 
            FROM tb_dosen_mk 
            ORDER BY tahun_akademik DESC
        ");
        $tahunAkademikList = $stmt->fetchAll(PDO::FETCH_COLUMN);
    
        require __DIR__ . '/../Views/pegawai/form_nilai.php';
    }


    // === Tambahan untuk AJAX ===
    public function getMataKuliahByTahun()
    {
        header('Content-Type: application/json');
    
        $tahun = $_GET['tahun'] ?? null;
    
        if (!$tahun) {
            echo json_encode([]);
            return;
        }
    
        $query = "
            SELECT dm.id_dmk, mk.singkatan, dm.kode_kelas, d.nama AS nama_dosen
            FROM tb_dosen_mk dm
            INNER JOIN tb_mk mk ON dm.kode_mk = mk.kode_mk
            INNER JOIN tb_dosen d ON dm.niy = d.niy
            WHERE dm.tahun_akademik = :tahun
            ORDER BY mk.singkatan ASC
        ";
    
        $stmt = $this->db->prepare($query);
        $stmt->execute(['tahun' => $tahun]);
        $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
        echo json_encode($data);
    }

}
