<?php include "../../root.class.php";

$html = new html();
$db = new db_safeguard();
$html->add_styles_page();
$barcode = new barcodeReader();

// $html->check_user_type("ADMIN");


?>

<style>
    body {
        padding: 4vw;
        margin: 5vw;
    }
</style>

<div class="form_down">
    <div class="search_form">

        <?php echo $html->input_html_from_db("orders", "jc_number", "record_id = {$_GET['record_id']}", "jc_number", "jc_number", "", "inputs", "", "readonly"); ?>

        <?php echo $html->select_html_from_db("users", "record_id", "username", "", "user_id", "user_id", "inputs", "", "", "disabled"); ?>

        <?php echo $html->select_from_db_add_current_selected_column("orders", 'user_id', "record_id = {$_GET['record_id']}", 'user_id'); ?>

        <?php echo $html->select_html_from_db("suppliers", "record_id", "name", "", "supplier_id", "supplier_id", "inputs", "", "", "disabled"); ?>

        <?php echo $html->select_from_db_add_current_selected_column("orders", 'supplier_id', "record_id = {$_GET['record_id']}", 'supplier_id'); ?>
        <form action="stock_receive.php" method="post">
            <?php echo $html->input_html("", date("Y-m-d H:i", strtotime("+2 hours")), "date_ordered", "date_ordered", "inputs", "", "", "text", "readonly") ?>
    </div>


    <div class="search_form">

        <h1>Items</h1>
    </div>
    <?php

    $index = 0;
    $items_res = $db->query("order_items", "SELECT * FROM order_items WHERE order_id = '{$_GET['record_id']}' AND stock_id != 0");
    echo "<div class='row'>";
    echo $html->input_html("", "ITEM", "", "", 'inputs', 'width:18vw;');
    echo $html->input_html("", "ORDERED", "", "");
    echo $html->input_html("", "RECEIVED TO DATE", "", "");
    echo $html->input_html("", "REMAINING", "", "");

    echo "</div>";
    while ($items = $items_res->fetch_assoc()) {




        echo "<div class='row'>";
        echo $html->input_html("", $items['stock_id'], "stock_id_$index", "stock_id_$index", "hidden");

        $amount_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE order_id = {$_GET['record_id']} AND stock_id = {$items['stock_id']}");

        if ($amount_res->num_rows > 0) {
            $total = 0;
            while ($amount = $amount_res->fetch_assoc()) {
                $total = $total + $amount['amount'];
            }
        } else {
            $total = 0;
        }

        echo "<textarea name='name_$index' id='name_$index' class='inputs' style='width:18vw;height:fit-content;padding:1vw;min-width:200px;' readonly>{$items['name']}</textarea>";
        echo $html->input_html("", $items['quantity'], "quantity_$index", "quantity_$index", "inputs", "", "", "text", "readonly");
        echo $html->input_html("", $total, "actual_amount_$index", "actual_amount_$index", "inputs", "", "onchange = check($index)", "", "readonly");
        echo $html->input_html("", $items['quantity'] - $total, "amount_remaining_$index", "amount_remaining_$index", "inputs", "", "readonly");


        if ($items['quantity'] == $total) {
            echo "<script>document.getElementById('quantity_$index').style.backgroundColor = 'green';</script>";
            echo "<script>document.getElementById('quantity_$index').style.color = 'white';</script>";
            echo "<script>document.getElementById('name_$index').style.backgroundColor = 'green';</script>";
            echo "<script>document.getElementById('name_$index').style.color = 'white';</script>";
            echo "<script>document.getElementById('actual_amount_$index').style.backgroundColor = 'green';</script>";
            echo "<script>document.getElementById('actual_amount_$index').style.color = 'white';</script>";

            echo "<script>document.getElementById('amount_remaining_$index').style.backgroundColor = 'green';</script>";
            echo "<script>document.getElementById('amount_remaining_$index').style.color = 'white';</script>";
        } else {
            echo "<script>document.getElementById('quantity_$index').style.backgroundColor = 'red';</script>";
            echo "<script>document.getElementById('quantity_$index').style.color = 'white';</script>";

            echo "<script>document.getElementById('name_$index').style.backgroundColor = 'red';</script>";
            echo "<script>document.getElementById('name_$index').style.color = 'white';</script>";

            echo "<script>document.getElementById('actual_amount_$index').style.backgroundColor = 'red';</script>";
            echo "<script>document.getElementById('actual_amount_$index').style.color = 'white';</script>";

            echo "<script>document.getElementById('amount_remaining_$index').style.backgroundColor = 'red';</script>";
            echo "<script>document.getElementById('amount_remaining_$index').style.color = 'white';</script>";



        }
        echo "</div>";

        $index++;
    }
    ?>
    <input type="text" name="order_id" id="order_id" hidden value="<?php echo $_GET['record_id']; ?>">
    <input type="text" name="counter" id="counter" hidden value="<?php echo $index; ?>">

    <input type="text" name="q_counter" id="q_counter" hidden value="<?php echo $index; ?>">
    <?php
    ?>
    </form>

    <script>
        function security_check() {

            quantity = document.getElementById('quantity').value;
            actual_amount = document.getElementById('actual_amount').value;

            if (actual_amount > quantity) {
                console.log("actual_amount may not be bigger than the quantity");
            }
        }

        function check(ind) {
            actual_amount = document.getElementById('actual_amount_' + ind);
            quantity = document.getElementById('quantity_' + ind);

            if (actual_amount.value * 1 > quantity.value * 1) {
                alert("Receiving more than ordered");
                actual_amount.value = 0;
                actual_amount.style.backgroundColor = "red";
            } else {
                actual_amount.style.backgroundColor = "green";
            }
            if (actual_amount.value * 1 < 0) {
                alert("Cant Be Negative Number");
                actual_amount.value = 0;
                actual_amount.style.backgroundColor = "red";
            } else {
                actual_amount.style.backgroundColor = "green";
            }
        }
    </script>