<?php

include "classes/html.class.php";

$html = new html("j.c. type");

$db = new db();

$res = $db->exec_query('jobcard_types', ['*']);
$list = '<option></option>';
while ($row = $res->fetch_assoc()) {
    $list = $list . "<option value='{$row['record_id']}'>{$row['name']}</option>";
}
?>



<div class='form'>
    <h2 style="font-size:2vw">SELECT JOBCARD TYPE:</h2>
    <select class='form_input' id='jobcard_type_id' onchange="get_info()">
        <? echo $list; ?>
    </select>
    <h2 style="font-size:2vw">ADD JOB CARD TYPE:</h2>
    <hr><br>
    <input type='text' placeholder="NAME" id='name' class='form_input' /><br><br>
    <label style="font-size:1.5vw">TRIGGER WARNING EVENT ON OPEN?</label> <br><br>
    <select class='form_input' id='warning'>
        <option>NO</option>
        <option>YES</option>
    </select><br><br>
    <input type='submit' value='ADD' class='form_btn' onclick='ajax_func()' />
</div>

<script>
    function ajax_func() {
        // alert("Test");
        name = document.getElementById('name').value;
        warning = document.getElementById('warning').value;
        jobcard_type_id = document.getElementById('jobcard_type_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');
                window.location.href = "edit_jobcard_type.php";
            }
        }

        xhttp.open("GET", "ajax/edit_jobcard_type.ajax.php?name=" + name + "&warning=" + warning + "&jobcard_type_id=" + jobcard_type_id);
        xhttp.send();

    }


    function get_info() {
        // alert("Test");
        jobcard_type_id = document.getElementById('jobcard_type_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);
                values = this.responseText.split(',');
                document.getElementById('name').value = values[0];
                document.getElementById('warning').value = values[1];




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

            }
        }

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

    }
</script>


</body>

</html>