<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();
$db_2 = new db('fuel');

$where_data = str_replace('|', "'", $_POST['where_data']);
$petrol_res = $db_2->exec_query('petrol', ['*'], '', '', '', '', $where_data, "ORDER BY record_id DESC");

$total = 0;

while ($petrol = $petrol_res->fetch_assoc()) {

    $liters = $petrol['liters'] + $liters;

    $total++;
}

$pdf = new FPDF();
$pdf->AliasNbPages();


$pdf->AddPage('p');

$pdf->Image('images/logo_1.png', 170, 5, 40);
// Arial bold 15
$pdf->SetFont('Arial', 'B', 8);
// Move to the right
$pdf->Cell(20);
$pdf->Cell(60, -4, '', 0, 1, 'C');
$pdf->Cell(-3);

$table_top = [50, 28, 16, 65, 30, 55, 30, 20, 25, 20, 20, 10];
// Title
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 30, 'PETROL REPORT', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);

$pdf->Cell($table_top[2], 7, 'TOTAL', "LBR", 0, 'L');
$pdf->Cell($table_top[8], 7, 'TOTAL LITERS', "LBR", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[2], 5, $total, "LBR", 0, 'L');
$pdf->Cell($table_top[8], 5, $liters, "LBR", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[1], 15, "", 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'LITERS', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);
$pdf->Cell($table_top[11], 5, "NO", "LBR", 0, 'L');
$pdf->Cell($table_top[7], 5, "FARM", "LBR", 0, 'L');
$pdf->Cell($table_top[2], 5, "LITERS", "LBR", 0, 'l');
$pdf->Cell($table_top[4], 5, "DATE", "LBR", 0, 'l');
$pdf->Cell($table_top[4], 5, "NOTES", "LBR", 1, 'l');

$petrol_res = $db_2->exec_query('petrol', ['*'], '', '', '', '', $where_data, "ORDER BY record_id DESC");

while ($petrol = $petrol_res->fetch_assoc()) {

    $farm_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$petrol['farm_id']}", '');
    $farm = $farm_res->fetch_assoc();

    $pdf->Cell(-2);
    $pdf->Cell($table_top[11], 6, $petrol['record_id'], "LBR", 0, 'L');
    $pdf->Cell($table_top[7], 6, $farm['farm_name'], "LBR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $petrol['liters'], "LBR", 0, 'l');
    $pdf->Cell($table_top[4], 6, $petrol['date'], "LBR", 0, 'l');
    $pdf->Cell($table_top[4], 6, $petrol['notes'], "LBR", 1, 'l');

}


$pdf->Output("I");