<?php

include "../../classes/app.class.php";

$app = new app();
$db = new DBMain();

// $category_info = $db->get_category_info($_GET['category_id']);
$tools_info = $db->get_tools_info($_GET['record_id']);

?>

<div class='container flex_container'>
    <h1>EDIT TOOLS</h1>
    <hr>
    <div class="container flex_container_content" style="border: 1px solid black;padding: 2%; border-radius: 5px;">
        <div class='flex_container'>
            <label>Record:</label>
            <input type='text' name='record' id='record' readonly value="<?php echo $tools_info['record_id']; ?>">
        </div>
        <div class='flex_container'>
            <label>Name:</label>
            <input type='text' name='tool_name' id='tool_name' value="<?php echo $tools_info['name']; ?>">
        </div>
        <div class='flex_container'>
            <label>Brand:</label>
            <input type='text' name='tool_brand' id='tool_brand' value='<?php echo $tools_info['brand']; ?>'>
        </div>
    </div>
    <br><br>
    <div class="container flex_container_content" style="border: 1px solid black;padding: 2%; border-radius: 5px;">
        <div class='flex_container'>
            <label>QR CODE:</label>
            <input type='text' name='qr_code' id='qr_code' value='<?php echo $tools_info['qr_code']; ?>'>
        </div>
        <div class='flex_container'>
            <label>Category:</label>
            <input type='text' name='category_id' id='category_id' value='<?php echo $tools_info['category_id']; ?>'>
        </div>
    </div>
    <button onclick="update_tool()" id='add_tool_btn'>UPDATE TOOL</button>
</div>

<script>
    function update_tool(){
        qr_code = document.getElementById('qr_code').value;
        name = document.getElementById('tool_name').value;
        brand = document.getElementById('tool_brand').value;
        category = document.getElementById('category_id').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 == 0) {
                    alert("FAILED");
                } else if (this.responseText == 1) {
                    alert("PASSED");
                    window.location.href = 'search_tools.php';
                }
            }
            xhttp.open("GET", '../../ajax/tools/edit_tools.php?name=' + name + '&brand=' + brand + '&qr_code=' + qr_code + '&id=' + id + '&category_id=' + category);
            xhttp.send();
    }
}
</script>