<?php include "../../root.class.php";
$db = new db_safeguard();
$html = new html();
$html->add_styles_page();
// $html->check_user_type("ADMIN");


$category_id = new select();
$category_id->id("category_id");
$category_id->name("category_id");
$category_id->class("inputs");
$category_id->fill_from_db("stock_categories", "record_id", "name");
$category_id->value_from_db("stock", "category_id", "record_id = {$_GET['record_id']}");


$item_code = new input();
$item_code->id("item_code");
$item_code->name("item_code");
$item_code->class("inputs");
$item_code->value_from_db("stock", "item_code", "record_id = {$_GET['record_id']}");
$item_code->type("text");


$barcode = new barcodeReader();
?>

<div class="inputs" style="width:90%;">
    <h1>EDIT STOCK</h1>
</div>
<div class="form_down">
    <label>TYPE</label>
    <select id="stock_type" name="stock_type" class="inputs">
        <option></option>
        <option value="0">STOCK</option>
        <option value="1">NON STOCK</option>
        <option value="2">UNITS</option>
    </select>
    <?php

    echo $html->select_from_db_add_current_selected_column("stock", 'stock_type', "record_id = {$_GET['record_id']}", 'stock_type');

    echo $html->input_html("record_id", $_GET['record_id'], 'record_id', 'record_id', 'hidden');

    echo "<label>XERO ITEM CODE</label>";
    $item_code->add();

    echo "<label>CATEGORY</label>";

    // echo $html->select_html_from_db('stock_categories', 'record_id', 'name', "1", 'category_id', 'category_id');
    // echo $html->select_from_db_add_current_selected_column("stock", 'category_id', "record_id = {$_GET['record_id']}", 'category_id');
    
    $category_id->add();
    echo "<label>SUPPLIER</label>";

    echo $html->select_html_from_db('suppliers', 'record_id', 'name', "1", 'supplier_id', 'supplier_id');
    echo $html->select_from_db_add_current_selected_column("stock", 'supplier_id', "record_id = {$_GET['record_id']}", 'supplier_id');

    echo "<label>DESCRIPTION</label>";
    $db = new db_safeguard();
    $res = $db->query("stock", "SELECT * FROM `stock` WHERE record_id = {$_GET['record_id']}");
    $data = $res->fetch_assoc();
    echo "<textarea class='inputs' name='description' id='description' placeholder='Description' rows='4' cols='50'>" . $data['description'] . "</textarea>";
    // echo $html->text_formator_from_db("description", "stock", 'description', "record_id = {$_GET['record_id']}");

    echo "<label>PACKAGE SIZE</label>";

    echo $html->input_html_from_db('stock', 'packaging_size', "record_id = {$_GET['record_id']}", 'packaging_size', 'packaging_size', 'packaging_size');

    echo "<label>COST PRICE</label>";

    echo $html->input_html_from_db('stock', 'cost_price', "record_id = {$_GET['record_id']}", 'cost price', 'cost_price', 'cost_price');


    echo "<label>STOCK ADJUSTMENT ON IMPORT</label>";

    echo "<label>X + - /</label>";

    echo $html->input_html_from_db('stock', 'adjustment_type', "record_id = {$_GET['record_id']}", 'Adjustment Type', 'adjustment_type', 'adjustment_type');

    echo "<label>AMOUNT OF ADJUSTMENT</label>";

    echo $html->input_html_from_db('stock', 'amount', "record_id = {$_GET['record_id']}", 'Amount', 'amount', 'amount');


    echo "<label>MAX STOCK</label>";

    echo $html->input_html_from_db('stock', 'max', "record_id = {$_GET['record_id']}", 'Max Amount', 'max', 'max');

    echo "<label>MIN STOCK</label>";

    echo $html->input_html_from_db('stock', 'min', "record_id = {$_GET['record_id']}", 'Min Amount', 'min', 'min');
    ?>
    IN STOCK:
    <input type='text' readonly
        value='<?php echo $db->query("order_trans", "SELECT sum(amount) as in_stock FROM order_trans WHERE stock_id = {$_GET['record_id']}")->fetch_assoc()['in_stock']; ?>'
        class="inputs" />
    <?php

    echo "<label>BARCODE</label>";

    echo $html->input_html_from_db('stock', 'barcode', "record_id = {$_GET['record_id']}", 'QR/Bar-code', 'barcode', 'barcode');


    // echo $barcode->add_button("barcode");
    // echo $html->QRCodeScanner("barcode");
    // echo $html->qr_code_generator($_GET['record_id'], $_GET['record_id']);
    // echo "<img src='../../images/qr_codes/{$_GET['record_id']}.png' alt='QR Code' />";
    // echo "<a href='../../images/qr_codes/{$_GET['record_id']}.png' download>Download QR Code</a>";
    
    echo $html->submit_btn("submit", 'SAVE', 'stock()', 'select_button'); ?>

</div>

<?php
$html->html_form_ajax("stock", "UPDATE", "stock", '', false, [], '', [], 'barcode-zoom-control-0,');
$stock_res = $db->query("order_trans", "SELECT * FROM `order_trans` WHERE units_stock_id != 0 AND amount = 1 AND stock_id = {$_GET['record_id']}");
if ($stock_res->num_rows > 0) {
    ?>
    <br><br>
    <div class="columns">
        <h1>UNITS IN STOCK</h1>

        <div class="row">
            <input type="text" class="inputs" value="DESCRIPTION">
            <input type="text" class="inputs" value="SERIAL NUMBER">
        </div>
        <?php

        while ($stock = $stock_res->fetch_assoc()) {
            ?>
            <div class="row">
                <?php
                echo $html->input_html_from_db('units_stock', 'description', "record_id = {$stock['units_stock_id']}");
                echo $html->input_html_from_db('units_stock', 'serial_number', "record_id = {$stock['units_stock_id']}");
                ?>
            </div>
            <?php
        } ?>
    </div>

<?php } ?>