<?php
require("dbconnect.php");
require("fpdf.php");

if (!isset($_GET['id'])) {
    die("Invalid Demand ID");
}
$demand_id = intval($_GET['id']);

// ---------------- FETCH DATA ----------------
$header = $con->query("SELECT * FROM demand_header WHERE id=$demand_id")->fetch_assoc();
$trades = $con->query("SELECT * FROM demand_trades WHERE demand_id=$demand_id");
$terms = $con->query("SELECT * FROM demand_terms WHERE demand_id=$demand_id")->fetch_assoc();
$footer = $con->query("SELECT * FROM demand_footer WHERE demand_id=$demand_id")->fetch_assoc();

// ---------------- CREATE PDF ----------------
$pdf = new FPDF('P','mm','A4');
$pdf->SetAutoPageBreak(true, 20);
$pdf->AddPage();

// ---------------- BACKGROUND ----------------
$pdf->SetFillColor(245, 248, 250);
$pdf->Rect(0, 0, 210, 297, 'F');

// ---------------- LOGOS ----------------
$logoHeight = 20;
if (!empty($header['logo_left']) && file_exists("uploads/".$header['logo_left'])) {
    $pdf->Image("uploads/".$header['logo_left'], 15, 10, 0, $logoHeight);
}
if (!empty($header['logo_right']) && file_exists("uploads/".$header['logo_right'])) {
    $pdf->Image("uploads/".$header['logo_right'], 165, 10, 0, $logoHeight);
}

// ---------------- HEADER TITLE ----------------
$pdf->Ln($logoHeight + 2);
$pdf->SetFont('Arial','B',16);
$pdf->SetTextColor(0, 0, 100);
$pdf->Cell(0, 10, strtoupper($header['title']), 0, 1, 'C');

$pdf->SetFont('Arial','B',14);
$pdf->SetTextColor(52, 73, 94);  // dark grey-blue
$pdf->Cell(0, 8, strtoupper($header['company_name']), 0, 1, 'C');

$pdf->SetFont('Arial','',12);
$pdf->SetTextColor(80, 80, 80);
$pdf->Cell(0, 7, strtoupper(trim($header['visa_type']." - ".$header['selection_type'])), 0, 1, 'C');
$pdf->Ln(5);

// ---------------- TRADES TABLE ----------------
$pdf->SetFont('Arial','B',12);
$pdf->SetFillColor(52, 152, 219);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(12,10,"#",1,0,'C',true);
$pdf->Cell(80,10,"Trade",1,0,'C',true);
$pdf->Cell(30,10,"Qty",1,0,'C',true);
$pdf->Cell(68,10,"Salary",1,1,'C',true);

$pdf->SetFont('Arial','',11);
$pdf->SetTextColor(0,0,0);
$sn = 1;
$fill = false;
while ($row = $trades->fetch_assoc()) {
    $pdf->SetFillColor($fill ? 245 : 255, 255, 255);
    $pdf->Cell(12, 8, $sn++, 1, 0, 'C', true);
    $pdf->Cell(80, 8, $row['trade'], 1, 0, 'L', true);
    $pdf->Cell(30, 8, $row['qty'], 1, 0, 'C', true);
    $pdf->Cell(68, 8, $row['salary'], 1, 1, 'C', true);
    $fill = !$fill;
}
$pdf->Ln(8);

// ---------------- TERMS TABLE ----------------
$pdf->SetFont('Arial','B',13);
$pdf->SetTextColor(0, 0, 80);
$pdf->Cell(0, 9, 'TERMS & CONDITIONS', 0, 1, 'L');

$pdf->SetFont('Arial','B',11);
$pdf->SetFillColor(200, 220, 255);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(70, 8, 'TERM', 1, 0, 'C', true);
$pdf->Cell(120, 8, 'DETAILS', 1, 1, 'C', true);

$pdf->SetFont('Arial','',11);
$termsArr = [
    "Duty Hours" => $terms['duty_hours'],
    "Age Limit" => $terms['age_limit'],
    "Contract Period" => $terms['contract_period'],
    "Medical" => $terms['medical'],
    "Accommodation & Transport" => $terms['accommodation'],
];

$fill = false;
foreach ($termsArr as $label => $value) {
    $pdf->SetFillColor($fill ? 245 : 255, 255, 255);
    $pdf->Cell(70, 8, $label, 1, 0, 'L', true);
    $pdf->Cell(120, 8, $value, 1, 1, 'L', true);
    $fill = !$fill;
}
$pdf->Ln(10);

// ---------------- FOOTER (AGENCY INFO) ----------------
$footerBoxStartY = $pdf->GetY();

// Light grey background box
$pdf->SetFillColor(230, 230, 230);
$pdf->Rect(10, $footerBoxStartY, 190, 40, 'F');

// Agency Logo
if (!empty($footer['agency_logo']) && file_exists("uploads/".$footer['agency_logo'])) {
    $pdf->Image("uploads/".$footer['agency_logo'], 15, $footerBoxStartY + 2, 0, 18);
}

// Agency Name (Centered & Colored)
$pdf->SetY($footerBoxStartY + 2);
$pdf->SetFont('Arial','B',13);
$pdf->SetTextColor(41, 128, 185);
$pdf->Cell(0, 8, strtoupper($footer['agency_name']), 0, 1, 'C');

$pdf->SetFont('Arial','',11);
$pdf->SetTextColor(60, 60, 60);

$pdf->SetY($footerBoxStartY + 12);

// Replace emojis with text icons as FPDF does not support emojis
$pdf->Cell(0, 6, "Address: " . $footer['address'], 0, 1, 'C');
$pdf->Cell(0, 6, "Phone: " . $footer['phone'] . "   Email: " . $footer['email'], 0, 1, 'C');

// ---------------- OUTPUT ----------------
$pdf->Output("I", "demand_{$demand_id}.pdf");
exit;
?>
