<?php

include "../../classes/app.class.php";
$app = new app();
$db = new DBMain();
?>

<div class='container flex_container'>
    <h1>NEW STOCK</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_name' id='stock_name'>
        </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>
    <div class="container flex_container_content" style="border: 1px solid black;padding: 2%; border-radius: 5px;width:48.5%; margin-top: 1%;">
        <label>description:</label>
        <textarea style='width:100%;' id='stock_description'></textarea>
    </div>
    <br>
    <div class="container flex_container_content" style="border: 1px solid black;padding: 2%; border-radius: 5px;">
        <div class='flex_container'>
            <label>Shelf:</label>
            <input type='text' name='stock_shelf' id='stock_shelf'>
        </div>
        <div class='flex_container'>
            <label>Row:</label>
            <input type='text' name='stock_row' id='stock_row'>
        </div>
    </div>

    <button onclick="add_stock()" id='add_stock_btn'>ADD STOCK</button>
</div>

<script>
    function add_stock(){
        name = document.getElementById('stock_name').value;
        description = document.getElementById('stock_description').value;
        qr_code = document.getElementById('qr_code').value;
        stock_shelf = document.getElementById('stock_shelf').value;
        stock_row = document.getElementById('stock_row').value;
        category = document.getElementById('category').value;
        error = 0;

        inputs = document.getElementsByTagName('input');
        textarea = document.getElementsByTagName('textarea');

        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';
            }
        }

        for (var i = 0; i < textarea.length; i++) {
            if (textarea[i].value == "") {
                textarea[i].style.border = '2px solid red';
                error = 1;
            } else {
                textarea[i].style.border = '2px solid green';
            }
        }

        if (error == 0) {
            var xhttp = new XMLHttpRequest();
            xhttp.onload = function() {
                alert(this.responseText);
                if (this.responseText == 1) {
                    alert('Stock added successfully');
                }
            }
            xhttp.open("GET", '../../ajax/stock/add_stock.php?name=' + name + '&qr_code=' + qr_code + '&description=' + description + '&stock_shelf=' + stock_shelf + '&stock_row=' + stock_row + '&category_id=' + category);
            xhttp.send();
            } else {
            document.getElementById('add_stock_btn').disabled = false;
        }
    }
</script>