<?php
namespace App\Controllers;

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

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

class PDF extends FPDF
{
    function Header()
    {
        $this->Image('assets/img/kop_surat.jpg', 3, 3, 205);
        $this->Ln(40);
    }
}

class DosenPDFUTSUASKelasController extends Controller
{
    public function index()
    {
        $db = Database::getConnection();

        // Ambil parameter dari URL
        $id_j_soal = $_GET['id_j_soal'] ?? '';
        $kode_mk = $_GET['kode_mk'] ?? '';
        $kode_kelas = $_GET['kode_kelas'] ?? '';
        $tahun_akademik = $_GET['tahun_akademik'] ?? '';
        $jenis_nilai = ($id_j_soal == 1) ? "UTS" : "UAS";

        // Ambil nama MK dan semester
        $stmtMk = $db->prepare("SELECT nama_mk, smt FROM tb_mk WHERE kode_mk = :kode_mk");
        $stmtMk->execute([':kode_mk' => $kode_mk]);
        $mk = $stmtMk->fetch(\PDO::FETCH_ASSOC) ?: ['nama_mk' => '-', 'smt' => '-'];
        $nama_mk = $mk['nama_mk'];
        $smt = $mk['smt'];

        // Ambil nama dosen
        $stmtDosen = $db->prepare("SELECT d.nama FROM tb_dosen_mk dm 
            INNER JOIN tb_dosen d ON d.niy = dm.niy 
            WHERE dm.kode_mk = :kode_mk AND dm.tahun_akademik = :tahun_akademik AND dm.kode_kelas= :kode_kelas LIMIT 1");
        $stmtDosen->execute([
            ':kode_mk' => $kode_mk,
            ':tahun_akademik' => $tahun_akademik,
            ':kode_kelas' => $kode_kelas
        ]);
        $dosen = $stmtDosen->fetch(\PDO::FETCH_ASSOC) ?: ['nama' => '-'];
        $nama = $dosen['nama'];

        // Ambil data mahasiswa dan nilai
        $stmt = $db->prepare("SELECT DISTINCT m.nim, m.nama, k.kode_kelas, d.nilai
            FROM tb_mk_dijawab d
            INNER JOIN tb_mahasiswa m ON d.nim = m.nim
            INNER JOIN tb_kelas k ON m.nim = k.nim
            WHERE d.kode_mk = :kode_mk 
            AND d.id_jenis_s = :id_j_soal 
            AND d.tahun_akademik = :tahun_akademik 
            AND k.tahun_akademik = :tahun_akademik 
            AND k.status = 'Aktif' 
            AND k.kode_kelas = :kode_kelas
            ORDER BY m.nim ASC");

        $stmt->execute([
            ':kode_mk' => $kode_mk,
            ':id_j_soal' => $id_j_soal,
            ':tahun_akademik' => $tahun_akademik,
            ':kode_kelas' => $kode_kelas
        ]);

        $hasil = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        $kelas = $hasil[0]['kode_kelas'] ?? '-';

        // Buat PDF
        $pdf = new PDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 14);
        $pdf->Cell(190, 10, "Daftar Nilai $jenis_nilai - Kelas $kode_kelas", 0, 1, 'C');
        $pdf->Ln(5);

        $pdf->SetFont('Arial', '', 12);
        $pdf->Cell(50, 7, "Nama Dosen", 0, 0);
        $pdf->Cell(140, 7, ": $nama", 0, 1);
        $pdf->Cell(50, 7, "Nama MK", 0, 0);
        $pdf->Cell(140, 7, ": $nama_mk", 0, 1);
        $pdf->Cell(50, 7, "Kelas", 0, 0);
        $pdf->Cell(140, 7, ": $kode_kelas", 0, 1);
        $pdf->Cell(50, 7, "SMT", 0, 0);
        $pdf->Cell(140, 7, ": $smt", 0, 1);
        $pdf->Ln(5);

        // Tabel Header
        $pdf->SetFillColor(200, 200, 200);
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(10, 10, 'No', 1, 0, 'C', true);
        $pdf->Cell(40, 10, 'NIM', 1, 0, 'C', true);
        $pdf->Cell(90, 10, 'Nama', 1, 0, 'C', true);
        $pdf->Cell(25, 10, 'Nilai', 1, 1, 'C', true);

        // Isi Tabel
        $pdf->SetFont('Arial', '', 12);
        if (!empty($hasil)) {
            $no = 1;
            foreach ($hasil as $row) {
                $pdf->Cell(10, 8, $no++, 1, 0, 'C');
                $pdf->Cell(40, 8, $row['nim'], 1, 0, 'L');
                $pdf->Cell(90, 8, $row['nama'], 1, 0, 'L');
                $pdf->Cell(25, 8, $row['nilai'] ?? 0, 1, 1, 'C');
            }
        } else {
            $pdf->Cell(165, 8, 'Tidak ada data', 1, 1, 'C');
        }

        $nama_file = "Nilai $jenis_nilai - Kelas $kelas.pdf";
        $pdf->Output($nama_file, 'I'); // inline view
    }
}
