<?php
ob_start();
chdir('../');
require_once('assets/vendor/tcpdf/tcpdf.php');
require_once("public/connection.php");
require_once("public/functions.php");

// Validate input
if (!isset($_GET['term'])) {
    die("No term selected.");
}

$view_pin_term = $_GET['term'];
$term_table_map = [
    "First Term" => ["table" => "pin1", "column" => "first_term_pin"],
    "Second Term" => ["table" => "pin2", "column" => "second_term_pin"],
    "Third Term" => ["table" => "pin3", "column" => "third_term_pin"]
];

if (!isset($term_table_map[$view_pin_term])) {
    die("Invalid term.");
}

$table_name = $term_table_map[$view_pin_term]["table"];
$pin_column = $term_table_map[$view_pin_term]["column"];

// Fetch PINs
$query = "SELECT * FROM `$table_name` WHERE `term` = '{$view_pin_term}'";
$run_query = mysqli_query($connection, $query);
if (!$run_query || mysqli_num_rows($run_query) === 0) {
    die("No pins found for {$view_pin_term}.");
}

$all_pins = [];
while ($result = mysqli_fetch_assoc($run_query)) {
    $all_pins[] = [
        'serial_number' => $result['serial_number'],
        'pin' => $result[$pin_column]
    ];
}

// School info
$school_name = '';
$school_short_name = '';
$school_website = '';
$school_query = "SELECT * FROM `administratives`";
$school_result = mysqli_query($connection, $school_query);
if ($school_result && mysqli_num_rows($school_result) > 0) {
    $school_data = mysqli_fetch_assoc($school_result);
    $school_name = $school_data['school_name'];
    $school_short_name = $school_data['school_short_name'];
    $school_website = $school_data['school_website'];
}

// TCPDF setup
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($school_short_name);
$pdf->SetTitle("{$view_pin_term} Result Pins");
$pdf->SetMargins(10, 10, 10);
$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();

// Title
$pdf->SetFont("Helvetica", 'B', 14);
$pdf->Cell(0, 10, "{$view_pin_term} Result Pins", 0, 1, 'C');
$pdf->Ln(2);

// Layout settings
$pin_width = 90;  // Half of A4 minus margins
$pin_height = 50;
$pins_per_page = 12;

$x_start = 10;
$y = $pdf->GetY();
$x = $x_start;

$pdf->SetFont("Helvetica", '', 10);
$count = 0;

foreach ($all_pins as $index => $pin) {
    if ($count && $count % $pins_per_page == 0) {
        $pdf->AddPage();
        $pdf->SetFont("Helvetica", 'B', 14);
        $pdf->Cell(0, 10, "{$view_pin_term} Result Pins", 0, 1, 'C');
        $pdf->Ln(2);
        $y = $pdf->GetY();
    }

    if ($count % 2 == 0) {
        $x = $x_start;
    } else {
        $x = $x_start + $pin_width;
    }

    // Draw cell
    $pdf->SetXY($x, $y);
    $pdf->SetFont("Helvetica", 'B', 11);
    $pdf->MultiCell($pin_width, 7, "{$school_short_name} {$view_pin_term} Result Pin", 1, 'C');
    $pdf->SetFont("Helvetica", '', 10);
    $pdf->SetX($x); $pdf->MultiCell($pin_width, 6, "S/N: {$pin['serial_number']}", 1);
    $pdf->SetX($x); $pdf->MultiCell($pin_width, 6, "PIN: {$pin['pin']}", 1);
    $pdf->SetX($x); $pdf->SetTextColor(255, 0, 0);
    $pdf->MultiCell($pin_width, 6, "NOTE: You can only Use this Pin 5 Times", 1, 'C');
    $pdf->SetX($x); $pdf->SetTextColor(0, 0, 255);
    $pdf->MultiCell($pin_width, 6, "{$school_website}", 1, 'C');
    $pdf->SetTextColor(0, 0, 0);

    if ($count % 2 == 1) {
        $y += $pin_height;
    }

    $count++;
}

ob_end_clean();
$pdf->Output("{$school_short_name}_{$view_pin_term}_Result_Pins.pdf", 'I');

