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

require_once("public/connection.php");
require_once("public/functions.php");
require_once __DIR__ . "/tcpdf/tcpdf.php";

// Fetch school details
$query = "SELECT * FROM `administratives` LIMIT 1";
$run_query = mysqli_query($connection, $query);
if (!$run_query) die("Database query failed: " . mysqli_error($connection));
$school = mysqli_fetch_assoc($run_query);
if (!$school) die("No school data found.");

// Determine logo path
$logoPath = __DIR__ . '/' . ltrim($school['school_logo'], '/'); 
if (!file_exists($logoPath)) die("Logo file not found: " . $logoPath);

// Initialize PDF
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator('School Portal');
$pdf->SetAuthor($school['school_name']);
$pdf->SetTitle('Admission Form');
$pdf->SetMargins(15, 20, 15);
$pdf->SetAutoPageBreak(true, 20);
$pdf->AddPage();

// Watermark
$pdf->SetAlpha(0.08);
$pdf->Image($logoPath, 20, 60, 170, '', '', '', false, 300);
$pdf->SetAlpha(1);

// Header
$pdf->SetFont('helvetica', '', 12);
$htmlHeader = '
<table width="100%" cellpadding="5">
<tr>
<td width="25%"><img src="'.$logoPath.'" width="60"></td>
<td width="50%" align="center">
<h1 style="color:#2A2A72; font-size:16pt;">'.$school['school_name'].'</h1>
<p style="font-size:10pt;">'.$school['school_address'].'<br>
Phone: '.$school['school_phone'].' | Website: '.$school['school_website'].' | Email: '.$school['school_mail'].'</p>
<p style="font-weight:bold; font-size:11pt;">'.$school['school_motto'].'</p>
</td>
<td width="25%" align="center">
<b>PASSPORT</b><br>
<div style="border:1px solid #000; width:100px; height:120px;"></div>
</td>
</tr>
</table><hr style="border:1px solid #2A2A72;">';
$pdf->writeHTML($htmlHeader, true, false, true, false, '');

// Function to draw input box
function drawBoxField($pdf, $label, $width=0, $height=8) {
    $pdf->SetFont('helvetica','',10);
    $pdf->Cell(50, $height, $label, 0, 0);
    $pdf->Cell($width, $height, '', 1, 1); // Draw a box for input
}

// ------------------- STUDENT INFORMATION -------------------
$pdf->Ln(5);
$pdf->SetFont('helvetica','B',12);
$pdf->SetFillColor(42, 42, 114);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(0,8,'STUDENT INFORMATION',0,1,'L',true);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('helvetica','',10);
$pdf->Ln(2);

drawBoxField($pdf,'First Name',120);
drawBoxField($pdf,'Last Name',120);
drawBoxField($pdf,'Other Name',120);
drawBoxField($pdf,'Date of Birth',120);
drawBoxField($pdf,'Gender',120);
drawBoxField($pdf,'Nationality',120);
drawBoxField($pdf,'State of Origin',120);
drawBoxField($pdf,'LGA',120);
drawBoxField($pdf,'Last School Attended',120);
drawBoxField($pdf,'Last Completed Class',120);
drawBoxField($pdf,'Class Applied For',120);

// ------------------- SPONSOR 1 -------------------
$pdf->Ln(5);
$pdf->SetFont('helvetica','B',12);
$pdf->SetFillColor(42, 42, 114);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(0,8,'SPONSOR 1 DETAILS (FATHER)',0,1,'L',true);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('helvetica','',10);
$pdf->Ln(2);

drawBoxField($pdf,'Title',120);
drawBoxField($pdf,'Surname',120);
drawBoxField($pdf,'Other Name',120);
drawBoxField($pdf,'Address',120);
drawBoxField($pdf,'Occupation',120);
drawBoxField($pdf,'Email Address',120);
drawBoxField($pdf,'Phone Number',120);
drawBoxField($pdf,'Alternative Phone Number',120);

// ------------------- SPONSOR 2 -------------------
$pdf->Ln(5);
$pdf->SetFont('helvetica','B',12);
$pdf->SetFillColor(42, 42, 114);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(0,8,'SPONSOR 2 DETAILS (MOTHER)',0,1,'L',true);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('helvetica','',10);
$pdf->Ln(2);

drawBoxField($pdf,'Title',120);
drawBoxField($pdf,'Surname',120);
drawBoxField($pdf,'Other Name',120);
drawBoxField($pdf,'Address',120);
drawBoxField($pdf,'Occupation',120);
drawBoxField($pdf,'Email Address',120);
drawBoxField($pdf,'Phone Number',120);
drawBoxField($pdf,'Alternative Phone Number',120);

// ------------------- OTHER DETAILS -------------------
$pdf->Ln(5);
$pdf->SetFont('helvetica','B',12);
$pdf->SetFillColor(42, 42, 114);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(0,8,'OTHER DETAILS',0,1,'L',true);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('helvetica','',10);
$pdf->Ln(2);

drawBoxField($pdf,'Student Language Spoken',120);
drawBoxField($pdf,'Learning Challenges?',120);
drawBoxField($pdf,'How did you hear about us?',120);

// ------------------- DECLARATION -------------------
$pdf->Ln(5);
$pdf->SetFont('helvetica','B',12);
$pdf->SetFillColor(42, 42, 114);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(0,8,'DECLARATION',0,1,'L',true);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('helvetica','',10);
$pdf->Ln(2);

drawBoxField($pdf,'Parent / Guardian Signature',120);
drawBoxField($pdf,'Date',120);

// ------------------- Output PDF -------------------
ob_end_clean();
$fileName = 'Admission_Form_'.$school['school_short_name'].'.pdf';
$pdf->Output($fileName, 'D');
exit;
