<?php

include "../../classes/app.class.php";

$app = new app();
$db = new DBMain();

$stock_info = $db->get_stock_info($_GET['record_id']);
?>

<div class='container flex_container'>
    <h1>EDIT STOCK</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 $stock_info['record_id']; ?>">
            </div>
            <div class='flex_container'>
                <label>Name:</label>
                <input type='text' name='name' id='name' value="<?php echo $stock_info['name']; ?>">
            </div>
            <div class='flex_container'>
                <label>Category:</label>
                <input type='text' name='category_id' id='category_id' <?php echo $stock_info['category_id']; ?>>
            </div>
            <div class='flex_container'>
                <label>Description:</label>
                <input type='text' name='description' id='description' value='<?php echo $stock_info['description']; ?>'>
            </div>
    </div>
    <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 $stock_info['qr_code']; ?>'>
            </div>
            <div class='flex_container'>
                <label>Shelf:</label>
                <input type='text' name='shelf' id='shelf' value="<?php echo $stock_info['shelf']; ?>">
            </div>
            <div class='flex_container'>
                <label>Row:</label>
                <input type='text' name='row' id='row' value="<?php echo $stock_info['row']; ?>">
            </div>
    </div>
    <button onclick="update_stock()" id='add_tool_btn'>UPDATE TOOL</button>
</div>

<script>
    function update_stock(){
        name = document.getElementById('name').value;
        description = document.getElementById('description').value;
        qr_code = document.getElementById('qr_code').value;
        shelf = document.getElementById('shelf').value;
        row = document.getElementById('row').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_stock.php';
                }
            }
            xhttp.open("GET", '../../ajax/stock/edit_stock.php?name=' + name + '&description=' + description + '&qr_code=' + qr_code + '&shelf=' + shelf + '&row=' + row + '&id=' + id + '&category_id=' + category);
            xhttp.send();
    }
}
</script>