<?php
session_start();

include "classes/html.class.php";

// var_dump($_POST);

$where_data = ' 1 ';

if (strlen($_POST['date_from']) > 5) {
    $where_data = $where_data . " AND date_time_opened BETWEEN '{$_POST['date_from']} 00:00' AND '{$_POST['date_to']} 23:59'";
}

if ($_POST['asset_id'] != 0) {
    $where_data = $where_data . " AND asset_id = '1_{$_POST['asset_id']}'";
}

if ($_POST['tank_id'] != 0) {
    $where_data = $where_data . " AND tank_id = '{$_POST['tank_id']}'";
}

$db = new db();
$db_2 = new db('fuel');

$table_data = '<tr></tr>';

$unq_data = $db_2->query('SELECT DISTINCT asset_id FROM fuel_movement WHERE ' . $where_data . ' ORDER BY asset_id DESC');

$where_data = str_replace("'", "|", $where_data);
$index = 0;
$total = 0;

while ($row = $unq_data->fetch_assoc()) {
    $asset_id = $row['asset_id'];
    $asset_id = str_replace("1_", "", $asset_id);
    $asset_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = $asset_id");
    $asset = $asset_res->fetch_assoc();

    if($asset['description'] == '') {
        $asset_desc = "MANUAL OVERRIDE";
    }else{
        $asset_desc = $asset['description'];
    }
    

    $amount_res = $db_2->query("SELECT SUM(amount) as total_amount FROM fuel_movement WHERE asset_id = '1_$asset_id' ");
    $amount = $amount_res->fetch_assoc();
    $liters_1 = $amount['total_amount'] / 1000;

    // LAST ODO
    $last_odo_res = $db_2->query("SELECT odo FROM fuel_movement WHERE asset_id = '1_$asset_id' ORDER BY record_id DESC LIMIT 1");
    $last_odo = $last_odo_res->fetch_assoc();
    $odo = $last_odo['odo'];

    // FIRST ODO 
    $first_odo_res = $db_2->query("SELECT odo FROM fuel_movement WHERE asset_id = '1_$asset_id' ORDER BY record_id ASC LIMIT 1");
    $first_odo = $first_odo_res->fetch_assoc();
    $first_odo = $first_odo['odo'];

    $odo = $odo - $first_odo;
    
    // NET ODO 
    if ($odo > 1 && $first_odo > 1) {
        
        $consumption = round($liters_1 / $odo,2);

    } else {
        $consumption = 0;
    }

    $table_data = $table_data . "
        <tr>
        <td><input type='text' id='asset_$index' class='table_input' value=' {$asset_desc}' readonly /></td>
        <td><input type='text' id='liters_$index' class='table_input' value='$liters_1" . " L' readonly /></td>
        <td><input type='text' id='odo_$index' class='table_input' value='{$odo}' readonly /></td>
        <td><input type='text' id='consumption_$index' class='table_input' value='$consumption' readonly /></td>
        </tr>
        ";

    $total++;
}

$html = new html("CONSUMPTION REPORTS");

?>

<style>
    body {
        background-image: url('');
        /* background-color: white; */
        background-repeat: repeat-y;

    }
</style>


<table style='width:100%; text-align:center; font-size:2vw;'>
    <tr>
        <td>
            TOTAL: <?php echo $total; ?>
        </td>
        <td>
            <form action='consumption_report.pdf.php' target='_blank' method='POST'>
                <input type='text' value="<? echo $where_data; ?>" name='where_data' hidden />
                <input type='submit' value='PDF REPORT' class='form_btn' id='report_btn' style="margin-top:2vw;" />
            </form>
        </td>

</table>

<hr><br>
<table style='width:100%; text-align:center;'>
    <tr>
        <td>
            ASSET
        </td>
        <td>
            AMOUNT
        </td>
        <td>
            ODO
        </td>
        <td>
            CONSUMPTION
        </td>
    </tr>


    <?php echo $table_data; ?>
</table>

<br>
<br>
<br>
<br>
<br>
<br>