<?php

namespace App\Controllers;

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); // Spasi setelah gambar header
    }
}

class DosenDownloadPdfSoalController
{
    public function index()
    {
        $db = Database::getConnection();

        // Ambil parameter dari URL
        $kode_mk = $_GET['kode_mk'] ?? null;
        $kode_kelas = $_GET['kode_kelas'] ?? null;
        $niy = $_GET['niy'] ?? null;

        if (!$kode_mk || !$kode_kelas || !$niy) {
            die('Parameter tidak lengkap.');
        }

        // Ambil jenis soal aktif
        $stmt = $db->prepare("SELECT jenis_soal FROM tb_jenis_soal WHERE status = 'Aktif'");
        $stmt->execute();
        $active_soal = $stmt->fetchColumn();

        // Ambil nama dosen
        $stmt = $db->prepare("SELECT nama FROM tb_dosen WHERE niy = :niy");
        $stmt->execute(['niy' => $niy]);
        $nama_dosen = $stmt->fetchColumn() ?: 'Nama Dosen Tidak Ditemukan';

        // Ambil nama mata kuliah
        $stmt = $db->prepare("
            SELECT mk.nama_mk
            FROM tb_soal s
            INNER JOIN tb_mk mk ON s.kode_mk = mk.kode_mk
            INNER JOIN tb_akademik ak ON s.tahun_akademik = ak.tahun_akademik
            WHERE s.niy = :niy AND ak.status = 'Aktif' AND s.kode_mk = :kode_mk
        ");
        $stmt->execute([
            'niy' => $niy,
            'kode_mk' => $kode_mk
        ]);
        $nama_mk = $stmt->fetchColumn() ?: 'Mata Kuliah Tidak Ditemukan';

        // Ambil soal
        $stmt = $db->prepare("
            SELECT soal
            FROM tb_soal s
            INNER JOIN tb_akademik a ON s.tahun_akademik = a.tahun_akademik
            WHERE s.kode_mk = :kode_mk AND s.niy = :niy AND s.kode_kelas = :kode_kelas AND s.tipe_soal = :tipe_soal
        ");
        $stmt->execute([
            'kode_mk' => $kode_mk,
            'kode_kelas' => $kode_kelas,
            'niy' => $niy,
            'tipe_soal' => $active_soal
        ]);
        $soal = $stmt->fetchAll(\PDO::FETCH_ASSOC);

        // Buat PDF
        $pdf = new PDF();
        $pdf->AddPage();
        $pdf->SetFont('Times', 'B', 14);
        $pdf->Cell(0, 10, 'SOAL', 0, 1, 'C');
        $pdf->Ln(2);

        $pdf->SetFont('Arial', '', 12);
        $pdf->Cell(0, 10, 'Nama Dosen: ' . $nama_dosen, 0, 1);
        $pdf->Cell(0, 10, 'Mata Kuliah: ' . $nama_mk, 0, 1);
        $pdf->Cell(0, 10, 'Kode Kelas: ' . $kode_kelas, 0, 1);
        $pdf->Ln(5);

        if ($soal) {
            $no = 1;
            foreach ($soal as $item) {
                $pdf->Cell(10, 10, $no++ . '.', 0);
                $pdf->MultiCell(0, 10, $item['soal']);
                $pdf->Ln(2);
            }
        } else {
            $pdf->Cell(0, 10, 'Tidak ada soal untuk mata kuliah ini.', 0, 1);
        }

        ob_end_clean(); // Bersihkan buffer output
        $pdf->Output("Soal_{$kode_mk}_{$kode_kelas}.pdf", 'I'); // Tampilkan PDF
    }
}
