<?php

include "../../root.class.php";

$html = new html();
$db = new db_safeguard();
$html->add_styles_page();
$html->prevent_enter_script();

?>
<div style="position: sticky;
    display: flex;
    top: 0px;
    font-size: 1.5em;
    background-color: grey;
    width: 100%;
    padding: 3vw;
    border-radius: 2vw;
    flex-direction: row;
    align-content: center;
    justify-content: center;
    align-items: center;">
    <input type="text" id="search_input" style="width:100vw; font-size: 1em;" class="inputs" placeholder="Search...">
    <button onclick="search()" class="select_button">Search</button>
    <script>
        function search() {
            var search_input = document.getElementById("search_input").value;
            var inputs = document.querySelectorAll(".form_down .inputs");
            index = 0;
            inputs.forEach(input => {
                input.style.backgroundColor = "white";
                if (input.value.toUpperCase().includes(search_input.toUpperCase())) {
                    if (index == 0) {
                        input.focus();
                    }
                    input.style.backgroundColor = "lightgrey";
                    index++;
                }

            });
        }
    </script>
</div>


<?php


function get_stock_name($stock_id)
{
    $db = new db_safeguard();
    $res = $db->query("stock", "SELECT `description` FROM stock WHERE record_id = '{$stock_id}'");
    $row = $res->fetch_assoc();
    return $row['description'];
}

// echo $_GET['jobcard_number'];

$transactions_amount_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE job_card_no = '{$_GET['jobcard_number']}' AND order_id = 0");
if ($transactions_amount_res->num_rows < 1) {
    echo "NO ITEMS DISPTACHED UNDER THIS JOB CARD";
} else {
    $index = 0;
    echo "<form action='return_stock_process.php?jobcard_number={$_GET['jobcard_number']}' method='post'>";
    echo "<div class='form_down'>";
    echo "<h1>STOCK</h1>";
    $get_dispatches_res = $db->query("order_trans", "SELECT DISTINCT stock_id FROM order_trans WHERE job_card_no = '{$_GET['jobcard_number']}' AND order_id = 0 AND units_stock_id = 0");
    while ($get_dispatches_row = $get_dispatches_res->fetch_assoc()) {

        $index++;

        $get_dispatches_res2 = $db->query("order_trans", "SELECT SUM(amount) as total FROM order_trans WHERE job_card_no = '{$_GET['jobcard_number']}' AND stock_id = '{$get_dispatches_row['stock_id']}' AND order_id = 0");

        $get_dispatches_row2 = $get_dispatches_res2->fetch_assoc();
        if ($get_dispatches_row2['total'] == 0) {
            continue;
        }
        echo "<div class='row'>";
        echo "<input type='text' class='hidden' name='stock_id_$index' id='stock_id_$index' value='" . $get_dispatches_row['stock_id'] . "' readonly>";
        echo "   <textarea name='name_$index' id='name_$index' class='inputs' style='width:18vw;height:fit-content;padding:1vw;min-width:200px;' readonly>" . get_stock_name($get_dispatches_row['stock_id']) . "</textarea>";
        echo "<input type='text' class='inputs' name='dispatched_amount_$index' id='dispatched_amount_$index'  value='" . abs($get_dispatches_row2['total']) . "' readonly>";
        echo "<input type='text' step='0.001' name='return_amount_$index' id='return_amount_$index' class='inputs' value='0' onchange='run_checks(this,$index);'>";
        echo "</div>";

    }

    echo "</div>";
    echo "<div class='form_down'>";
    echo "<h1>UNITS</h1>";
    $get_dispatches_res = $db->query("order_trans", "SELECT DISTINCT units_stock_id,stock_id FROM order_trans WHERE job_card_no = '{$_GET['jobcard_number']}' AND order_id = 0 AND units_stock_id > 0");
    while ($get_dispatches_row = $get_dispatches_res->fetch_assoc()) {
        // check total of units
        $amound_res = $db->query("order_trans", "SELECT SUM(amount) as total FROM order_trans WHERE units_stock_id = '{$get_dispatches_row['units_stock_id']}'");
        $amount_row = $amound_res->fetch_assoc();
        if ($amount_row['total'] > 0) {
            $index++;
            continue;
        }
        $index++;
        $get_dispatches_res2 = $db->query("units_stock", "SELECT * FROM units_stock WHERE record_id = '{$get_dispatches_row['units_stock_id']}'");
        $get_dispatches_row2 = $get_dispatches_res2->fetch_assoc();
        echo "<div class='row'>";
        echo "<input type='hidden' name='unit_id_$index' id='unit_id_$index' class='inputs' value='" . $get_dispatches_row['units_stock_id'] . "' readonly>";
        echo "<input type='hidden' name='stock_id_$index' id='unit_id_$index' class='inputs' value='" . $get_dispatches_row['stock_id'] . "' readonly>";
        echo "   <textarea name='unit_name_$index' id='unit_name_$index' class='inputs' style='width:18vw;height:fit-content;padding:1vw;min-width:200px;' readonly>" . $get_dispatches_row2['description'] . "</textarea>";
        echo "   <textarea class='inputs' style='width:18vw;height:fit-content;padding:1vw;min-width:200px;' readonly>" . $get_dispatches_row2['serial_number'] . "</textarea>";
        echo "<input type='text' step='0.001' name='return_amount_$index' id='return_amount_$index' class='inputs' value='0' onchange='run_unit_checks(this,$index);'>";
        echo "</div>";

    }
    echo "<input type='text' name='signature' id='signature' hidden required>";
    echo "<input type='text' name='received' id='received' class='inputs' placeholder='RETURNED BY?' required>";
    new signature("signature");
    echo "<button type='submit' class='submit_btn' name='return_stock' value='{$_GET['jobcard_number']}' onclick='submit_form()'>RETURN STOCK</button>";

    echo "</form></div>";


}

?>


<script>
    function submit_form() {
        let all_inputs = document.querySelectorAll(".inputs");
        let is_all_filled = true;
        all_inputs.forEach(input => {
            if (input.value.trim() == "") {
                input.style.backgroundColor = "red";
                is_all_filled = false;
            }
        });
        if (!is_all_filled) {
            return;
        }
        if (document.getElementById('signature').value == '') {
            alert('Please sign');
            return;
        }
        let form = document.forms[0];
        form.submit();
    }
    function run_checks(input, index) {
        let dispatched_amount = document.getElementById("dispatched_amount_" + index).value;
        let return_amount = document.getElementById("return_amount_" + index).value;
        if (return_amount * 1 > dispatched_amount * 1) {
            // input.value = 0;
            // input.style.backgroundColor = "red";
            // input.style.color = 'white';
            // alert("Return amount cannot be greater than dispatched amount :" + dispatched_amount);
        } else if (return_amount < 0) {
            input.value = 0;
            input.style.backgroundColor = "red";
            input.style.color = 'white';
            alert("Return amount cannot be less than 0");
        }
        else {
            input.style.backgroundColor = "green";
            input.style.color = 'white';
        }
    }

    function run_unit_checks(input, index) {
        let return_amount = document.getElementById("return_amount_" + index).value;
        if (return_amount < 0 || return_amount > 1) {
            input.value = 0;
            input.style.backgroundColor = "red";
            input.style.color = 'white';
            alert("Return amount cannot be less than 0");
        }
        else {
            input.style.backgroundColor = "green";
            input.style.color = 'white';
        }
    }
</script>