<?php

include "classes/html.class.php";

$html = new html("EVENTS");


$db = new db();

$res = $db->exec_query('log_reasons', ['*']);

$whereFarm = ($_SESSION['farm_id'] == 0) ? "1" : "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";

$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>";
}
?>



<div class='form'>
    <h2 style="font-size:2vw">SELECT A LOG REASON:</h2>
    <select id='cur_name' class='form_input'>
        <option></option>
        <?
        while ($row = $res->fetch_assoc()) {
            echo "<option value='{$row['record_id']}'>{$row['name']}</option>";
        }
        ?>
    </select>
    <hr><br>
    <div class="test6">
        <label>ASSET</label><br>
        <select name='asset_id' id='asset_id' onchange="get_odo()" required class='form_input'>
            <?php echo $asset_list; ?>
        </select><br><br>
    </div>
    <label style="font-size:1.5vw">ODO:</label><br>
    <input type='text' id='odo' class='form_input' /><br><br>
    <label style="font-size:1.5vw">NOTES:</label><br>
    <textarea type='text' id='notes' class='form_input'></textarea><br><br>
    <input type='submit' value='ADD' class='form_btn' onclick='ajax_func()' />

</div>

<script>
    function ajax_func() {
        // alert("Test");
        notes = document.getElementById('notes').value;
        if (notes.length < 1) {
            alert("PLEASE ENTER A NOTE");
        } else {
            reason_id = document.getElementById('cur_name').value;
            if (reason_id.length < 1) {
                alert("PLEASE SELECT A REASON");
            } else {
                asset_id = document.getElementById('asset_id').value;

                if (asset_id.length < 1) {
                    alert("PLEASE SELECT ASSET");
                } else {
                    odo = document.getElementById('odo').value;
                    if (odo.length < 1) {
                        alert("PLEASE ENTER A ODO");
                    } else {
                        // Create an XMLHttpRequest object
                        const xhttp = new XMLHttpRequest();
                        // Define a callback function
                        xhttp.onload = function() {
                            // alert(this.responseText);

                            // Here you can use the Data
                            if (this.responseText != 1) {

                                alert(this.responseText);

                            } else {
                                alert('LOGGED');
                                window.location.href = "home.php";
                            }
                        }

                        xhttp.open("GET", "ajax/new_log.ajax.php?&reason_id=" + reason_id + "&notes=" + notes + "&asset_id=" + asset_id + "&odo=" + odo);
                        xhttp.send();
                    }
                }
            }
        }
        s
    }

    function get_odo() {
        // alert("Test");
        notes = document.getElementById('notes').value;
        reason_id = document.getElementById('cur_name').value;
        asset_id = document.getElementById('asset_id').value;

        if (asset_id.length < 1) {
            asset_id = '0';
        }

        // Create an XMLHttpRequest object
        const xhttp = new XMLHttpRequest();
        // Define a callback function
        xhttp.onload = function() {
            // alert(this.responseText);

            // Here you can use the Data
            if (this.responseText == 1) {

                alert(this.responseText);

            } else {

                document.getElementById('odo').value = this.responseText;
            }
        }

        xhttp.open("GET", "ajax/get_odo.ajax.php?&asset_id=" + asset_id);
        xhttp.send();

    }
</script>


</body>

</html>