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

    function NbLines($w, $txt)
    {
        $cw = &$this->CurrentFont['cw'];
        if ($w == 0) $w = $this->w - $this->rMargin - $this->x;
        $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
        $s = str_replace("\r", '', $txt);
        $nb = strlen($s);
        if ($nb > 0 && $s[$nb - 1] == "\n") $nb--;
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while ($i < $nb) {
            $c = $s[$i];
            if ($c == "\n") {
                $i++; $sep = -1; $j = $i; $l = 0; $nl++;
                continue;
            }
            if ($c == ' ') $sep = $i;
            $l += $cw[$c];
            if ($l > $wmax) {
                if ($sep == -1) {
                    if ($i == $j) $i++;
                } else {
                    $i = $sep + 1;
                }
                $sep = -1; $j = $i; $l = 0; $nl++;
            } else {
                $i++;
            }
        }
        return $nl;
    }

    function Row($data, $widths, $height)
    {
        $nb = 0;
        for ($i = 0; $i < count($data); $i++) {
            $nb = max($nb, $this->NbLines($widths[$i], $data[$i]));
        }
        $h = $height * $nb;

        if ($this->GetY() + $h > $this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);

        for ($i = 0; $i < count($data); $i++) {
            $w = $widths[$i];
            $x = $this->GetX();
            $y = $this->GetY();

            $this->Rect($x, $y, $w, $h);
            $this->MultiCell($w, $height, $data[$i], 0, 'L');
            $this->SetXY($x + $w, $y);
        }
        $this->Ln($h);
    }
}

class DownloadDataPMBController extends Controller
{
    public function downloadHasil()
    {
        $db = new Database();
        $conn = $db->getConnection();

        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tahun'])) {
            $tahun = $_POST['tahun'];

            $stmt = $conn->prepare("
                SELECT pmb.nisn, pmb.nama_camaba, pmb.asal_sekolah, pmb.wa, prodi.nama_prodi 
                FROM tb_pmb AS pmb
                LEFT JOIN tb_prodi AS prodi ON pmb.id_prodi = prodi.id_prodi
                WHERE pmb.tahun_masuk = ?
                ORDER BY pmb.nama_camaba ASC
            ");
            $stmt->execute([$tahun]);
            $data = $stmt->fetchAll(\PDO::FETCH_ASSOC);

            $pdf = new PDF('P', 'mm', 'A4');
            $pdf->AddPage();

            // Judul di bawah kop surat
            $pdf->SetFont('Arial', 'B', 14);
            $pdf->Cell(0, 10, 'DAFTAR CAMABA', 0, 1, 'C');
            $pdf->Cell(0, 10, 'STKIP BABUNNAJAH PANDEGLANG', 0, 1, 'C');
            $pdf->Cell(0, 10, 'TAHUN ' . $tahun, 0, 1, 'C');
            $pdf->Ln(5);

            $pdf->SetFont('Arial', 'B', 11);
            $pdf->SetFillColor(200, 220, 255);

            // Header kolom
            $colWidths = [10, 30, 50, 45, 30, 25];
            $header = ['No', 'NISN', 'Nama', 'Asal Sekolah', 'No WA', 'Prodi'];

            for ($i = 0; $i < count($header); $i++) {
                $pdf->Cell($colWidths[$i], 10, $header[$i], 1, 0, 'C', true);
            }
            $pdf->Ln();

            // Data baris
            $pdf->SetFont('Arial', '', 10);
            $no = 1;
            foreach ($data as $row) {
                $prodi = ($row['nama_prodi'] == 'Pendidikan Guru Sekolah Dasar') ? 'PGSD' : 'PGPAUD';
                $pdf->Row([
                    $no++,
                    $row['nisn'],
                    $row['nama_camaba'],
                    $row['asal_sekolah'],
                    $row['wa'],
                    $prodi
                ], $colWidths, 10);
            }

            $pdf->Output("I", "Data_PMB_$tahun.pdf");
        } else {
            echo "Tahun belum dipilih.";
        }
    }
}
