<?php
namespace App\Models;

use Core\Database;
use Core\Model;

class DosenMk
{
    private $db;

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

    public function getAllMatkul()
    {
        $stmt = $this->db->query("SELECT kode_mk, nama_mk FROM tb_mk ");
        return $stmt->fetchAll();
    }

    public function getTahunAkademikAktif()
    {
        $stmt = $this->db->query("SELECT tahun_akademik FROM tb_akademik WHERE status = 'Aktif' ");
        $row = $stmt->fetch();
        return $row['tahun_akademik'] ?? null;
    }

	public function getMatkulByDosen($niy, $tahunAkademik)
	{
		$stmt = $this->db->prepare("
			SELECT dm.*, mk.nama_mk, mk.singkatan
			FROM tb_dosen_mk dm
			JOIN tb_mk mk ON mk.kode_mk = dm.kode_mk
			WHERE dm.niy = :niy AND dm.tahun_akademik = :tahun_akademik
		");
		$stmt->execute(['niy' => $niy, 'tahun_akademik' => $tahunAkademik]);
		return $stmt->fetchAll();
	}

    public function getDetailMatkul($idDmk)
    {
        $stmt = $this->db->prepare("
            SELECT kode_mk, kode_kelas, tahun_akademik 
            FROM tb_dosen_mk 
            WHERE id_dmk = :id_dmk
        ");
        $stmt->execute(['id_dmk' => $idDmk]);
        return $stmt->fetch();
    }

}
