<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();

$asset_res = $db->exec_query('assets', ['*']);

$asset_total = $asset_res->num_rows;
$active = 0;
$suspended = 0;
$unassigned = 0;

while ($asset = $asset_res->fetch_assoc()) {
    if ($asset['status'] == "ACTIVE") {
        $active++;
    }
    if ($asset['status'] == "SUSPENDED") {
        $suspended++;
    }

    if ($asset['status'] == "") {
        $unassigned++;
    }
    $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, 35, 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[8], 7, 'ASSETS', "BR", 0, 'L');
$pdf->Cell($table_top[8], 7, 'ACTIVE', "BR", 0, 'L');
$pdf->Cell($table_top[8], 7, 'SUSPENDED', "BR", 0, 'L');
$pdf->Cell($table_top[8], 7, 'UNASSIGNED', "BR", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[8], 5, $asset_total, "BR", 0, 'L');
$pdf->Cell($table_top[8], 5, $active, "BR", 0, 'L');
$pdf->Cell($table_top[8], 5, $suspended, "BR", 0, 'L');
$pdf->Cell($table_top[8], 5, $unassigned, "BR", 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, 'ASSETS UNASSIGNED', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);


$pdf->Cell(-2);
$pdf->Cell($table_top[4], 5, "FLEET ID", "LBR", 0, 'L');
$pdf->Cell($table_top[3], 5, "DESCRIPTION", "LBR", 0, 'L');
$pdf->Cell($table_top[4], 5, "ASSET TYPE", "BR", 1, 'L');

$assets_unassigned = $db->exec_query('assets', ['*'], '', '', '', '', "status = ' '", 'ORDER BY asset_type_id DESC');

while ($asset_unassigned = $assets_unassigned->fetch_assoc()) {
    $asset_type_res = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$asset_unassigned['asset_type_id']}", 'ORDER BY asset_type_name ASC');
    $service_type = $asset_type_res->fetch_assoc();
    $pdf->Cell(-2);
    $pdf->Cell($table_top[4], 5, $asset_unassigned['fleet_no'], "LBR", 0, 'L');
    $pdf->Cell($table_top[3], 5, $asset_unassigned['description'], "LBR", 0, 'L');
    $pdf->Cell($table_top[4], 5, $service_type['asset_type_name'], "LBR", 1, 'L');
}

$pdf->Cell(0,16,'',0,1);
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'ASSETS SUSPENDED', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);
$pdf->Cell($table_top[4], 5, "FLEET ID", "LBR", 0, 'L');
$pdf->Cell($table_top[3], 5, "DESCRIPTION", "LBR", 0, 'L'); 

$pdf->Cell($table_top[4], 5, "ASSET TYPE", "BR", 1, 'L');

$assets_suspended = $db->exec_query('assets', ['*'], '', '', '', '', "status = 'SUSPENDED'", 'ORDER BY asset_type_id DESC');

while ($asset_suspended = $assets_suspended->fetch_assoc()) {
    $asset_type_res1 = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$asset_suspended['asset_type_id']}", 'ORDER BY asset_type_name ASC');
    $type1 = $asset_type_res1->fetch_assoc();
    $pdf->Cell(-2);
    $pdf->Cell($table_top[4], 5, $asset_suspended['fleet_no'], "LBR", 0, 'L');
    $pdf->Cell($table_top[3], 5, $asset_suspended['description'], "LBR", 0, 'L');
    $pdf->Cell($table_top[4], 5, $type1['asset_type_name'], "LBR", 1, 'L');
}

$pdf->Cell(0,16,'',0,1);
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'ASSETS ACTIVE', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);
$pdf->Cell($table_top[4], 5, "FLEET ID", "LBR", 0, 'L');
$pdf->Cell($table_top[3], 5, "DESCRIPTION", "LBR", 0, 'L');
$pdf->Cell($table_top[4], 5, "ASSET TYPE", "BR", 1, 'L');

$assets_res = $db->exec_query('assets', ['*'], '', '', '', '', "status = 'ACTIVE'", 'ORDER BY asset_type_id DESC');

while ($assets = $assets_res->fetch_assoc()) {
    $asset_type_res = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$assets['asset_type_id']}", 'ORDER BY asset_type_name ASC');
    $service_type = $asset_type_res->fetch_assoc();
    $pdf->Cell(-2);
    $pdf->Cell($table_top[4], 5, $assets['fleet_no'], "LBR", 0, 'L');
    $pdf->Cell($table_top[3], 5, $assets['description'], "LBR", 0, 'L');
    $pdf->Cell($table_top[4], 5, $service_type['asset_type_name'], "LBR", 1, 'L');
}

$pdf->Output();

