<?php
require($_SERVER['DOCUMENT_ROOT'] . "/fpdf.php");
include "../../root.class.php";

$db = new db_safeguard();

/* =========================
   PDF CLASS
========================= */
class PDF extends FPDF
{
    function Header()
    {
        // Title
        $this->SetFont('Arial', 'B', 20);
        $this->SetTextColor(30, 58, 138); // Blue
        $this->Cell(0, 10, 'Booked Stock Report', 0, 1, 'C');

        // Orange line
        $this->SetDrawColor(249, 115, 22);
        $this->Line(10, 22, 285, 22);

        $this->Ln(6);
    }

    function Footer()
    {
        $this->SetY(-12);
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(120);
        $this->Cell(0, 10, 'Generated: ' . date('Y-m-d H:i'), 0, 0, 'L');
        $this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'R');
    }
}

/* =========================
   PDF INIT
========================= */
$pdf = new PDF(); // Landscape
$pdf->AddPage('L');
$pdf->SetFont('Arial', 'B', 9);


$logo = $_SERVER['DOCUMENT_ROOT'] . '/icons/Savuki_Logo.png';

if (file_exists($logo)) {
    $pdf->Image($logo, 235, 12, 50);
}

$pdf->Ln(10);

/* =========================
   TABLE HEADER
========================= */
$pdf->SetFillColor(30, 58, 138);
$pdf->SetTextColor(255);
// $pdf->Cell(25, 8, 'Date Time', 1, 0, 'C', true);
$pdf->Cell(35, 8, 'Booking Date', 1, 0, 'C', true);
$pdf->Cell(18, 8, 'Pump', 1, 0, 'C', true);
$pdf->Cell(20, 8, 'JC No', 1, 0, 'C', true);
$pdf->Cell(60, 8, 'Stock Type', 1, 0, 'C', true);
$pdf->Cell(18, 8, 'Stock No', 1, 0, 'C', true);
$pdf->Cell(40, 8, 'Item Name', 1, 0, 'C', true);
$pdf->Cell(18, 8, 'Qty', 1, 0, 'C', true);
$pdf->Cell(35, 8, 'Team Assigned', 1, 0, 'C', true);
$pdf->Cell(20, 8, 'Open Bal', 1, 0, 'C', true);
$pdf->Cell(20, 8, 'Close Bal', 1, 1, 'C', true);

/* =========================
   DATA ROWS
========================= */
$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0);

$stock_res = $db->query("book_stock", "SELECT * FROM book_stock");

$fill = false;

while ($stock = $stock_res->fetch_assoc()) {

    $stock_type_res = $db->query(
        "stock_types",
        "SELECT * FROM stock_types WHERE record_id = '{$stock['stock_type_id']}'"
    );
    $stock_type = $stock_type_res->fetch_assoc();

    $team_res = $db->query(
        "teams",
        "SELECT * FROM teams WHERE record_id = {$stock['team_assigned_id']}"
    );

    $team = $team_res->fetch_assoc();

    $pdf->SetFillColor($fill ? 234 : 255, $fill ? 240 : 255, 255);

    // $pdf->Cell(25, 8, $stock['date_time'], 1, 0, 'L', $fill);
    $pdf->Cell(35, 8, $stock['booking_date'], 1, 0, 'L', $fill);
    $pdf->Cell(18, 8, $stock['pump_code'], 1, 0, 'L', $fill);
    $pdf->Cell(20, 8, $stock['jc_no'], 1, 0, 'L', $fill);
    $pdf->Cell(60, 8, $stock_type['name'], 1, 0, 'L', $fill);
    $pdf->Cell(18, 8, $stock['stock_no'], 1, 0, 'L', $fill);
    $pdf->Cell(40, 8, $stock['item_name'], 1, 0, 'L', $fill);
    $pdf->Cell(18, 8, $stock['quantity'], 1, 0, 'C', $fill);
    $pdf->Cell(35, 8, $team['name'], 1, 0, 'L', $fill);
    $pdf->Cell(20, 8, $stock['open_balance'], 1, 0, 'C', $fill);
    $pdf->Cell(20, 8, $stock['close_balance'], 1, 1, 'C', $fill);

    $fill = !$fill;
}

/* =========================
   OUTPUT
========================= */
$pdf->Output('I', 'Booked_Stock_Report_' . date('Ymd_His') . '.pdf');
