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

$html = new html();
$html->add_styles_page();
$html->prevent_enter_script();

$po_number = new input();
$po_number->name("po_number");
$po_number->value("");
$po_number->id("po_number");
$po_number->class("inputs");
$po_number->placeholder("PO NUMBER");
$po_number->onkeyup("search_stock_control()");


$jc_number = new input();
$jc_number->name("jc_number");
$jc_number->value("");
$jc_number->id("jc_number");
$jc_number->class("inputs");
$jc_number->placeholder("J.C. NUMBER");
$jc_number->onkeyup("search_stock_control()");



// $html->check_user_type("ADMIN");
?>
<div class="search_form">
    <?php $po_number->add(); ?>
    <?php echo $html->input_html("", "0", "status", "status", "hidden"); ?>
    <?php $jc_number->add(); ?>
    <?php echo $html->select_html_from_db("suppliers", "record_id", "name", "", "supplier_id", "supplier_id", "inputs", "width:50%;"); ?>

    <?php echo $html->submit_btn('submit', 'SEARCH', 'search_stock_control()', 'select_button'); ?>
</div>



<?php echo $html->html_form_ajax("search_stock_control", "SELECT", "orders", '', false, ['po_number', 'jc_number', 'supplier_id', 'date_time_created'], "receive_stock.php"); ?>

<script>
    search_stock_control();
    document.getElementById("submit").addEventListener("click", check_order_closed);
    function check_order_closed(e) {
        e.preventDefault();
        console.log("CLICKED");
        po_number = document.getElementById("po_number").value;
        if (po_number != "") {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                console.log(this.responseText);
                if (this.readyState == 4 && this.status == 200) {
                    if (this.responseText == "CLOSED") {
                        alert("PO NUMBER " + po_number + " IS CLOSED");
                        document.getElementById("po_number").value = "";
                    } else if (this.responseText == "NO THERE") {
                        alert("PO NUMBER " + po_number + " IS NOT FOUND");
                        document.getElementById("po_number").value = "";
                    }
                }
            };
            xhttp.open("GET", "check_order_closed.php?po_number=" + po_number, true);
            xhttp.send();
        }
    }
</script>