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

function http_get_curl($url, $get_variables_array, $get_values_array)
{
    $get_variables_array[] = 'username';
    $get_variables_array[] = 'password';
    $get_values_array[] = 'MUX01';
    $get_values_array[] = 'MUX01!@';
    $index = 0;
    $get_vars = "?";
    foreach ($get_variables_array as $variables) {
        $get_vars = $get_vars . $variables . "=" . $get_values_array[$index] . "&";
        $index++;
    }

    $url = $url . $get_vars;
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $resp = curl_exec($curl);
    curl_close($curl);

    return $resp;
}

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

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['fleet_no'];
        } else {
            return "NONE";
        }
    }
}

$where_data = $_POST['where_data'];


$db = new db();
$db_2 = new db('fuel');

$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, 'DIESEL 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, 40, 70, 20, 15, 20, 269];



$comp = $db_2->exec_query('companies', ['*'], '', '', '', '', "name = 'MUELL ESTATES'");

while ($comp_info = $comp->fetch_assoc()) {
    $company_id = $comp_info['record_id'];



    $sites = $db_2->exec_query('sites', ['*'], '', '', '', '', "company_id = $company_id");

    while ($site = $sites->fetch_assoc()) {
        $site_name = $site['name'];
        $site_id = $site['record_id'];

        $tanks = $db_2->exec_query('tanks', ['*'], '', '', '', '', "site_id = $site_id");

        while ($tank = $tanks->fetch_assoc()) {

            $tank_id = $tank['record_id'];
            $tank_name = $tank['name'];
            $pdf->Cell(1);
            $pdf->SetFont('Arial', 'B', 9);
            $pdf->Cell($table_top[8], 9, $tank_name, "B", 1, 'L');
            $pdf->SetFont('Arial', '', 9);
            $pdf->Cell(1);
            $pdf->Cell($table_top[0], 5, "STATUS", "LBR", 0, 'L');
            $pdf->Cell($table_top[1], 5, "OPENED", "BR", 0, 'l');
            $pdf->Cell($table_top[2], 5, "CLOSED", "BR", 0, 'l');
            $pdf->Cell($table_top[3], 5, "USER", "BR", 0, 'l');
            $pdf->Cell($table_top[4], 5, "ASSET", "BR", 0, 'l');
            $pdf->Cell($table_top[5], 5, "ODO", "BR", 0, 'l');
            $pdf->Cell($table_top[6], 5, "FLOW", "BR", 0, 'l');
            $pdf->Cell($table_top[7], 5, "AMOUNT", "BR", 0, 'l');
            $pdf->Cell($table_top[7], 5, "KM/L", "BR", 1, 'l');
            $total = 0;
            $index = 0;
            $transactions = $db_2->exec_query('fuel_movement', ['*'], '', '', '', '', "tank_id = $tank_id AND " . $where_data, "ORDER BY date_time_opened DESC, record_id DESC ");
            while ($tran = $transactions->fetch_assoc()) {
                $users = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$tran['user_id']}");
                $user = $users->fetch_assoc();
                $asset_id = str_replace("1_", "", $tran['asset_id']);
                $assets = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = $asset_id");
                $asset = $assets->fetch_assoc();


                $pdf->Cell(1);
                $pdf->Cell($table_top[0], 6.5, $tran['status'], "B", 0, 'L');
                $pdf->Cell($table_top[1], 6.5, $tran['date_time_opened'], "B", 0, 'l');
                $pdf->Cell($table_top[2], 6.5, $tran['date_time_closed'], "B", 0, 'l');
                $pdf->Cell($table_top[3], 6.5, $user['username'], "B", 0, 'l');
                $pdf->Cell($table_top[4], 6.5, $asset['description'], "B", 0, 'l');
                $pdf->Cell($table_top[5], 6.5, $tran['odo'], "B", 0, 'l');
                $pdf->Cell($table_top[6], 6.5, $tran['flow_rate'], "B", 0, 'l');
                $pdf->SetFont('Arial', 'B', 9);
                $pdf->Cell($table_top[7], 6.5, ($tran['amount'] / 1000) . ' L', "LB", 0, 'l');

                $get_last_tran = $db_2->exec_query('fuel_movement', ['*'], '', '', '', '', "asset_id = '1_$asset_id' AND record_id < {$tran['record_id']}", "ORDER BY record_id DESC");
                if ($get_last_tran->num_rows < 1) {
                    $pdf->Cell($table_top[7], 6.5, "N/A", "LB", 1, 'l');
                } else {
                    $last_tran = $get_last_tran->fetch_assoc();
                    $tran['odo'];
                    $last_tran['odo'];
                    $net_odo = $tran['odo'] - $last_tran['odo'];
                    if ($tran['odo'] ==  $last_tran['odo']) {
                        $pdf->Cell($table_top[7], 6.5, "0", "LB", 1, 'l');
                    } else {
                        if ($net_odo < 0) {
                            $pdf->Cell($table_top[7], 6.5, "0", "LB", 1, 'l');
                        } else {
                            $net = ($tran['amount'] / 1000) / $net_odo;
                            $pdf->Cell($table_top[7], 6.5, round($net, 2), "LB", 1, 'l');
                        }
                    }
                }


                $pdf->SetFont('Arial', '', 9);
                $total = $total + $tran['amount'];
                $index++;
            }

            $pdf->Cell(1);
            $pdf->SetFont('Arial', 'B', 9);
            $pdf->Cell($table_top[0], 8, 'TOTAL TRANSACTIONS', "", 0, 'L');
            $pdf->Cell($table_top[1], 8, $index, "", 0, 'l');
            $pdf->Cell($table_top[2], 8, '', "", 0, 'l');
            $pdf->Cell($table_top[3], 8, '', "", 0, 'l');
            $pdf->Cell($table_top[4], 8, '', "", 0, 'l');
            $pdf->Cell($table_top[5], 8, 'TOTAL FUEL USED', "", 0, 'l');
            $pdf->Cell($table_top[6], 8, '', "", 0, 'l');
            $pdf->Cell($table_top[7], 8, ($total / 1000) . " L", "", 1, 'l');
            $pdf->SetFont('Arial', '', 9);
            $pdf->AddPage('L');
            $pdf->Image('images/logo_1.png', 255, 2, 40);
            $header = [75, 160];
            // Title
            $pdf->SetFont('Arial', 'B', 20);
            $pdf->Cell(1);
            $pdf->Cell($header[0], 15, 'FUEL 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->AddPage('L');
// $year = date('Y');
// $db = new db();

// for ($m = 1; $m < 13; $m++) {
//     for ($d = 1; $d < 32; $d++) {
//         $day = date('D', $time = strtotime($year . "-" . $m . "-" . $d));
//         if (strlen($m) < 2) {
//             $m = '0' . $m;
//         }
//         if (strlen($d) < 2) {
//             $d = '0' . $d;
//         }
//         $date = $m . "-" . $d;
//         // echo $day;
//         if ($day == "Sun") {
//             $Week_dates[] = $date;
//         }
//     }
// }


// $pdf->SetFont('Arial', 'B', 15);
// $pdf->SetXY(10, 20);
// $pdf->Cell(10, 10, "JOB CARDS & INSPECTIONS", 0, 1, 'L');

// $data_hc_att = array(
//     'SERVICE' => $job_cards_service,
//     'BREAKDOWNS' => $job_cards_breakdown,
//     'OTHER J.C.' => $job_cards_other,
//     'INSPECTIONS' => $inspections
// );
// $colors_hc_att = array(
//     'SERVICE' => array(0, 0, 255),
//     'BREAKDOWNS' => array(255, 0, 0),
//     'OTHER J.C.' => array(180, 180, 180),
//     'INSPECTIONS' => array(0, 0, 0)
// );

// $pdf->SetXY(0, 30);

// $pdf->LineGraph(342, 70, $data_hc_att, 'V', $colors_hc_att, $top_val_wens + 2, 2);

$pdf->Output("I");
