<?php
session_start();
require('classes/fpdf.php');
require('classes/db.class.php');



function get_asset_id($asset_id)
{
    if ($asset_id == "NONE") {
    } else {

        $db = new db();

        $res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = $asset_id");
        if ($res) {
            $values = $res->fetch_assoc();

            return $values['description'];
        } else {
            return "NONE";
        }
    }
}
$where_data = " 1 ";

if (strlen($_POST['date_from']) > 5) {
    $where_data = $where_data . " AND date_time BETWEEM '{$_POST['date_from']} 00:00' AND '{$_POST['date_to']} 23:59'";
}
if ($_POST['asset_id'] != 0) {
    $where_data = $where_data . " AND asset_id = {$_POST['asset_id']}";
}
if ($_POST['user_id'] != 0) {
    $where_data = $where_data . " AND user_id = {$_POST['user_ids']}";
}

$db = new db();

$log_res = $db->exec_query('log_book', ['*'], '', '', '', '', $where_data, "ORDER BY record_id DESC");


if($log_res->num_rows < 1){
    echo "<script>window.location.href='log_report_form.php?no_results'</script>";
}



$pdf = new FPDF();
$pdf->AliasNbPages();

$pdf->AddPage('L');
$pdf->Image('images/logo_1.png', 255, 2, 40);

// Arial bold 15
$pdf->SetFont('Arial', 'B', 8);
// Move to the right

$pdf->Cell(20);
$pdf->Cell(60, -4, '', 0, 1, 'C');

$header = [75, 160];
// Title
$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(1);
$pdf->Cell($header[0], 15, 'LOG REPORT', "BR", 0, 'L');
$pdf->SetFont('Arial', '', 20);

if (strlen($_POST['date_from']) > 2) {
    $pdf->Cell($header[1], 15,  " {$_POST['date_from']} 00:00   TO   {$_POST['date_to']} 23:59", "T", 1, 'L');
} else {
    $pdf->Cell($header[1], 15, 'ALL', "T", 1, 'C');
}
$pdf->SetFont('Arial', '', 9);

$pdf->Cell($header[0], 10, "", "", 1, 'L');

// $pdf->Line(11, 22, 290, 22);

$totals_table = [30];
$index = 0;
$numbers = 0;
$y = 28;

$table_top = [40, 28, 28, 70, 20, 20, 186, 20, 0];




$pdf->SetFont('Arial', '', 9);
$pdf->Cell(1);
$pdf->Cell($table_top[0], 5, "REASON", "LBR", 0, 'L');
$pdf->Cell($table_top[1], 5, "DATE", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "USER", "BR", 0, 'l');
$pdf->Cell($table_top[3], 5, "ASSET", "BR", 0, 'l');
$pdf->Cell($table_top[4], 5, "ODO", "BR", 1, 'l');
// $pdf->Cell($table_top[5], 5, "NOTES", "BR", 1, 'l');


while ($log = $log_res->fetch_assoc()) {
    $pdf->Cell(1);
    $reason_res = $db->exec_query('log_reasons', ['*'], '', '', '', '', "record_id = {$log['reason_id']}");
    $reason = $reason_res->fetch_assoc();
    $user_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$log['user_id']}");
    $user = $user_res->fetch_assoc();
    $asset_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = {$log['asset_id']}");
    $asset = $asset_res->fetch_assoc();


    $pdf->Cell($table_top[0], 8, $reason['name'], "B", 0, 'L');
    $pdf->Cell($table_top[1], 8, $log['date_time'], "B", 0, 'l');
    $pdf->Cell($table_top[2], 8, $user['username'], "B", 0, 'l');
    $pdf->Cell($table_top[3], 8, $asset['description'], "B", 0, 'l');
    $pdf->Cell($table_top[4], 8, $log['odo'], "B", 1, 'l');
    $pdf->Cell(1);
    $pdf->MultiCell($table_top[6], 10, $log['notes'], "B", 'l');

}




$pdf->Output("I");
