<?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(35);
        }
    }
}

class PDFTranskipController extends Controller
{
    public function hasilPDF()
    {
        // ✅ Koneksi yang benar
        $conn = Database::getConnection();

        if (isset($_GET['nim'])) {
            $nim = htmlspecialchars($_GET['nim'], ENT_QUOTES, 'UTF-8');

            // Cek apakah sudah mengikuti komprehensif
            $cek = $conn->prepare("SELECT COUNT(*) FROM tb_token_kompre WHERE nim = :nim");
            $cek->execute(['nim' => $nim]);
            if ($cek->fetchColumn() == 0) {
                echo "<script>alert('Anda belum melakukan kegiatan Komprehensif.'); window.location.href='mahasiswa-ajukan';</script>";
                exit;
            }

            // Ambil data transkrip
            $stmt = $conn->prepare("
                SELECT m.nama, p.nama_prodi, p.kaprodi, mk.nama_mk, mk.kode_mk, kh.mutu, kh.bobot, kh.sks_x_bobot, kh.smt, mk.sks 
                FROM tb_mahasiswa m
                INNER JOIN tb_prodi p ON m.id_prodi = p.id_prodi
                INNER JOIN tb_khs kh ON m.nim = kh.nim
                INNER JOIN tb_mk mk ON kh.kode_mk = mk.kode_mk
                WHERE m.nim = :nim
                ORDER BY kh.smt ASC, mk.nama_mk ASC
            ");
            $stmt->execute(['nim' => $nim]);
            $transkrip = $stmt->fetchAll();

            // Ambil data IPK
            $ipk_stmt = $conn->prepare("
                SELECT SUM(kh.sks_x_bobot) AS total_sks_x_bobot, SUM(mk.sks) AS total_sks
                FROM tb_khs kh
                INNER JOIN tb_mk mk ON kh.kode_mk = mk.kode_mk
                WHERE kh.nim = :nim
            ");
            $ipk_stmt->execute(['nim' => $nim]);
            $ipk_data = $ipk_stmt->fetch();

            $ipk = ($ipk_data['total_sks'] > 0) ? ($ipk_data['total_sks_x_bobot'] / $ipk_data['total_sks']) : 0.0;
            $jumlah_mata_kuliah = count($transkrip);

            if ($transkrip) {
                $pdf = new PDF();
                $pdf->AddPage();

                $nama_mahasiswa = $transkrip[0]['nama'];
                $prodi = $transkrip[0]['nama_prodi'];
                $nmprodi = $transkrip[0]['kaprodi'];

                $pdf->SetFont('Arial', 'B', 14);
                $pdf->Cell(0, 10, 'Transkrip Nilai Sementara', 0, 1, 'C');

                $pdf->SetFont('Arial', '', 12);
                $pdf->Cell(40, 10, 'Nama', 0, 0);
                $pdf->Cell(0, 10, ': ' . $nama_mahasiswa, 0, 1);
                $pdf->Cell(40, 10, 'NIM', 0, 0);
                $pdf->Cell(0, 10, ': ' . $nim, 0, 1);
                $pdf->Cell(40, 10, 'Program Studi', 0, 0);
                $pdf->Cell(0, 10, ': ' . $prodi, 0, 1);
                $pdf->Ln(2);

                $pdf->SetFont('Arial', 'B', 12);
                $pdf->Cell(10, 10, 'No', 1);
                $pdf->Cell(90, 10, 'Nama MK', 1);
                $pdf->Cell(20, 10, 'Kode MK', 1);
                $pdf->Cell(10, 10, 'SKS', 1);
                $pdf->Cell(15, 10, 'SMT', 1);
                $pdf->Cell(15, 10, 'Mutu', 1);
                $pdf->Cell(15, 10, 'Bobot', 1);
                $pdf->Cell(15, 10, 'Skor', 1);
                $pdf->Ln();

                $pdf->SetFont('Arial', '', 8);
                $no = 1;
                foreach ($transkrip as $data) {
                    $pdf->Cell(10, 6, $no++, 1);
                    $pdf->Cell(90, 6, $data['nama_mk'], 1);
                    $pdf->Cell(20, 6, $data['kode_mk'], 1);
                    $pdf->Cell(10, 6, $data['sks'], 1, 0, 'C');
                    $pdf->Cell(15, 6, $data['smt'], 1, 0, 'C');
                    $pdf->Cell(15, 6, $data['mutu'], 1, 0, 'C');
                    $pdf->Cell(15, 6, $data['bobot'], 1, 0, 'C');
                    $pdf->Cell(15, 6, $data['sks_x_bobot'], 1, 0, 'C');
                    $pdf->Ln();
                }

                $pdf->Ln(4);
                $pdf->SetFont('Arial', 'B', 10);
                $pdf->Cell(45, 5, 'IPK', 0, 0);
                $pdf->Cell(0, 5, ': ' . number_format($ipk, 2), 0, 1);
                $pdf->Cell(45, 5, 'Jumlah Mata Kuliah', 0, 0);
                $pdf->Cell(0, 5, ': ' . $jumlah_mata_kuliah, 0, 1);
                $pdf->Cell(45, 5, 'Jumlah SKS', 0, 0);
                $pdf->Cell(0, 5, ': ' . $ipk_data['total_sks'], 0, 1);
                $pdf->Ln(5);

                $pdf->SetFont('Arial', '', 12);
                $pdf->Cell(110, 10, '', 0, 0);
                $pdf->Cell(70, 0, 'Pandeglang, ' . date('d F Y'), 0, 1);
                $pdf->Cell(10, 10, 'Kaprodi', 0, 0);
                $pdf->Cell(100, 10, '', 0, 0);
                $pdf->Cell(70, 10, 'Mahasiswa', 0, 1);
                $pdf->Ln(23);

                $pdf->SetFont('Arial', 'B', 12);
                $pdf->Cell(10, 0, $nmprodi, 0, 0);
                $pdf->Cell(100, 0, '', 0, 0);
                $pdf->Cell(70, 0, $nama_mahasiswa, 0, 1);
                $pdf->SetFont('Arial', '', 12);
                $pdf->Cell(10, 10, '', 0, 0);
                $pdf->Cell(100, 10, '', 0, 0);
                $pdf->Cell(70, 10, 'NIM: ' . $nim, 0, 1);

                $pdf->Output('D', 'transkrip_' . $nim . '.pdf');
                exit;
            }
        }
    }
}
