<?php
session_start();

include "classes/html.class.php";

$db = new db();
$db_2 = new db('fuel');
$html = new html("DIESEL REPORTS");
$where_data = ' 1 ';
// echo $_POST['date_from'];
// echo $_POST['date_to'];
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['site_id'] != 0) {

    $tanks = $db_2->exec_query('tanks', ['*'], '', '', '', '', "site_id = {$_POST['site_id']}");
    while ($tank = $tanks->fetch_assoc()) {
        $where_arr[] = " tank_id = {$tank['record_id']} ";
    }
} else {
    $company_res = $db_2->exec_query('companies', ['*'], '', '', '', '', "name = 'MUELL ESTATES'");
    $company = $company_res->fetch_assoc();
    $sites = $db_2->exec_query('sites', ['*'], '', '', '', '', "company_id = {$company['record_id']}");
    while ($site = $sites->fetch_assoc()) {

        $tanks = $db_2->exec_query('tanks', ['*'], '', '', '', '', "site_id = {$site['record_id']}");
        while ($tank = $tanks->fetch_assoc()) {
            $where_arr[] = "tank_id = {$tank['record_id']}";
        }
    }
}
$data = implode(' OR ', $where_arr);

$where_data = $where_data . " AND (" . $data . ")";

$results = $db_2->exec_query('fuel_movement', ['*'], '', '', '', '', $where_data, "ORDER BY record_id DESC");


// foreach ($data_1 as $row) {
while ($data_row = $results->fetch_assoc()) {
    // var_dump($data_row);
    // echo $data_row['record_id'];

    $asset_info_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = " . str_replace("1_", "", $data_row['asset_id']));
    $asset_info = $asset_info_res->fetch_assoc();

    $tank_name_res = $db_2->exec_query('tanks', ['*'], '', '', '', '', "record_id = {$data_row['tank_id']}");
    $tank_info = $tank_name_res->fetch_assoc();


    $table_data = $table_data . "
        <tr>
        <td><input type='text' class='table_input' style='font-size:0.75vw;' value='{$asset_info['description']}' readonly /></td>
        <td><input type='text' class='table_input' id='$index' value='" . round(($data_row['amount'] / 1000), 2) . " L' readonly /></td>
        <td><input type='text' class='table_input' value='{$tank_info['name']}' readonly /></td>
        <td><input type='text' class='table_input' value='{$data_row['date_time_opened']}' readonly /></td>
        <td><input type='text' class='table_input' value='{$data_row['odo']}' readonly /></td>
        </tr>
        ";
    $index++;
    $total++;
}

// echo $table_data;
$index = 0;
$total = 0;
$total_open = 0;
$total_closed = 0;
$total_waiting = 0;

?>
<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>
            <form action='fuel_report.pdf.php' target='_blank' method='POST'>
                <input type='text' value="<? echo $where_data; ?>" name='where_data' hidden />
                <input type='text' value="<? echo $_POST['date_from']; ?>" name='date_from' hidden />
                <input type='text' value="<? echo $_POST['date_to']; ?>" name='date_to' hidden />
                <input type='submit' value='PDF REPORT' class='form_btn' id='report_btn' style="margin-top:2vw;" />
            </form>
            <form action='fuel_consumption.php' target='_blank' method='POST'>
                <input type='text' value="<? echo $where_data; ?>" name='where_data' hidden />
                <input type='text' value="<? echo $_POST['date_from']; ?>" name='date_from' hidden />
                <input type='text' value="<? echo $_POST['date_to']; ?>" name='date_to' hidden />
                <input type='submit' value='V. CONSUMPTION REPORT' class='form_btn' id='report_btn' style="margin-top:2vw;" />
            </form>
        </td>
        <!-- <td>
            <form action='api/excel_esport_fuel.php' target='_blank' method='POST'>
                <input type='submit' value='CSV 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>
            LITERS
        </td>
        <td>
            TANK
        </td>
        <td>
            DATE
        </td>
        <td>
            ODO
        </td>
    </tr>


    <?php echo $table_data; ?>
</table>

<br>
<br>
<br>
<br>
<br>
<br>

</body>

</html>