<?php
include "classes/html.class.php";
$html = new html("ASSETS GROUP");
$db = new db();
$asset_type_res = $db->exec_query('service_types', ["*"]);
$service_type_list = '<option></option>';
while ($asset_type = $asset_type_res->fetch_assoc()) {
    $service_type_list = $service_type_list . "<option value='{$asset_type['record_id']}'>{$asset_type['name']}</option>";
}
$ins_res = $db->exec_query('pre_inspection_content', ["*"]);

$inspections = '<option></option>';
while ($ins = $ins_res->fetch_assoc()) {
    $inspections = $inspections . "<option value='{$ins['record_id']}'>{$ins['name']}</option>";
}

?>
<div class='form'>
    <h2 style="font-size:2vw">ADD ASSET GROUP:</h2>
    <hr><br>
    <input type='text' placeholder="GROUP NAME" id='asset_type_name' class='form_input' /><br><br>
    <label style="font-size:1.5vw">SERVICE TYPE: </label><br>
    <select id='service_type_id' class='form_input'>
        <?php echo $service_type_list; ?>
    </select><br><br>
    <label style="font-size:1.5vw">PRE INSPECTION LIST: </label><br>
    <select id='pre_inspection_id' class='form_input'>
        <?php echo $inspections; ?>
    </select><br><br>

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

</div>

<script>
    function ajax_func() {
        // alert("Test");
        asset_type_name = document.getElementById('asset_type_name').value;
        service_type_id = document.getElementById('service_type_id').value;
        pre_inspection_id = document.getElementById('pre_inspection_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(this.responseText);

            } else {
                alert("ADDED");

            }
        }

        xhttp.open("GET", "ajax/add_asset_type.ajax.php?asset_type_name=" + asset_type_name + "&service_type_id=" + service_type_id + "&pre_inspection_id=" + pre_inspection_id);
        xhttp.send();

    }
</script>


</body>

</html>