<?php
ob_start();
require("../../classes/autoload.php");

$dn_res = $db->query("delivery_note", "SELECT * FROM delivery_note WHERE record_id = '{$_GET['record_id']}'");
$delivery_note = $dn_res->fetch_assoc();

$invoice_res = $db->query('invoices', "SELECT * FROM invoices WHERE record_id = '{$delivery_note['quote_id']}'");
$invoice = $invoice_res->fetch_assoc();

$company_res = $db->query("company_info", "SELECT * FROM company_info WHERE record_id = 1");
$company = $company_res->fetch_assoc();

$client_details_res = $db->query("clients", "SELECT * FROM clients WHERE record_id = '{$invoice['client_id']}'");
$client = $client_details_res->fetch_assoc();

$list_res = $db->query("delivery_note_list", "SELECT * FROM delivery_note_list WHERE delivery_note_id = '{$_GET['record_id']}'");

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);

// Logos
$pdf->Image('../../assets/logo.png', 10, 8, 30);
$pdf->Image('../../assets/logo_2.png', 170, 11, 25);

// Title
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 20, 'DELIVERY NOTE', 0, 1, 'C');
$pdf->Ln(10);

// Client Details
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(110, 8, $client['name'], 0, 0);
$pdf->Cell(80, 8, $company['name'], 0, 1);
$pdf->SetFont('Arial', '', 9);

$pdf->Cell(110, 8, $client['email'], 0, 0);
$pdf->Cell(80, 8, "TEL: " . $company['tel'], 0, 1);

$pdf->Cell(110, 8, $client['contact_person'], 0, 0);
$pdf->Cell(80, 8, "EMAIL: " . $company['email'], 0, 1);

$pdf->Cell(110, 8, $client['contact_number'], 0, 1);
$pdf->MultiCell(100, 8, $client['address']);
$pdf->Ln(6);
$pdf->SetXY(120, 64);
$pdf->MultiCell(70, 8, $company['address_line_1'] . "\n" . $company['address_line_2'] . "\n" . $company['address_line_3'] . "\n" . ($company['address_line_4'] ?? ''));

// Info
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(26, 6, "DATE: ", 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(50, 6, date("Y-m-d"), 0, 1);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(26, 6, "INVOICE: ", 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(50, 6, $invoice['invoice_number'], 0, 1);

if ($invoice['additional_delivery_details'] != "") {
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(50, 6, "DELIVERY DETAILS", "B", 1);
    $pdf->SetFont('Arial', '', 10);
    $pdf->MultiCell(185, 6, $invoice['additional_delivery_details'], 1, 1);
}

$pdf->Ln(2);

// Table Header
$pdf->SetFont('Arial', 'B', 8);
$pdf->SetFillColor(200, 200, 200);
$pdf->Cell(20, 7, 'Code', 1, 0, 'C', true);
$pdf->Cell(130, 7, 'Description', 1, 0, 'C', true);
$pdf->Cell(15, 7, 'Size', 1, 0, 'C', true);
$pdf->Cell(15, 7, 'Panels', 1, 1, 'C', true);

// Table Body from delivery_note_list
$pdf->SetFont('Arial', '', 7);
while ($row = $list_res->fetch_assoc()) {
    $pdf->Cell(20, 7, $row['stock_code'], 1);
    $pdf->Cell(130, 7, $row['description'], 1);
    $pdf->Cell(15, 7, $row['size_m'], 1, 0, 'C');
    $pdf->Cell(15, 7, $row['pannels'], 1, 1, 'C');
}

// Note
if (!empty($delivery_note['note'])) {
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(0, 6, "NOTE:", 0, 1);
    $pdf->SetFont('Arial', '', 9);
    $pdf->MultiCell(185, 5, $delivery_note['note'], 1);
}

// Signature section
$pdf->Ln(8);
$pdf->SetFont('Arial', 'B', 10);
$pdf->MultiCell(180, 5, "RECEIVED IN GOOD CONDITION");
$pdf->Ln(8);
$pdf->SetFont('Arial', '', 7);
$pdf->Cell(50, 7, '', "B", 0, 'L');
$pdf->Cell(50, 7, '', 0, 0, 'C');
$pdf->Cell(50, 7, '', "B", 1, 'L');
$pdf->Cell(50, 7, 'CLIENT NAME AND SIGNATURE', "", 0, 'L');
$pdf->Cell(50, 7, '', 0, 0, 'C');
$pdf->Cell(50, 7, 'DELIVERY/PICK-UP CHECKED BY', "", 1, 'L');

ob_end_clean();
$pdf->Output('I', 'delivery_note.pdf');