<?php

include "../../classes/app.class.php";
$app = new app();
$db = new DBMain();


$tools_category_info = $db->get_tools_category_info($_GET['record_id']);
?>

<div class='container flex_container'>
    <h1>NEW TOOL CATEGORY</h1>
    <hr>
    <div class="container flex_container_content" style="border: 1px solid black;padding: 2%; border-radius: 5px;">
        <div class='flex_container'>
            <label>Name:</label>
            <input type='text' name='tool_category_name' id='tool_category_name' value="<?php echo $tools_category_info['name'] ?>">
        </div>
    </div>
    <button onclick="update_tool_category()" id='tool_category_btn'>UPDATE CATEGORY</button>
</div>

<script>

    function update_tool_category(){
        name = document.getElementById('tool_category_name').value;
        id = <?php echo $_GET['record_id']; ?>;
        error = 0;
        inputs = document.getElementsByTagName('input');
        
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].value == "") {
                inputs[i].style.border = '2px solid red';
                error = 1;
            } else {
                inputs[i].style.border = '2px solid green';
            }
        }

        if (error == 0) {
            var xhttp = new XMLHttpRequest();
            xhttp.onload = function() {
                // alert(this.responseText);
                if (this.responseText == 1) {
                    // alert('Tool added successfully');
                    window.location.href = 'tool_category.php';
                }
            }
            xhttp.open("GET", '../../ajax/tools/edit_tool_category.php?name=' + name + '&id=' + id, true);
            xhttp.send();
            } else {
            document.getElementById('tool_category_btn').disabled = false;
        }
    }

</script>