<?php

include "../../html.class.php";
include "../../classes/db.class.php";
include "../../functions.class.php";
$html = new html();
$functions = new functions();
$where_data[] = "fm.company_id = {$_SESSION['company_id']}";


if (strlen($_POST['date_from']) > 5 && strlen($_POST['date_to']) > 5) {
    $dates = " fm.date_time_opened BETWEEN '{$_POST['date_from']} 00:00' AND '{$_POST['date_to']} 23:59'";
}

if (isset($_POST['client_id']) && $_POST['client_id'] != "") {
    $where_data[] = " fm.client_id = {$_POST['client_id']}";
    $other_side[] = " fm.client_id = {$_POST['client_id']}";
}   

if (isset($_POST['user_id']) && $_POST['user_id'] != "") {
    $where_data[] = " fm.user_id = {$_POST['user_id']}";
    $other_side[] = " fm.user_id = {$_POST['user_id']}";
}

if (isset($_POST['tank_id']) && $_POST['tank_id'] != "") {
    $where_data[] = " fm.tank_id = {$_POST['tank_id']}";
    $other_side[] = " fm.tank_id = {$_POST['tank_id']}";
}

if (isset($_POST['cash_type']) && $_POST['cash_type'] != "") {
    $where_data[] = " fm.cash_type = {$_POST['cash_type']}";
}
if (isset($_POST['exclude_from_reports']) && $_POST['exclude_from_reports'] != "") {
    $where_data[] = " (c.company_id = {$_SESSION['company_id']} AND (c.exclude_from_reports = {$_POST['exclude_from_reports']}))";
} else {
    $where_data[] = " (c.company_id = {$_SESSION['company_id']} AND (c.exclude_from_reports = 0))";

}

if (isset($_POST['site_id']) && $_POST['site_id'] != "") {
    $where_data[] = " fm.tank_id in (SELECT record_id FROM tanks WHERE company_id = {$_SESSION['company_id']} AND site_id = {$_POST['site_id']})";
    $other_side[] = " fm.tank_id in (SELECT record_id FROM tanks WHERE company_id = {$_SESSION['company_id']} AND site_id = {$_POST['site_id']})";
}

$data = implode(" AND ", $where_data);
$other_data = implode(" AND ", $other_side);

// echo $data;
$latest_orders_res = $db->query("fuel_movement", "SELECT * FROM fuel_movement as fm LEFT JOIN clients as c ON c.record_id = fm.client_id WHERE ($data AND $dates ) OR (fm.client_id = 0 AND fm.status = 'MANUAL OVERRIDE' AND $dates AND $other_data) ORDER BY date_time_closed DESC");
$sql = str_replace("'", "\'", "SELECT * FROM fuel_movement as fm LEFT JOIN clients as c ON c.record_id = fm.client_id WHERE ($data AND $dates ) OR (fm.client_id = 0 AND fm.status = 'MANUAL OVERRIDE' AND $dates AND $other_data) ORDER BY date_time_closed DESC");
?>
<style>
    body {
        display: flex;
        flex-direction: column;
        flex-wrap: nowrap;
        align-items: center;

    }
</style>
<div class="tank_liters_container">
    <?php $data = str_replace("'", "\'", $data); ?>
    <input type="submit" class="app_buttons_small" value="PDF"
        onclick="window.open('diesel_orders.pdf.php?sql=<?php echo $sql; ?>','_blank')" />
    <br>
    <input type="submit" class="app_buttons_small" value="EXCEL/CSV"
        onclick="window.open('vsv.report.php?sql=<?php echo $sql; ?>','_blank')" />
