<?php
include('session.php');
require('./fpdf/pdf-a4-lanscape01.php');

//error_reporting(E_ALL);
//ini_set('display_errors', 1);

    $strid = $_REQUEST['id'];
    $strcategory = $_REQUEST['category'];
    $strverified = $_REQUEST['verified'];

    $strValue01 = [];
    $strValue02 = [];
    $strValue03 = [];
    $strValue04 = [];
    $strValue05 = [];
    $strValue06 = [];
    $strValue07 = [];
    $strValue08 = [];
    $strValue09 = [];

    $where = [];
                                                                
    // Category filter
    if($strcategory != "ALL"){
        $where[] = "Category = '".mysqli_real_escape_string($conn, $strcategory)."'";
    }
    
    if($strverified == "VERIFIED"){
        $where[] = "VerifyDateTime IS NOT NULL";
    }
    elseif($strverified == "PENDING VERIFICATION"){
        $where[] = "VerifyDateTime IS NULL";
    }
    
    // Build SQL
    $sql = "SELECT * FROM $tblquizict2026";
    
    if(!empty($where)){
        $sql .= " WHERE " . implode(" AND ", $where);
    }
    
    $sql .= " ORDER BY FullName ASC";
                                                                
    $a = 0;
    
    //echo $sql;
    
    
    if($result=mysqli_query($conn,$sql)){
        while ($row=mysqli_fetch_array($result)){
            $a=$a+1;
            
            $strValue01 [] = strtoupper($row["FullName"]);
            $strValue02 [] = strtolower($row["EmailAdd"]);
            $strValue03 [] = strtoupper($row["SchoolName"]);
            $strValue04 [] = $row["Age"];
            $categoryParts = explode(':', $row["Category"]);
            $strValue05[] = strtoupper(trim($categoryParts[0]));
            //$strValue05 [] = strtoupper($row["Category"]);
            $strValue06 [] = strtoupper($row["NRICMyKid"]);
            $strValue07 [] = strtoupper($row["Platform"]);
            if (!empty($row["VerifyDateTime"]) && strtotime($row["VerifyDateTime"])) {
                $strValue08[] = date("d-m-Y H:i:s", strtotime($row["VerifyDateTime"]));
            } else {
                $strValue08[] = '';
            }
            $strValue09 [] = $row["ContactNo"];
            
        }
    }
    

$number_of_records = $a;

class PDF extends FPDF
{
    private $isLastPage = false;
    
    private $pageNumber = 0;
    function AddNewPageWithFieldNames($Y_Fields_Name_position)
    //function AddNewPageWithFieldNames($pdf, $Y_Fields_Name_position, $strValue01,$strValue02,$strValue03,$strValue04,$strValue05,$strValue06,$strValue07,$strValue08)
    {
        $this->AddPage();
        $this->SetFillColor(232, 232, 232);
        $this->SetFont('Arial', 'B', 8);
        $this->SetY($Y_Fields_Name_position);
        $this->SetX(12);
        $this->Cell(10, 6, 'NO', 1, 0, 'C', 1);
        $this->SetX(22);
        $this->Cell(80, 6, 'NAME', 1, 0, 'C', 1);
        $this->SetX(102);
        $this->Cell(10, 6, 'AGE', 1, 0, 'C', 1);
        $this->SetX(112);
        $this->Cell(20, 6, 'MYKID / NRIC', 1, 0, 'C', 1);
        $this->SetX(132);
        $this->Cell(20, 6, 'CONTACT NO', 1, 0, 'C', 1);
        $this->SetX(152);
        $this->Cell(40, 6, 'EMAIL', 1, 0, 'C', 1);
        $this->SetX(192);
        $this->Cell(20, 6, 'PLATFORM', 1, 0, 'C', 1);
        $this->SetX(212);
        $this->Cell(42, 6, 'CATEGORY', 1, 0, 'C', 1);
        $this->SetX(254);
        $this->Cell(30, 6, 'VERIFY ON', 1, 0, 'C', 1);
        $this->Ln();

    }

