<?php

namespace App\Controllers;

use Core\Controller;
use Core\Database;

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

class PDF extends \FPDF
{
    private $data;
    private $tahun;
    private $nama;
    private $kaprod;

    function Header()
    {
        $this->Image(__DIR__ . '/../../public/assets/img/kop_surat.jpg', 3, 3, 205);
    }

    function formatTanggalIndo($tanggal)
    {
        $bulanIndo = [
            '01' => 'Januari', '02' => 'Februari', '03' => 'Maret', '04' => 'April',
            '05' => 'Mei', '06' => 'Juni', '07' => 'Juli', '08' => 'Agustus',
            '09' => 'September', '10' => 'Oktober', '11' => 'November', '12' => 'Desember'
        ];
        $date = new \DateTime($tanggal);
        $day = $date->format('d');
        $month = $bulanIndo[$date->format('m')];
        $year = $date->format('Y');
        return "$day $month $year";
    }

    function Footer()
    {
        $this->SetY(-60);
        $this->Cell(85, 15, '', 0, 0, 'L');
        $this->Cell(35, 15, '', 0, 0, 'L');
        $this->Cell(85, 15, 'Pandeglang, ' . $this->formatTanggalIndo(date('Y-m-d')), 0, 0, 'L');

        $this->Ln(5);
        $this->Cell(85, 15, 'Petugas Pendaftaran', 0, 0, 'L');
        $this->Cell(35, 15, '', 0, 0, 'L');
        $this->Cell(85, 15, 'Mahasiswa YBS', 0, 0, 'L');

        $this->Ln(30);
        $this->SetFont('Times', 'B', 12);
        $this->Cell(85, 15, '-------------------------------', 0, 0, 'L');
        $this->Cell(35, 15, '', 0, 0, 'L');
        $this->Cell(85, 15, $this->nama, 0, 0, 'L');
    }

    function LoadData($nim, $db)
    {
        $stmt = $db->prepare("SELECT * FROM tb_yudisium 
            INNER JOIN tb_prodi ON tb_yudisium.id_prodi = tb_prodi.id_prodi
            WHERE nim = ?");
        $stmt->execute([$nim]);
        $this->data = $stmt->fetch(\PDO::FETCH_ASSOC);

        if ($this->data) {
            $this->tahun = date('Y');
            $stmt = $db->prepare("SELECT tm.nama, tp.kaprodi
                                  FROM tb_mahasiswa tm
                                  INNER JOIN tb_prodi tp ON tm.id_prodi = tp.id_prodi
                                  WHERE tm.nim = ?");
            $stmt->execute([$nim]);
            $r = $stmt->fetch(\PDO::FETCH_ASSOC);
            if ($r) {
                $this->kaprod = $r['kaprodi'];
                $this->nama = $r['nama'];
            }
        }
    }

    function PageContent()
    {
        if ($this->data) {
            $this->SetFont('Times', 'B', 14);
            $this->Cell(0, 35, '', 0, 1, 'C');
            $this->Cell(0, 10, 'FORMULIR PENDAFTARAN YUDISIUM', 0, 1, 'C');
            $this->Cell(0, 10, 'STKIP BABUNNAJAH PANDEGLANG', 0, 1, 'C');
            $this->Ln(10);

            $this->SetFont('Times', '', 12);
            $this->Cell(35, 7, "Nama", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['nama'], 0, 1, 'L');
            $this->Cell(35, 7, "NIM", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['nim'], 0, 1, 'L');
            $this->Cell(35, 7, "Program Studi", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['nama_prodi'], 0, 1, 'L');
            $this->Cell(35, 7, "NIK", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['nik'], 0, 1, 'L');
            $this->Cell(35, 7, "NISN", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['nisn'], 0, 1, 'L');
            $this->Cell(35, 7, "Tempat/Tgl Lahir", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['tempat_lahir'] . ', ' . $this->formatTanggalIndo($this->data['tanggal_lahir']), 0, 1, 'L');
            $this->Cell(35, 7, "JK", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['gender'], 0, 1, 'L');
            $this->Cell(35, 7, "Alamat", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['alamat'], 0, 1, 'L');
            $this->Cell(35, 7, "Angkatan", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['tahun_masuk'], 0, 1, 'L');
            $this->Cell(35, 7, "Judul Skripsi", 0, 0, 'L');
            $this->MultiCell(0, 8, ": " . $this->data['judul_skripsi']);
            $this->Cell(35, 7, "Tgl Sidang", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->formatTanggalIndo($this->data['tgl_sidang']), 0, 1, 'L');
            $this->Cell(35, 7, "Pembimbing 1", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['dospem1'], 0, 1, 'L');
            $this->Cell(35, 7, "Pembimbing 2", 0, 0, 'L');
            $this->Cell(0, 7, ": " . $this->data['dospem2'], 0, 1, 'L');
            $this->Ln(10);

            $this->MultiCell(0, 10, "Selaku calon peserta yudisium saya bersedia mentaati tata tertib pelaksanaan Yudisium Tahun {$this->tahun} sebagaimana yang telah ditentukan (terutama Wajib mengikuti Yudisium, Gladi Bersih, dan Tidak Meninggalkan Ruangan sebelum acara selesai).");
            $this->Ln(10);
        } else {
            $this->SetFont('Times', 'B', 12);
            $this->Cell(0, 10, 'Data tidak ditemukan.', 0, 1, 'C');
        }
    }

    function AddPage2()
    {
        $this->AddPage();
        $this->Ln(30);
        $this->SetFont('Times', 'B', 16);
        $this->Cell(0, 20, 'DAFTAR CEKLIS', 0, 1, 'C');
        $this->Ln(5);
        $this->SetFont('Times', '', 12);
        $this->MultiCell(0, 10, "Bersama ini saya lampirkan persyaratan-persyaratan sebagai berikut:");
        $this->Ln(10);

        $checklistItems = [
            "Formulir Pendaftaran Yudisium sudah diisi dengan benar.",
            "Fotokopi KTP yang masih berlaku.",
            "Fotokopi Kartu Keluarga (KK).",
            "Fotokopi Ijazah SMA.",
            "Fotokopi Akta Lahir.",
            "Skripsi yang telah disetujui oleh dosen pembimbing, penguji dan dibubuhi materai.",
            "KHS semester 1 s/d semester akhir",
            "Bukti lolos plagiasi Jurnal"
        ];

        foreach ($checklistItems as $item) {
            $this->Cell(10, 10, '', 1, 0, 'C');
            $this->Cell(7, 10, ' ', 0, 0, 'C');
            $this->Cell(30, 10, $item, 0, 1, 'L');
            $this->Ln(4);
        }

        $this->Ln(10);
        $this->Cell(0, 10, 'Catatan: Mohon pastikan semua berkas telah dilengkapi sebelum hari H.', 0, 1, 'L');
    }
}

class PDFYudisiumController extends Controller
{
    public function hasilPDF()
    {
        $nim = $_SESSION['user']['nim'] ?? null;
        $nama = $_SESSION['user']['nama'] ;

        if (!$nim) {
            echo "<div class='alert alert-danger'>NIM tidak ditemukan dalam sesi.</div>";
            exit;
        }

        $db = Database::getConnection();
        $pdf = new PDF();
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->LoadData($nim, $db);
        $pdf->PageContent();
        $pdf->AddPage2();

        ob_end_clean();
        $pdf->Output("Formulir_yudisium_{$nim}_{$nama}.pdf", 'I');
    }
}
