<?php

include "../../classes/app.class.php";
$app = new app();
$db = new DBMain();
?>

<div class='container flex_container'>
    <h1>NEW TOOL</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_name' id='tool_name'>
        </div>
        <div class='flex_container'>
            <label>Brand:</label>
            <input type='text' name='tool_brand' id='tool_brand'>
        </div>
        <div class='flex_container'>
            <label>QR CODE:</label>
            <input type='text' name='qr_code' id='qr_code'>
        </div>
        <div class='flex_container'>
            <label>CATEGORY:</label>
            <select id='category_id' name='category_id'>
                <option></option>
                <?php echo $db->get_tool_category_list(); ?>
            </select>
        </div>
    </div>
    <button onclick="add_tool()" id='add_tool_btn'>ADD TOOL</button>
</div>

<script>

    function add_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;
        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');
                }
            }
            xhttp.open("GET", '../../ajax/tools/new_tools.php?name=' + name + '&brand=' + brand + '&qr_code=' + qr_code + '&category_id=' + category, true);
            xhttp.send();
            } else {
            document.getElementById('add_tool_btn').disabled = false;
        }
    }

</script>