<?php

include "../../classes/app.class.php";
$app = new app();
?>

<div class='container flex_container'>
    <h1>NEW STOCK 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='stock_category_name' id='stock_category_name'>
        </div>
    </div>
    <button onclick="add_stock_category()" id='stock_category_btn'>ADD CATEGORY</button>
</div>

<script>

    function add_stock_category(){
        name = document.getElementById('stock_category_name').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');
                    window.location.href = 'stock_category.php';
                }
            }
            xhttp.open("GET", '../../ajax/stock/add_stock_category.php?name=' + name, true);
            xhttp.send();
            } else {
            document.getElementById('stock_category_btn').disabled = false;
        }
    }

</script>