<?php
session_start();
// include "classes/db.class.php";
include "classes/html.class.php";

$db_2 = new db('fuel');

// echo $_SESSION['user_id'];

$res = $db_2->exec_query('fuel_movement', ['*'], '', '', '', '', "user_id = {$_SESSION['user_id']} AND status = 'AUTHORISED'");

if ($res->num_rows > 0) {
    $row = $res->fetch_assoc();
    echo "<script>window.location.href='fuel_step_b.php?tran_no={$row['record_id']}';</script>";
}

$db = new db();

$qr = new qrcode();


$asset_res = $db->exec_query('assets', ["*"], '', '', '', '', "1", "ORDER BY fleet_no ASC");

// $asset_res = $db->exec_query('assets', ["*"]);

if ($_SESSION['farm_id'] == 0) {
    $whereFarm = "1";
} else {

    $whereFarm = ($_SESSION['farm_id'] == 9) ? "farm_id = {$_SESSION['farm_id']} OR farm_id = 10" : "farm_id = {$_SESSION['farm_id']}";
}


$sqlQueryAssets = "SELECT A.*,B.asset_type_name,C.farm_name FROM assets A 
LEFT JOIN asset_types B ON A.asset_type_id = B.record_id 
LEFT JOIN farms C ON A.farm_id = C.record_id
WHERE $whereFarm ORDER BY farm_name ASC, fleet_no ASC, asset_type_name ASC";

// echo $sqlQueryAssets;

$asset_res = $db->query($sqlQueryAssets);

$asset_list = '<option></option>';

$assetsArr = $asset_res->fetch_all(MYSQLI_ASSOC);

foreach ($assetsArr as $index => $asset) {

    $currentFarm = $asset['farm_name'];
    $nextFarm = $assetsArr[$index + 1]['farm_name'];
    $prevFarm = $assetsArr[$index - 1]['farm_name'];

    $currentType = $asset['asset_type_name'];
    $nextType = $assetsArr[$index + 1]['asset_type_name'];
    $prevType = $assetsArr[$index - 1 ?? 0]['asset_type_name'];


    if ($currentFarm != $prevFarm) {
        $asset_list = $asset_list . "<optgroup label='$asset[farm_name]'>";
    }

    if ($currentType != $prevType) {
        $asset_list = $asset_list . "<optgroup label='&nbsp;&nbsp;&nbsp;&nbsp;$asset[asset_type_name]'>
        <option value='{$asset['record_id']}'>{$asset['fleet_no']} {$asset['description']}</option>";

        continue;
    }

    if ($currentType != $nextType) {
        $asset_list = $asset_list . "<option value='{$asset['record_id']}'>{$asset['fleet_no']} {$asset['description']}</option>
        </optgroup>";

        continue;
    }

    if ($currentFarm != $nextFarm) {
        $asset_list = $asset_list . "</optgroup>";
    }

    $asset_list = $asset_list . "<option value='{$asset['record_id']}'>{$asset['fleet_no']} {$asset['description']}</option>";
}

$html = new html("FUEL");

?>


<div class='form'>
    <br>
    <? $qr->scanner('test', 'call_qr_search'); ?>
    <br>

    <h2>OR</h2>

    <form action='fuel_step_a.php' method='POST' id='form_1'>
        <h2>SELECT ASSET:</h2>
        <br>
        <select name='asset_record_id' id='asset_record_id' onchanges='' class='form_input'>
            <?php echo $asset_list; ?>
        </select>
        <br>
        <br>
        <input type='text' id='test' hidden />
        <br>
        <input type='submit' value='CONTINUE' class='form_btn' />
    </form>
</div>
<script>
    function call_qr_search() {

        qr_code = document.getElementById('test').value;

        // Create an XMLHttpRequest object
        const xhttp = new XMLHttpRequest();
        // Define a callback function
        xhttp.onload = function () {
            // Here you can use the Data
            if (this.responseText != 0) {

                if (this.responseText == 0) {
                    alert('YOUR NOT AUTHORISED TO DO INSPECTION ON THIS ASSET');
                    console.log(this.responseText);
                    document.getElementById('qr_code').value = '';
                } else {
                    values = this.responseText.split(',');
                    document.getElementById('asset_record_id').value = values[9];
                    document.getElementById('form_1').submit();
                }



            } else if (this.responseText == 0) {
                alert("QR CODE NOT REGISTERED TO AN ASSET : " + qr_code);

            } else {
                // window.location.href = "users.php";
                alert("Something Went Wrong");

            }
        }

        xhttp.open("GET", "ajax/get_asset.ajax.php?qr_code=" + qr_code);
        xhttp.send();

    }
</script>



</body>

</html>