<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
// $html->check_user_type("ADMIN");

?>

<form class="form_down">
    
    <h1>ADD STOCK TAKE</h1>
    <?php
    echo $html->input_html(
        "date_created",
        date("Y-m-d H:i", strtotime("+ 2 Hours")),
        "date_created",
        "date_created",
        "hidden"
    );
    echo "<label>DATE STOCK TAKE TO BE DONE</label>";
    echo $html->date_input_html("date_to_be_done", 'date_to_be_done');
    echo $html->input_html(
        "user_id_created",
        $_SESSION['user_id'],
        "user_id_created",
        "user_id_created",
        "hidden"
    );
    echo $html->submit_btn('submit', "CREATE", '');
    ?>

</form>

<script>
    document.querySelector("#submit").addEventListener("click", (e) => {
        e.preventDefault();
        const http = new XMLHttpRequest();
        const formElement = document.querySelector(".form_down");
        if (formElement instanceof HTMLFormElement) {
            const formData = new FormData(formElement);
            http.addEventListener("load", () => {
                if (http.responseText == 1){
                    formElement.reset();
                    alert("STOCK TAKE INSTRUCTION CREATED");
                }else{
                    alert(http.responseText);
                }
            });
            http.open("POST", "add_stock_take_ajax.php");
            http.send(formData);
        } else {
            console.error("The selected element is not a form.");
        }
    });
</script>

