<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once('tcpdf/tcpdf.php');
require_once('db_connection.php');

if (!isset($_POST['CLASS'])) {
    die("No class selected.");
}

$class = $_POST['CLASS'];

$stmt = $pdo->prepare("SELECT * FROM STUDENTS WHERE CLASS = :class ORDER BY STUDENT_NAME");
$stmt->execute(['class' => $class]);
$students = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (!$students) {
    die("No students found for class: " . htmlspecialchars($class));
}

class MYPDF extends TCPDF {
    public function Header() {
        // Page dimensions
        $pageWidth = $this->getPageWidth();
        $pageHeight = $this->getPageHeight();

        // Full-page background
        $this->Image(
            'bg.png',
            0, 0,
            $pageWidth, $pageHeight,
            '', '', '', false, 300,
            '', false, false, 0,
            false, false, false
        );

        // === Pink Frame ===
        $this->SetLineWidth(1); // thickness of border
        $this->SetDrawColor(255, 105, 180); // pink (hot pink)
        $this->Rect(5, 5, $pageWidth - 10, $pageHeight - 10); 
        // leaves 5mm margin inside page

        // School logo
        $this->Image('logo.png', 10, 5, 15, 15, '', '', '', false, 300);

        // Title
        $this->SetFont('helvetica', 'B', 12);
        $this->SetTextColor(0, 102, 204);
        $this->Cell(0, 6, 'LUZIRA C/U PRE-PRIMARY SCHOOL', 0, 1, 'C');

        $this->SetFont('helvetica', '', 8);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(0, 5, 'P.O BOX 20061 KAMPALA | Tel: 0774867289 / 0701867289', 0, 1, 'C');

        $this->SetFillColor(200, 200, 200);
        $this->SetFont('helvetica', 'B', 10);
        $this->SetTextColor(153, 51, 0);
        $this->Cell(0, 6, 'TERM ONE PROGRESSIVE REPORT 2025', 0, 1, 'C', 1);
        $this->Ln(1);
    }

    public function Footer() {
        // Disable footer
    }
}

$pdf = new MYPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->setPrintHeader(true);
$pdf->setPrintFooter(false);

// No auto-page-breaks → fills entire page
$pdf->SetAutoPageBreak(false, 0);
$pdf->SetMargins(8, 40, 8);

$areas = [
    "Social development",
    "Language development 1",
    "Health habits",
    "Numeracy",
    "Language development 2",
    "Writing",
    "Physical development"
];

