<?php

namespace App\Controllers;

use PDO;
use Core\Controller;
use Core\Database;  // <-- jangan lupa import ini

// Load FPDF dulu
require_once __DIR__ . '/../libraries/fpdf/fpdf.php';

class PDF extends \FPDF
{
    public $nim, $smt, $db;

    public function __construct($orientation, $unit, $size, $nim, $smt, PDO $db)
    {
        parent::__construct($orientation, $unit, $size);
        $this->nim = $nim;
        $this->smt = $smt;
        $this->db = $db;
    }

    public 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(3);
        $this->Cell(0, 10, 'Kartu Hasil Studi', 0, 1, 'C');

        $stmt = $this->db->prepare("
            SELECT m.nama, p.nama_prodi 
            FROM tb_mahasiswa m 
            JOIN tb_prodi p ON m.id_prodi = p.id_prodi 
            WHERE m.nim = ?
        ");
        $stmt->execute([$this->nim]);
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
        $nama = $data['nama'] ?? '-';
        $prodi = $data['nama_prodi'] ?? '-';

        $kelas = $this->db->prepare("SELECT kode_kelas FROM tb_kelas WHERE nim = ? ORDER BY id_kelas DESC LIMIT 1");
        $kelas->execute([$this->nim]);
        $kode_kelas = $kelas->fetch(PDO::FETCH_ASSOC)['kode_kelas'] ?? '-';

        $this->SetFont('Times', '', 12);
        $this->Cell(25, 10, "Nama", 0, 0);
        $this->Cell(75, 10, ": $nama", 0, 0);
        $this->Ln(6);
        $this->Cell(25, 10, "NIM", 0, 0);
        $this->Cell(75, 10, ": {$this->nim}", 0, 0);
        $this->Ln(6);
        $this->Cell(25, 10, "SMT/Kls", 0, 0);
        $this->Cell(75, 10, ": {$this->smt} / $kode_kelas", 0, 1);

        $this->SetFont('Times', 'B', 12);
        $this->Cell(8, 10, 'No', 1);
        $this->Cell(30, 10, 'Kode MK', 1);
        $this->Cell(85, 10, 'Mata Kuliah', 1);
        $this->Cell(17, 10, 'SKS', 1);
        $this->Cell(17, 10, 'Mutu', 1);
        $this->Cell(17, 10, 'Bobot', 1);
        $this->Cell(17, 10, 'Nilai', 1);
        $this->Ln();
    }

    public function Footer()
    {
        $tanggal = date('d F Y');

        $stmt = $this->db->prepare("
            SELECT kaprodi 
            FROM tb_mahasiswa m 
            JOIN tb_prodi p ON m.id_prodi = p.id_prodi 
            WHERE m.nim = ?
        ");
        $stmt->execute([$this->nim]);
        $kaprodi = $stmt->fetch(PDO::FETCH_ASSOC)['kaprodi'] ?? '-';

        $this->SetY(-55);
        $this->SetFont('Times', '', 12);
        $this->Cell(130, 7, '', 0, 0);
        $this->Cell(60, 7, "Pandeglang, $tanggal", 0, 1, 'L');
        $this->Cell(130, 7, '', 0, 0);
        $this->Cell(60, 7, "Kaprodi", 0, 1, 'L');
        $this->Ln(20);
        $this->SetFont('Times', 'B', 12);
        $this->Cell(130, 7, '', 0, 0);
        $this->Cell(60, 7, "$kaprodi", 0, 1, 'L');
    }
}

class CetakKHSController extends Controller
{
    protected $db;

    // Constructor sekarang argumen PDO jadi opsional
    public function __construct(PDO $db = null)
    {
        if ($db === null) {
            // Ambil koneksi PDO secara otomatis jika tidak diberikan
            $this->db = Database::getConnection();
        } else {
            $this->db = $db;
        }
    }

    public function index()
    {
        $nim = $_SESSION['user']['nim'] ?? null;
        $smt = $_GET['smt'] ?? null;

        if (!$nim || !$smt) {
            die("NIM atau semester tidak valid.");
        }

        $db = $this->db;

        $pdf = new PDF('P', 'mm', 'A4', $nim, $smt, $db);
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->SetFont('Times', '', 12);

        $aktif = 1;
        $no = 1;

        $query = $db->prepare("SELECT * FROM tb_khs WHERE smt = ? AND accept = ? AND nim = ? ORDER BY kode_mk ASC");
        $query->execute([$smt, $aktif, $nim]);

        $totalSKS = 0;
        $totalPoin = 0;
        
        while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $kodeMK = $row['kode_mk'];
            $mk = $db->prepare("SELECT nama_mk, sks FROM tb_mk WHERE kode_mk = ?");
            $mk->execute([$kodeMK]);
            $mata = $mk->fetch(PDO::FETCH_ASSOC);
        
            // Jika tidak ditemukan, beri nilai default
            $namaMK = $mata['nama_mk'] ?? '-';
            $sksMK = $mata['sks'] ?? 0;
        
            $pdf->Cell(8, 10, $no++, 1, 0, 'C');
            $pdf->Cell(30, 10, $kodeMK, 1);
            $pdf->Cell(85, 10, $namaMK, 1);
            $pdf->Cell(17, 10, $sksMK, 1, 0, 'C');
            $pdf->Cell(17, 10, $row['mutu'], 1, 0, 'C');
            $pdf->Cell(17, 10, $row['bobot'], 1, 0, 'C');
            $pdf->Cell(17, 10, $row['nilai'], 1, 1, 'C');
        
            $totalSKS += $sksMK;
            $totalPoin += $row['sks_x_bobot'] ?? 0;
        }


        $ips = $totalSKS > 0 ? number_format($totalPoin / $totalSKS, 2) : '0.00';

        $pdf->Ln(5);
        $pdf->Cell(0, 10, "Jumlah SKS : $totalSKS", 0, 1, 'L');
        $pdf->Cell(0, 10, "IP Semester : $ips", 0, 1, 'L');

        $stmt = $db->prepare("SELECT nama FROM tb_mahasiswa WHERE nim = ?");
        $stmt->execute([$nim]);
        $nama = $stmt->fetch(PDO::FETCH_ASSOC)['nama'] ?? 'Mahasiswa';

        $pdf->Output("I", "KHS_{$nama}_{$nim}.pdf");
        exit;
    }
}
