<?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);
// ---------------- STYLISH WHITE FOOTER ----------------
$footerY = $pdf->GetY();

// White background
$pdf->SetFillColor(255,255,255);
$pdf->Rect(10, $footerY, 190, 75, 'F');

// --------- LEFT: LOGO (Clean Spaced) ---------
$leftX = 15;
$leftY = $footerY + 12;

if (!empty($footer['agency_logo']) && file_exists("uploads/" . $footer['agency_logo'])) {
    $pdf->Image("uploads/" . $footer['agency_logo'], $leftX, $leftY, 28, 0);
}

// --------- CENTER BLOCK ---------
$centerX = 55;
$centerWidth = 135;

// Agency Name
$pdf->SetXY($centerX, $footerY + 10);
$pdf->SetFont('Arial','B',15);
$pdf->SetTextColor(30, 80, 200);   // Modern Blue
$pdf->Cell($centerWidth, 8, strtoupper($footer['agency_name']), 0, 1, 'C');

// ---- Thin Divider Line ----
$pdf->SetDrawColor(200,200,200);
$pdf->Line($centerX, $pdf->GetY() + 1, 200, $pdf->GetY() + 1);
$pdf->Ln(3);

// ------------ ADDRESS (Center Multiline) ------------
$pdf->SetFont('Arial','',11);
$pdf->SetTextColor(60,60,60);

$address = "Address: " . $footer['address'];
$words = explode(' ', $address);
$line = "";

foreach ($words as $w) {
    $test = $line . " " . $w;
    if ($pdf->GetStringWidth($test) < ($centerWidth - 10)) {
        $line = $test;
    } else {
        $pdf->SetX($centerX);
        $pdf->Cell($centerWidth, 6, trim($line), 0, 1, 'C');
        $line = $w;
    }
}
$pdf->SetX($centerX);
$pdf->Cell($centerWidth, 6, trim($line), 0, 1, 'C');

// ---- Thin Divider Line ----
$pdf->Ln(1);
$pdf->SetDrawColor(200,200,200);
$pdf->Line($centerX, $pdf->GetY(), 200, $pdf->GetY());
$pdf->Ln(3);

// ------------ CONTACT INFO (Modern, Center) ------------
$pdf->SetFont('Arial','',11);
$pdf->SetTextColor(0,0,0);

$pdf->SetX($centerX);
$pdf->Cell($centerWidth, 6, "Phone: " . $footer['phone'], 0, 1, 'C');

$pdf->SetX($centerX);
$pdf->Cell($centerWidth, 6, "Email: " . $footer['email'], 0, 1, 'C');

$pdf->SetX($centerX);
$pdf->Cell($centerWidth, 6, "Website: " . $footer['website'], 0, 1, 'C');




// ---------------- OUTPUT ----------------
$pdf->Output("I", "demand_{$demand_id}.pdf");
exit;
?>
