<?php

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

$html = new html();
$db = new db_safeguard();
$html->add_styles_page();
$html->prevent_enter_script();
$call = new call_functions();
$jobcard_number = new input();
$jobcard_number->id("jobcard_number");
$jobcard_number->name("jobcard_number");
$jobcard_number->type("text");
$jobcard_number->placeholder("Jobcard Number");
$jobcard_number->required();
$jobcard_number->datalist("job_cards");
$jobcard_number->onchange("check_input_match_with_datalist(this)");


if (!isset($_SESSION['job_cards_list'])) {
    echo "<script>alert('PLEASE REFRESH THE SITE TO LOAD NEW DATA');</script>";
    echo "<script>window.location.href = 'home.php';</script>";
}

echo $_SESSION['job_cards_list'];


?>


<div class="form_down">
    <div class="row">
        <?php $jobcard_number->add(); ?>
    </div>
</div>

<script>
    function check_input_match_with_datalist(input) {
        var list = input.list;
        var options = list.options;
        var msg = [];
        for (var i = 0; i < options.length; i++) {
            msg.push(options[i].value);
        }
        if (msg.indexOf(input.value) === -1) {
            input.value = "";
            document.getElementById("jobcard_number").value = "";
            alert("PLEASE SELECT A JOB CARD NUMBER");
            input.style.backgroundColor = "red";
            input.style.color = "white";
        } else {
            document.getElementById("jobcard_number").value = input.value;
            input.readOnly = true;
            input.style.backgroundColor = "green";
            input.style.color = "white";
            document.getElementById("jobcard_number").setAttribute("readonly", "true");
            window.location.href = "return_stock.php?jobcard_number=" + input.value;
        }
    } 
</script>