foreach ($students as $student) {
    $pdf->AddPage();

    $pdf->SetFont('helvetica', 'B', 9);
    $pdf->Cell(28, 6, "PUPIL'S NAME:", 0, 0);
    $pdf->SetFont('helvetica', '', 9);
    $pdf->Cell(70, 6, $student['STUDENT_NAME'], 'B', 0);
    $pdf->SetFont('helvetica', 'B', 9);
    $pdf->Cell(15, 6, 'CLASS:', 0, 0);
    $pdf->SetFont('helvetica', '', 9);
    $pdf->Cell(25, 6, $student['CLASS'], 'B', 0);
    $pdf->SetFont('helvetica', 'B', 9);
    $pdf->Cell(10, 6, 'SEX:', 0, 0);
    $pdf->SetFont('helvetica', '', 9);
    $pdf->Cell(20, 6, $student['SEX'], 'B', 1);
    $pdf->Ln(1);

    // Midterm
    $pdf->SetFont('helvetica', 'B', 9);
    $pdf->SetTextColor(0, 102, 204);
    $pdf->Cell(0, 6, 'MIDTERM ASSESSMENT', 0, 1, 'C');
    $pdf->SetTextColor(0, 0, 0);

    $pdf->SetFont('helvetica', 'B', 8);
    $pdf->Cell(60, 6, 'LEARNING AREAS', 1, 0, 'C');
    $pdf->Cell(90, 6, 'COMMENT', 1, 0, 'C');
    $pdf->Cell(20, 6, 'SIGN', 1, 1, 'C');

    $pdf->SetFont('helvetica', '', 8);
    foreach ($areas as $a) {
        $pdf->Cell(60, 5, $a, 1, 0);
        $pdf->Cell(90, 5, '', 1, 0);
        $pdf->Cell(20, 5, '', 1, 1);
    }
    $pdf->Ln(1);

    // End of Term
    $pdf->SetFont('helvetica', 'B', 9);
    $pdf->SetTextColor(0, 102, 204);
    $pdf->Cell(0, 6, 'END OF TERM ASSESSMENT', 0, 1, 'C');
    $pdf->SetTextColor(0, 0, 0);

    $pdf->SetFont('helvetica', 'B', 8);
    $pdf->Cell(60, 6, 'LEARNING AREAS', 1, 0, 'C');
    $pdf->Cell(90, 6, 'COMMENT', 1, 0, 'C');
    $pdf->Cell(20, 6, 'SIGN', 1, 1, 'C');

    $pdf->SetFont('helvetica', '', 8);
    foreach ($areas as $a) {
        $pdf->Cell(60, 5, $a, 1, 0);
        $pdf->Cell(90, 5, '', 1, 0);
        $pdf->Cell(20, 5, '', 1, 1);
    }
    $pdf->Ln(2);

    // Comments
    $pdf->SetFont('helvetica', 'B', 8);
    $pdf->Cell(35, 5, 'CLASS TEACHER:', 0, 0);
    $pdf->Cell(90, 5, '', 'B', 1);

    $pdf->Cell(45, 5, 'GENERAL COMMENTS:', 0, 0);
    $pdf->Cell(90, 5, '', 'B', 1);

    $pdf->Cell(20, 5, 'SIGN:', 0, 0);
    $pdf->Cell(40, 5, '', 'B', 1);

    $pdf->Cell(35, 5, 'HEAD TEACHER:', 0, 0);
    $pdf->Cell(90, 5, '', 'B', 1);

    $pdf->Cell(20, 5, 'SIGN:', 0, 0);
    $pdf->Cell(40, 5, '', 'B', 1);
    $pdf->Ln(2);

    // Requirements
    $pdf->SetFont('helvetica', 'B', 9);
    $pdf->Cell(0, 6, 'REQUIREMENTS', 0, 1, 'C');

    $pdf->SetFont('helvetica', 'B', 8);
    $pdf->SetFillColor(220, 220, 220);
    $pdf->Cell(80, 5, 'CLASS ROOM REQUIREMENTS', 1, 0, 'C', 1);
    $pdf->Cell(90, 5, 'Ug Shs: 20,000 (baby/middle), 22,000 for Top', 1, 1, 'C');

    $pdf->Cell(80, 5, 'REAM OF DUPLICATING PAPER', 1, 0, 'C', 1);
    $pdf->Cell(90, 5, 'DOLPHIN/KOPIER GOLD BIG SIZE', 1, 1, 'C');
    $pdf->Ln(1);

    // Next term details
    $pdf->SetFont('helvetica', 'B', 8);
    $pdf->Cell(65, 5, 'NEXT TERM BEGINS ON:', 0, 0, 'R');
    $pdf->Cell(40, 5, '________________', 0, 0, 'L');
    $pdf->Cell(60, 5, "NEXT TERM'S FEES: 153,000/=", 0, 1, 'L');
    $pdf->Ln(1);

    // Motto
    $pdf->SetFont('helvetica', 'B', 8);
    $pdf->SetTextColor(0, 102, 204);
    $pdf->Cell(0, 5, 'MOTTO: "THE SKY IS THE LIMIT"', 0, 1, 'C');
    $pdf->SetFont('helvetica', 'I', 7);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Cell(0, 4, 'class room requirements must be bought at school', 0, 1, 'C');
}

$pdf->Output("report_cards_{$class}_2025_Term1.pdf", 'I');
?>