</div>
<div class="table-responsive">
    <table>
        <tr>
            <th>
                TANK
            </th>
            <th>
                DATE
            </th>
            <th>
                CLIENT
            </th>
            <th>
                AMOUNT (mL)
            </th>
            <th>
                VALUE (R)
            </th>
            <th>
                R/L
            </th>
            <th>
                STATUS
            </th>
            <th>
                TYPE
            </th>


        </tr>

        <?php

        while ($latest_orders = $latest_orders_res->fetch_assoc()) {
            ?>

            <tr>
                <td>
                    <input class="table_input" type='text'
                        value='<?php echo $functions->get_tank_name($latest_orders['tank_id']); ?>'>
                </td>
                <td>
                    <input class="table_input" type='text' value='<?php echo $latest_orders['date_time_closed']; ?>'
                        onchange="change(this,'date_time_closed','<?php echo $latest_orders['record_id']; ?>')">
                </td>
                <td>
                    <select class="table_input"
                        onchange="change(this,'client_id','<?php echo $latest_orders['record_id']; ?>')">
                        <?php $clients_res = $db->query("clients", "SELECT * FROM clients WHERE company_id = {$_SESSION['company_id']}"); ?>
                        <option value="<?php echo $latest_orders['client_id']; ?>">
                            <?php echo $functions->get_client_name($latest_orders['client_id']); ?>
                        </option>
                        <?php while ($client = $clients_res->fetch_assoc()) { ?>
                            <option value="<?php echo $client['record_id']; ?>"><?php echo $client['client_name']; ?></option>
                        <?php } ?>
                    </select>
                </td>
                <td>
                    <input class="table_input" type='text'
                        value=' <?php echo number_format($latest_orders['amount'], 2); ?>'
                        onchange="change(this,'amount','<?php echo $latest_orders['record_id']; ?>')">
                </td>
                <td>
                    <input class="table_input" type='text'
                        value=' <?php echo number_format(($latest_orders['amount'] / 1000) * $latest_orders['fuel_price'], 2); ?>'>
                </td>
                <td>
                    <input class="table_input" type='text'
                        value=' <?php echo number_format($latest_orders['fuel_price'], 2); ?>'
                        onchange="change(this,'fuel_price','<?php echo $latest_orders['record_id']; ?>')">
                </td>
                <td>
                    <input class="table_input" type='text' value='<?php echo $latest_orders['status']; ?>'
                        onchange="change(this,'status','<?php echo $latest_orders['record_id']; ?>')">
                </td>
                <td>
                    <select class="table_input"
                        onchange="change(this,'cash_type','<?php echo $latest_orders['record_id']; ?>')">
                        <?php if ($latest_orders['cash_type'] == 0) {
                            $type = "N/A";
                            ?>
                            <option value="<?php echo $latest_orders['type']; ?>"><?php echo $type; ?></option>
                            <option value="1">CASH</option>
                            <option value="3">EFT</option>
                            <option value="2">CARD</option>

                            <?php

                        } else if ($latest_orders['cash_type'] == 1) {
                            $type = "CASH";
                            ?>
                                <option value="<?php echo $latest_orders['type']; ?>"><?php echo $type; ?></option>
                                <option value="3">EFT</option>
                                <option value="2">CARD</option>
                                <option value="0">N/A</option>

                            <?php
                        } else if ($latest_orders['cash_type'] == 2) {
                            $type = "CARD";
                            ?>
                                    <option value="<?php echo $latest_orders['type']; ?>"><?php echo $type; ?></option>
                                    <option value="3">EFT</option>
                                    <option value="1">CASH</option>
                                    <option value="0">N/A</option>

                            <?php
                        } else {
                            $type = "EFT";
                            ?>
                                    <option value="<?php echo $latest_orders['type']; ?>"><?php echo $type; ?></option>
                                    <option value="2">CARD</option>
                                    <option value="1">CASH</option>
                                    <option value="0">N/A</option>

                            <?php
                        } ?>
                    </select>
                </td>

            </tr>
            <?php
        }
        ?>

    </table>
</div>

<script>
    function change(input, column_name, record_id) {
        if (confirm("Are you sure you want to change this detail?")) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    var orig_color = input.style.backgroundColor;
                    if (this.responseText == "1") {
                        input.style.transition = "background-color 1.5s ease";
                        input.style.backgroundColor = "green";
                        setTimeout(function () {
                            input.style.backgroundColor = orig_color;
                        }, 1500);

                    } else {
                        input.style.transition = "background-color 1.5s ease";
                        input.style.backgroundColor = "red";
                        setTimeout(function () {
                            input.style.backgroundColor = orig_color;
                        }, 1500);
                        alert(this.responseText);
                    }
                }
            };
            xmlhttp.open("POST", "change_details.ajax.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send("value=" + input.value + "&column_name=" + column_name + "&record_id=" + record_id);
        }
    }
</script>

<div class="spacer"></div>