<?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'); // Contains valid $pdo

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));
}

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetMargins(10, 10, 10);
$pdf->SetAutoPageBreak(TRUE, 10);

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

    // ===== HEADER =====
    $pdf->SetFont('helvetica', 'B', 14);
    $pdf->Cell(0, 8, 'LUZIRA C/U PRE-PRIMARY SCHOOL', 0, 1, 'C');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->Cell(0, 6, 'P.O BOX 20061 KAMPALA', 0, 1, 'C');
    $pdf->Cell(0, 6, 'Tel: 0774867289 / 0701867289', 0, 1, 'C');
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetFillColor(220, 220, 220);
    $pdf->Cell(0, 8, 'TERM ONE PROGRESSIVE REPORT 2025', 0, 1, 'C', 1);
    $pdf->Ln(3);

    // ===== PUPIL INFO =====
    $pdf->SetFont('helvetica', '', 11);
    $pdf->Cell(30, 7, "PUPIL'S NAME:", 0, 0);
    $pdf->Cell(80, 7, $student['STUDENT_NAME'], 'B', 0);
    $pdf->Cell(15, 7, 'CLASS:', 0, 0);
    $pdf->Cell(30, 7, $student['CLASS'], 'B', 0);
    $pdf->Cell(10, 7, 'SEX:', 0, 0);
    $pdf->Cell(20, 7, $student['SEX'], 'B', 1);
    $pdf->Ln(3);

    // ===== MIDTERM ASSESSMENT =====
    $pdf->SetFont('helvetica', 'B', 11);
    $pdf->Cell(0, 7, 'MIDTERM ASSESSMENT', 0, 1, 'C');
    $pdf->SetFont('helvetica', 'B', 10);

    $pdf->Cell(60, 8, 'LEARNING AREAS', 1, 0, 'C');
    $pdf->Cell(100, 8, 'COMMENT', 1, 0, 'C');
    $pdf->Cell(20, 8, 'SIGN', 1, 1, 'C');

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

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

    // ===== END OF TERM ASSESSMENT =====
    $pdf->SetFont('helvetica', 'B', 11);
    $pdf->Cell(0, 7, 'END OF TERM ASSESSMENT', 0, 1, 'C');
    $pdf->SetFont('helvetica', 'B', 10);

    $pdf->Cell(60, 8, 'LEARNING AREAS', 1, 0, 'C');
    $pdf->Cell(100, 8, 'COMMENT', 1, 0, 'C');
    $pdf->Cell(20, 8, 'SIGN', 1, 1, 'C');

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

    // ===== COMMENTS & SIGNATURES =====
    $pdf->SetFont('helvetica', '', 10);
    $pdf->Cell(40, 8, 'CLASS TEACHER:', 0, 0);
    $pdf->Cell(120, 8, '', 'B', 1);

    $pdf->Cell(40, 8, 'GENERAL COMMENTS:', 0, 0);
    $pdf->Cell(120, 8, '', 'B', 1);

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

    $pdf->Cell(40, 8, 'HEAD TEACHER:', 0, 0);
    $pdf->Cell(120, 8, '', 'B', 1);

    $pdf->Cell(30, 8, 'SIGN:', 0, 0);
    $pdf->Cell(40, 8, '', 'B', 1);
    $pdf->Ln(6);

    // ===== REQUIREMENTS =====
    $pdf->SetFont('helvetica', 'B', 11);
    $pdf->Cell(0, 8, 'REQUIREMENTS', 0, 1, 'C');

    $pdf->SetFont('helvetica', '', 10);
    $pdf->MultiCell(0, 6, 
        "CLASS ROOM REQUIREMENTS: Ug Shs: 20,000 (baby/middle), 22,000 for Top\n" .
        "REAM OF DUPLICATING PAPER: DOLPHIN/KOPIER GOLD BIG SIZE\n\n" .
        "NEXT TERM BEGINS ON: __________     NEXT TERM'S FEES: 153,000/=\n\n" .
        "MOTTO: 'THE SKY IS THE LIMIT'\n\n" .
        "class room requirements must be bought at school", 
        0, 'L', 0, 1
    );
}

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