    function Header()
{
    
    //global $schoolnamebm, $schoolnamebt, $schooladdl1, $schooladdl2;
    //global $classsession;
    
    
    $compname = "NKN ACADEMY & TRAINING PROVIDER";
    $compadd = "NO 1234 FIRST FLOOR LORONG, KS 1/3 TAMAN KULIM SQUARE, LUNAS, 09600 KULIM KEDAH DARUL AMAN.";
    $compadd01 = "Tel : +6016-487 6512 E-Mail : kavinyar98@gmail.com";
    $systemtitle = "NATIONAL LEVEL STEM & ICT QUIZ COMPETITION 2026";

    if (file_exists('images/nkn-logo-horizontal.png')) {
        $this->Image('images/nkn-logo-horizontal.png', 10, 10, 15);
    } else {
        die('Image not found.');
    }

    $this->SetFont('Arial', 'B', 9);
    $this->SetY(8);
    $this->SetX(30);
    $this->Cell(0, 7, $compname, 0, 1, 'L');
    $this->SetY(12);
    $this->SetX(30);
    $this->SetFont('Arial', '', 8);
    $this->Cell(0, 7, $compadd, 0, 1, 'L');
    $this->SetY(16);
    $this->SetX(30);
    $this->Cell(0, 7, $compadd01, 0, 1, 'L');
    $this->Line(10, 24, 285, 24);
    
    $this->SetX(30);
    $this->SetY(22);
    $this->SetFont('Arial', 'B', 9);
    $this->Cell(0, 12, $systemtitle, 0, 1, 'C');
    
    $this->SetY(45);
    $this->SetX(0);
    $this->SetFont('Arial', 'B', 9);
    //$this->Cell(0, 12, "CATEGORY : ". $searchcategory. " | DATE : ".date("d-m-Y", strtotime($searchbdate))." - ". date("d-m-Y", strtotime($searchedate)) , 0, 1, 'C');

}

    function Footer()
    {

    }
}

function checkAddPage($pdf, $heightNeeded) {
    // Check if the remaining space is less than the height needed
    if ($pdf->GetY() + $heightNeeded > 280) { // 280 ~ A4 height minus margin
        $pdf->AddPage();
    }
}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->SetX(5);
$pdf->SetY(55);

$Y_Fields_Name_position = 35;//50;
$recordsPerPage = 25;

$totalPages = ceil($number_of_records / $recordsPerPage);

$pdf->AddNewPageWithFieldNames($Y_Fields_Name_position);


for ($x = 0; $x < $number_of_records; $x++) {
    if ($x > 0 && $x % $recordsPerPage == 0) {
        $pdf->AddNewPageWithFieldNames($Y_Fields_Name_position);
    }

    $pdf->SetFont('Arial', '', 6);
    $pdf->SetX(12);
    $pdf->Cell(10, 6, $x + 1, 1, 0, 'C');
    $pdf->SetX(22);
    $pdf->Cell(80, 6, $strValue01[$x], 1, 0, 'L');
    $pdf->SetX(102);
    $pdf->Cell(10, 6, $strValue04[$x], 1, 0, 'C');
    $pdf->SetX(112);
    $pdf->Cell(20, 6, $strValue06[$x], 1, 0, 'C');
    $pdf->SetX(132);
    $pdf->Cell(20, 6, $strValue09[$x], 1, 0, 'C');
    $pdf->SetX(152);
    $pdf->Cell(40, 6, $strValue02[$x], 1, 0, 'L');
    $pdf->SetX(192);
    $pdf->Cell(20, 6, $strValue07[$x], 1, 0, 'C');
    $pdf->SetX(212);
    $pdf->Cell(42, 6, $strValue05[$x], 1, 0, 'C');
    $pdf->SetX(254);
    $pdf->Cell(30, 6, $strValue08[$x], 1, 0, 'C');
    
    
    $pdf->Ln();
}



$pdf->Output();
?>
