<?php

include "classes/html.class.php";

$html = new html("EVENTS");


$db = new db();

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

?>



<div class='form'>
    <h2 style="font-size:2vw">SELECT A LOG REASON:</h2>
    <select onchange='change_(this)' 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>
    <label style="font-size:1.5vw">REASON NAME:</label><br>
    <input type='text' placeholder="NAME" id='name' class='form_input' /><br><br>
    <input type='text' placeholder="ID" hidden id='id' class='form_input' /><br><br>
    <input type='submit' value='EDIT' class='form_btn' onclick='ajax_func()' />

</div>

<script>
    function change_(sel) {
        // alert(sel.options[sel.selectedIndex].text);
        document.getElementById('name').value = sel.options[sel.selectedIndex].text;
        document.getElementById('id').value = sel.options[sel.selectedIndex].value;
    }

    function ajax_func() {
        // alert("Test");
        name = document.getElementById('name').value;
        id = document.getElementById('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 = "reasons.php";
            }
        }

        xhttp.open("GET", "ajax/edit_log_reason.ajax.php?&name=" + name + "&id=" + id);
        xhttp.send();

    }
</script>


</body>

</html>