<?php

namespace App\Controllers;

use FPDF;
use Core\Controller;
use Core\Database;

require_once __DIR__ . '/../libraries/fpdf/fpdf.php';

class PDF extends FPDF
{
    public $nama, $prodi, $smt, $kaprod, $jumlah_sks, $kode_kelas;

    function Header()
    {
        $this->Image('assets/img/kop_surat.jpg', 3, 3, 205);
        $this->SetFont('Times', 'B', 14);
        $this->Cell(0, 30, '', 0, 1, 'C');
        $this->Ln(2);
        $this->Cell(0, 10, 'Kartu Rencana Studi', 0, 1, 'C');
        $this->Ln(1);

        $db = Database::getConnection();

        // ambil nim dari session (pastikan session sudah start di controller)
        $nim = $_SESSION['user']['nim'] ?? null;
        $thakademik = $this->getTahunAkademik($db);

        $sql = $db->prepare("SELECT tm.nama, tp.nama_prodi, tk.smt, tk.kode_kelas
                             FROM tb_mahasiswa tm
                             JOIN tb_prodi tp ON tm.id_prodi = tp.id_prodi
                             JOIN tb_kelas tk ON tm.nim = tk.nim
                             WHERE tm.nim = ? AND tk.status = 'Aktif' AND tk.tahun_akademik = ?");
        $sql->execute([$nim, $thakademik]);
        $r = $sql->fetch();

        if ($r) {
            $this->nama = $r['nama'];
            $this->prodi = $r['nama_prodi'];
            $this->smt = $r['smt'];
            $this->kode_kelas = $r['kode_kelas'];

            $this->SetFont('Times', '', 12);
            $this->Cell(25, 0, "Nama", 0, 0, 'L');
            $this->Cell(45, 0, ": " . $this->nama, 0, 1, 'L');
            $this->Cell(25, 10, "NIM", 0, 0, 'L');
            $this->Cell(25, 10, ": $nim", 0, 1, 'L');
            $this->Cell(25, 10, "SMT/Kelas", 0, 0, 'L');
            $this->Cell(30, 10, ": " . $this->smt . " / " . $this->kode_kelas, 0, 1, 'L');
            $this->Ln(5);

            $this->SetFont('Times', 'B', 12);
            $this->Cell(8, 10, 'No', 1);
            $this->Cell(25, 10, 'Kode MK', 1);
            $this->Cell(70, 10, 'Mata Kuliah', 1);
            $this->Cell(75, 10, 'Dosen', 1);
            $this->Cell(9, 10, 'SKS', 1);
            $this->Ln();
        } else {
            $this->Cell(0, 10, 'Data tidak ditemukan.', 0, 1, 'C');
        }
    }

    function Footer()
    {
        $db = Database::getConnection();

        $nim = $_SESSION['user']['nim'] ?? null;
        $thakademik = $this->getTahunAkademik($db);

        $sql = $db->prepare("SELECT tm.nama, tp.kaprodi FROM tb_mahasiswa tm
                             JOIN tb_prodi tp ON tm.id_prodi = tp.id_prodi
                             WHERE tm.nim = ?");
        $sql->execute([$nim]);
        $r = $sql->fetch();
        $this->kaprod = $r['kaprodi'] ?? '';
        $this->nama = $r['nama'] ?? '';

        $skq = $db->prepare("SELECT SUM(tm.sks) AS jml_sks FROM tb_krs tk
                             JOIN tb_mk tm ON tk.kode_mk = tm.kode_mk
                             WHERE tk.nim = ? AND tk.accept = 1 AND tk.tahun_akademik = ?");
        $skq->execute([$nim, $thakademik]);
        $sks = $skq->fetch();
        $this->jumlah_sks = $sks['jml_sks'] ?? 'Nihil / Kosong';

        $this->SetY(-90);
        $this->SetFont('Times', '', 12);
        $this->Cell(0, 10, "Jumlah SKS : " . $this->jumlah_sks, 0, 1, 'L');
        $this->Ln(5);
        $this->Cell(65, 7, 'Mengetahui', 0, 0, 'L');
        $this->Cell(75, 7, 'Disetujui Oleh', 0, 0, 'L');
        $this->Cell(65, 7, '', 0, 1, 'L');

        $this->Cell(65, 7, 'Kaprodi', 0, 0, 'L');
        $this->Cell(75, 7, 'Dosen PA', 0, 0, 'L');
        $this->Cell(65, 7, 'Mahasiswa', 0, 1, 'L');
        $this->Ln(35);
        $this->SetFont('Times', 'B', 12);
        $this->Cell(65, 7, $this->kaprod, 0, 0, 'L');
        $this->Cell(75, 7, '--------------------------', 0, 0, 'L');
        $this->Cell(65, 7, $this->nama, 0, 1, 'L');
    }

    function getTahunAkademik($db)
    {
        $q = $db->query("SELECT tahun_akademik FROM tb_akademik WHERE status='Aktif'");
        return $q->fetch()['tahun_akademik'] ?? '';
    }
}

class PdfKRSController extends Controller
{
    public function index()
    {
        $pdf = new PDF();
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->SetFont('Times', '', 12);

        $db = Database::getConnection();

        $nim = $_SESSION['user']['nim'] ?? null;
        $thakademik = $pdf->getTahunAkademik($db);

        $kelasq = $db->prepare("SELECT kode_kelas FROM tb_kelas WHERE nim = ? AND tahun_akademik = ?");
        $kelasq->execute([$nim, $thakademik]);
        $kode_kelas = $kelasq->fetch()['kode_kelas'] ?? '';

        $stmt = $db->prepare("SELECT tk.kode_mk, tm.singkatan, tm.sks FROM tb_krs tk
                              JOIN tb_mk tm ON tk.kode_mk = tm.kode_mk
                              WHERE tk.nim = ? AND tk.accept = 1 AND tk.tahun_akademik = ?
                              ORDER BY tk.kode_mk ASC");
        $stmt->execute([$nim, $thakademik]);

        $no = 1;
        while ($row = $stmt->fetch()) {
            $dsnq = $db->prepare("SELECT td.nama FROM tb_dosen_mk tdm
                                  JOIN tb_dosen td ON tdm.niy = td.niy
                                  WHERE tdm.kode_kelas = ? AND tdm.kode_mk = ?");
            $dsnq->execute([$kode_kelas, $row['kode_mk']]);
            while ($dsn = $dsnq->fetch()) {
                $pdf->Cell(8, 10, $no++, 1);
                $pdf->Cell(25, 10, $row['kode_mk'], 1);
                $pdf->Cell(70, 10, $row['singkatan'], 1);
                $pdf->Cell(75, 10, $dsn['nama'], 1);
                $pdf->Cell(9, 10, $row['sks'], 1);
                $pdf->Ln();
            }
        }

        if ($no == 1) {
            $pdf->Cell(0, 10, 'Tidak ada data KRS ditemukan.', 0, 1, 'C');
        }

        ob_end_clean();
        // Format tanggal jam: Tahun-Bulan-Hari_Jam-Menit
        $tanggalJam = date('Y-m-d_H-i');
        
        // Output PDF dengan nama berisi NIM + tanggal jam
        $pdf->Output("KRS_{$nim}_{$tanggalJam}.pdf", 'D');
    }
}
