<?php
include "classes/html.class.php";
$html = new html("ASSETS");
$db = new db();
$asset_type_res = $db->exec_query('asset_types', ["*"]);
$qr_code = new qrcode();
$asset_type_list = '<option></option>';
while ($asset_type = $asset_type_res->fetch_assoc()) {
    $asset_type_list = $asset_type_list . "<option value='{$asset_type['record_id']}'>{$asset_type['asset_type_name']}</option>";
}
if ($_SESSION['farm_id'] == 0) {
    $farms_res = $db->exec_query('farms', ["*"]);
} else {
    $farms_res = $db->exec_query('farms', ["*"], '', '', '', '', "record_id = {$_SESSION['farm_id']}");
}
$farm_list = '<option></option>';
while ($farms = $farms_res->fetch_assoc()) {
    $farm_list = $farm_list . "<option value='{$farms['record_id']}'>{$farms['farm_name']}</option>";
}

?>
<div class='form' style="padding-bottom:2vw;">
    <h2 style="font-size:2vw;">ADD ASSET:</h2>
    <hr><br>

    <div class="center_2-1col">
        <div class="center_2-4col">
            <div class="">
                <label>ASSET ID: </label><br>
                <input type='text' placeholder="ASSET ID" onchange="check_asset_id()" id='asset_id' class='form_input' /><br><br>
            </div>
            <div class="">
                <label>FARM: </label><br>
                <select id='farm_id' class='form_input'>
                    <?php echo $farm_list; ?>
                </select><br><br>
            </div>
            <div class="">
                <label>FLEET ID: </label><br>
                <input type='text' placeholder="FLEET ID" onchange="check_fleet_id()" id='fleet_id' class='form_input' /><br><br>

            </div>
            <div class="">
                <label>ASSET TYPE: </label><br>
                <select id='asset_type_id' class='form_input'>
                    <?php echo $asset_type_list; ?>
                </select><br><br>
            </div>
            <div class="">
                <label>KM/Hours: </label><br>
                <input type='text' placeholder="ODO" id='odo' class='form_input' /><br><br>
            </div>
            <div class="">

                <label>VIN: </label><br>
                <input type='text' placeholder="ASSET MAKE" id='vin' class='form_input' /><br><br>
            </div>
            <div class="">
                <label>LISENCE EXP DATE: </label><br>
                <input type='Date' id='exp_date' class='form_input' /><br><br>
            </div>
            <div class="">
                <label>FUEL CAPASITY: </label><br>
                <input type='number' placeholder="ASSET MAKE" id='fuel' class='form_input' /><br><br>
            </div>
            <div class="">
                <label>DESCRIPTION: </label><br>
                <input type='text' placeholder="ASSET MODEL" id='description' class='form_input' /><br><br>
            </div>
        </div>


        <div class="center_1-4col">
            <div class="asset_QR">
                <input type='text' placeholder="QR Code" id='qr_code' class='form_input' /><br><br>
                <? echo $qr_code->scanner('qr_code', 'check_qr_code'); ?>
            </div>
        </div>


    </div>


    <input type='submit' value='ADD' class='form_btn' onclick='ajax_func()' />

</div>

<br>
<br>

<script>
    function check_qr_code() {
        qr_code = document.getElementById('qr_code').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 != 1) {

                alert("QR CODE ALREADY REGISTED WITH :" + this.responseText);
                document.getElementById('qr_code').value = '';
                document.getElementById('qr_code').style.backgroundColor = 'RED';
                document.getElementById('qr_code').focus;



            } else {
                document.getElementById('qr_code').style.backgroundColor = 'GREEN';

            }
        }

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

    function check_asset_id() {
        asset_id = document.getElementById('asset_id').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 != 1) {

                alert("ASSET ID ALREADY REGISTED WITH :" + this.responseText);
                document.getElementById('asset_id').value = '';
                document.getElementById('asset_id').style.backgroundColor = 'RED';
                document.getElementById('asset_id').focus;


            } else {
                document.getElementById('asset_id').style.backgroundColor = 'GREEN';

            }
        }

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

    function check_fleet_id() {
        fleet_id = document.getElementById('fleet_id').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 != 1) {

                alert("ASSET ID ALREADY REGISTED WITH :" + this.responseText);
                document.getElementById('fleet_id').value = '';
                document.getElementById('fleet_id').style.backgroundColor = 'RED';
                document.getElementById('fleet_id').focus;


            } else {
                document.getElementById('fleet_id').style.backgroundColor = 'GREEN';

            }
        }

        xhttp.open("GET", "ajax/check_fleet_id.ajax.php?fleet_id=" + fleet_id);
        xhttp.send();
    }

    function ajax_func() {
        // alert("Test");
        asset_id = document.getElementById('asset_id').value;
        odo = document.getElementById('odo').value;
        asset_type_id = document.getElementById('asset_type_id').value;
        qr_code = document.getElementById('qr_code').value;
        farm_id = document.getElementById('farm_id').value;
        exp_date = document.getElementById('exp_date').value;
        fleet_id = document.getElementById('fleet_id').value;
        description = document.getElementById('description').value;
        fuel = document.getElementById('fuel').value;
        vin = document.getElementById('vin').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 != 1) {

                alert(this.responseText);

            } else {
                window.location.href = "assets.php";
            }
        }

        xhttp.open("GET", "ajax/add_asset.ajax.php?asset_id=" + asset_id + "&odo=" + odo + "&asset_type_id=" + asset_type_id + '&qr_code=' + qr_code + '&farm_id=' + farm_id + '&exp_date=' + exp_date + '&vin=' + vin + '&fuel=' + fuel + '&description=' + description + '&fleet_id=' + fleet_id);
        xhttp.send();

    }
</script>


</body>

</html>