<?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()
    {
        if ($this->PageNo() == 1) {
            $this->Image('assets/img/kop_surat.jpg', 3, 3, 205);
            $this->Ln(30);
        }
    }

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('times', 'I', 8);
        $this->Cell(0, 10, 'Halaman ' . $this->PageNo(), 0, 0, 'C');
    }
}

class DosenDownJawabanController extends Controller
{
    public function index()
    {
        $db = Database::getConnection();
        $kodeMk = $_GET['kode_mk'] ?? null;
        $kodeKls = $_GET['kode_kelas'] ?? null;

        if (!$kodeMk || !$kodeKls) {
            echo "Parameter tidak lengkap.";
            return;
        }

        // Ambil info MK + dosen + kelas
        $stmtInfo = $db->prepare("SELECT mk.nama_mk, d.nama AS nama_dosen, k.kode_kelas
                                  FROM tb_mk mk
                                  JOIN tb_dosen_mk dm ON mk.kode_mk = dm.kode_mk
                                  JOIN tb_dosen d ON dm.niy = d.niy
                                  JOIN tb_kelas k ON dm.kode_kelas = k.kode_kelas
                                  WHERE mk.kode_mk = ? AND k.kode_kelas = ?");
        $stmtInfo->execute([$kodeMk, $kodeKls]);
        $info = $stmtInfo->fetch(\PDO::FETCH_ASSOC);

        if (!$info) {
            echo "<script>alert('Data mata kuliah tidak ditemukan.'); window.history.back();</script>";
            return;
        }

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

        // Ambil tahun akademik aktif
        $stmtTa = $db->prepare("SELECT tahun_akademik FROM tb_akademik WHERE status = 'Aktif' LIMIT 1");
        $stmtTa->execute();
        $tahunAkademik = $stmtTa->fetchColumn();

        // Ambil semua jawaban mahasiswa
        $stmt = $db->prepare("SELECT j.jawaban, m.nim, m.nama, s.soal, s.tipe_soal, s.tahun_akademik
                              FROM tb_jawaban j
                              JOIN tb_mahasiswa m ON j.nim = m.nim
                              JOIN tb_soal s ON j.id_soal = s.id_soal
                              WHERE s.tipe_soal = ? AND s.tahun_akademik = ? AND s.kode_mk = ? AND s.kode_kelas = ?
                              ORDER BY m.nim, s.soal ASC");
        $stmt->execute([$jenisSoal, $tahunAkademik, $kodeMk, $kodeKls]);

        $pdf = new PDF();
        $pdf->AddPage();
        $pdf->SetFont('times', '', 12);

        // Header informasi
        $pdf->SetFont('times', 'B', 12);
        $pdf->Ln(10);
        $pdf->Cell(0, 10, 'Mata Kuliah: ' . $info['nama_mk'], 0, 1);
        $pdf->Cell(0, 10, 'Nama Dosen: ' . $info['nama_dosen'], 0, 1);
        $pdf->Cell(0, 10, 'Kode Kelas: ' . $info['kode_kelas'], 0, 1);
        $pdf->Ln(10);

        $currentNim = '';
        $currentNama = '';
        $questionNumber = 1;

        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            if ($currentNim !== $row['nim']) {
                if ($currentNim !== '') {
                    $pdf->AddPage();
                }

                $currentNim = $row['nim'];
                $currentNama = $row['nama'];

                $pdf->SetFont('times', 'B', 12);
                $pdf->Ln(5);
                $pdf->Cell(0, 10, 'Nama: ' . $currentNama, 0, 1);
                $pdf->Cell(0, 10, 'NIM: ' . $currentNim, 0, 1);
                $pdf->Ln(10);
                $pdf->SetFont('times', '', 12);
                $questionNumber = 1;
            }

            $pdf->MultiCell(0, 10, 'Soal ' . $questionNumber . ': ' . $row['soal']);
            $pdf->MultiCell(0, 10, 'Jawaban: ' . $row['jawaban']);
            $pdf->Ln(2);
            $questionNumber++;
        }

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

        ob_end_clean();
        $fileName = "Jawaban_{$info['nama_dosen']}_{$info['nama_mk']}_{$info['kode_kelas']}.pdf";
        $pdf->Output($fileName, 'I');
    }
}
