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

// if (isset($_GET['structure_id'])) {


$db = new db();
$where_data = str_replace('|', "'", $_POST['where_data']);
$asset_res = $db->exec_query('assets', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC');
$total = 0;
$active = 0;
$suspended = 0;
$expired = 0;
$expired_in_30 = 0;
while ($asset = $asset_res->fetch_assoc()) {
    if ($asset['status'] == "ACTIVE") {
        $active++;
    }
    if ($asset['status'] == "SUSPENDED") {
        $suspended++;
    }

    if ($asset['license_exp_date'] <= date('Y-m-d')) {
        $expired++;
    }


    $total++;
}

$total_30_res = $db->exec_query('assets', ['*'], '', '', '', '', "license_exp_date BETWEEN '" . date('Y-m-d') . "' AND '" . date('Y-m-d', strtotime('+31 Days'))  . "' AND " . $where_data, 'ORDER BY record_id DESC');
$expired_in_30 = $total_30_res->num_rows;

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



$pdf->AddPage('L');

$pdf->Image('images/logo_1.png', 250, 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, 20];
// Title
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 30, 'ASSET REPORT', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);

$pdf->Cell($table_top[0], 7, 'TOTAL', "BR", 0, 'L');
$pdf->Cell($table_top[0], 7, 'ACTIVE', "BR", 0, 'L');
$pdf->Cell($table_top[0], 7, 'SUSPENDED', "BR", 0, 'L');
$pdf->Cell($table_top[0], 7, 'EXPIRED LICENSES', "BR", 0, 'L');
$pdf->Cell($table_top[0], 7, 'EXPIERS IN 30 DAYS', "BR", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[0], 5, $total, "BR", 0, 'L');
$pdf->Cell($table_top[0], 5, $active, "BR", 0, 'L');
$pdf->Cell($table_top[0], 5, $suspended, "BR", 0, 'L');
$pdf->Cell($table_top[0], 5, $expired, "BR", 0, 'L');
$pdf->Cell($table_top[0], 5, $expired_in_30, "BR", 1, 'L');

$pdf->Cell(-5);
$pdf->Cell($table_top[1], 15, "", 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'ASSETS', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);


$pdf->Cell(-6);
$pdf->Cell($table_top[2], 5, "ASSET ID", "LBR", 0, 'L');
$pdf->Cell($table_top[2], 5, "FLEET NO", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "STATUS", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "FARM", "BR", 0, 'l');
$pdf->Cell($table_top[1], 5, "LICENSE EXP", "BR", 0, 'l');
$pdf->Cell($table_top[1], 5, "CURRENT ODO", "BR", 0, 'l');
$pdf->Cell($table_top[3], 5, "DESCRIPTION", "BR", 0, 'l');
$pdf->Cell($table_top[5], 5, "VIN", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "FUEL SIZE", "BR", 0, 'l');
$pdf->Cell($table_top[6], 5, "GROUP", "B", 1, 'l');

// $pdf->Cell($table_top[1], 5, "ASSET ID", "BR", 1, 'l');

$asset_res = $db->exec_query('assets', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC');

while ($asset = $asset_res->fetch_assoc()) {
    $farm_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$asset['farm_id']}");
    $farm = $farm_res->fetch_assoc();
    $odo_res = $db->exec_query('asset_odo', ['*'], '', '', '', '', "asset_record_id = {$asset['record_id']}", "ORDER BY record_id DESC");
    $odo = $odo_res->fetch_assoc();
    $asset_type_res = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$asset['asset_type_id']}");
    $service_type = $asset_type_res->fetch_assoc();
    $pdf->Cell(-6);
    $pdf->Cell($table_top[2], 6, $asset['asset_id'], "B", 0, 'L');
    $pdf->Cell($table_top[2], 6, $asset['fleet_no'], "B", 0, 'l');
    $pdf->Cell($table_top[2], 6, $asset['status'], "B", 0, 'l');
    $pdf->Cell($table_top[2], 6, $farm['farm_name'], "B", 0, 'l');
    $pdf->Cell($table_top[1], 6, $asset['license_exp_date'], "B", 0, 'l');
    $pdf->Cell($table_top[1], 6, $odo['odo'], "B", 0, 'l');
    $pdf->Cell($table_top[3], 6, $asset['description'], "B", 0, 'l');
    $pdf->Cell($table_top[5], 6, $asset['vin'], "B", 0, 'l');
    $pdf->Cell($table_top[2], 6, $asset['fuel_size'], "B", 0, 'l');
    $pdf->Cell($table_top[6], 6, $service_type['asset_type_name'], "B", 1, 'l');
}

$pdf->AddPage("l");

$pdf->Cell(-3);
$pdf->Cell($table_top[1], 15, "", 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'EXPIRED LICENSES', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);


$pdf->Cell(-6);
$pdf->Cell($table_top[2], 5, "ASSET ID", "LBR", 0, 'L');
$pdf->Cell($table_top[2], 5, "FLEET NO", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "STATUS", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "FARM", "BR", 0, 'l');
$pdf->Cell($table_top[1], 5, "LICENSE EXP", "BR", 0, 'l');
$pdf->Cell($table_top[1], 5, "CURRENT ODO", "BR", 0, 'l');
$pdf->Cell($table_top[3], 5, "DESCRIPTION", "BR", 0, 'l');
$pdf->Cell($table_top[5], 5, "VIN", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "FUEL SIZE", "BR", 0, 'l');
$pdf->Cell($table_top[6], 5, "GROUP", "B", 1, 'l');

// $pdf->Cell($table_top[1], 5, "ASSET ID", "BR", 1, 'l');

$asset_res = $db->exec_query('assets', ['*'], '', '', '', '', "license_exp_date <= '" . date('Y-m-d') . "' AND " . $where_data, 'ORDER BY record_id DESC');

while ($asset = $asset_res->fetch_assoc()) {
    $farm_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$asset['farm_id']}");
    $farm = $farm_res->fetch_assoc();
    $odo_res = $db->exec_query('asset_odo', ['*'], '', '', '', '', "asset_record_id = {$asset['record_id']}", "ORDER BY record_id DESC");
    $odo = $odo_res->fetch_assoc();
    $asset_type_res = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$asset['asset_type_id']}");
    $service_type = $asset_type_res->fetch_assoc();
    $pdf->Cell(-6);
    $pdf->Cell($table_top[2], 6, $asset['asset_id'], "B", 0, 'L');
    $pdf->Cell($table_top[2], 6, $asset['fleet_no'], "B", 0, 'l');
    $pdf->Cell($table_top[2], 6, $asset['status'], "B", 0, 'l');
    $pdf->Cell($table_top[2], 6, $farm['farm_name'], "B", 0, 'l');
    $pdf->Cell($table_top[1], 6, $asset['license_exp_date'], "B", 0, 'l');
    $pdf->Cell($table_top[1], 6, $odo['odo'], "B", 0, 'l');
    $pdf->Cell($table_top[3], 6, $asset['description'], "B", 0, 'l');
    $pdf->Cell($table_top[5], 6, $asset['vin'], "B", 0, 'l');
    $pdf->Cell($table_top[2], 6, $asset['fuel_size'], "B", 0, 'l');
    $pdf->Cell($table_top[6], 6, $service_type['asset_type_name'], "B", 1, 'l');
}



$pdf->Output("I");

// echo "Structure?";
