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


$jc_number = new input();
$jc_number->name("jc_number");
$jc_number->id("jc_number");
$jc_number->class("inputs");
$jc_number->placeholder("JOB CARD NUMBER");
$jc_number->type("text");
$jc_number->onchange("check_jc_number(this.value)");
$jc_number->required();



$project_name = new input();
$project_name->name("project_name");
$project_name->id("project_name");
$project_name->class("inputs");
$project_name->placeholder("PROJECT NAME");
$project_name->type("text");
$project_name->required();


$status = new select();
$status->name("status");
$status->id("status");
$status->class("inputs");
$status->required();
$status->add_option("", "STATUS");
$status->add_option("1", "ACTIVE");
$status->add_option("0", "INACTIVE");


$account_manager_id = new select();
$account_manager_id->name("account_manager_id");
$account_manager_id->id("account_manager_id");
$account_manager_id->class("inputs");
$account_manager_id->add_option("", "PROJECT MANAGER");
$account_manager_id->fill_from_db("users", "record_id", "username");
$account_manager_id->required();

$submit = new button();
$submit->name("submit");
$submit->id("submit");
$submit->class("submit");
$submit->value("ADD");
$submit->onclick("add_jobcard()");
?>

<div class="form_down">
    <h2>ADD JOB CARD</h2>
    <?php
    $jc_number->add();
    $project_name->add();
    $status->add();
    $account_manager_id->add();
    $submit->add();
    ?>
</div>

<script>
    function check_jc_number(jc_number) {
        let xhr = new XMLHttpRequest();
        xhr.open('POST', 'check_jc_pj_number.ajax.php', true);
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.onload = function () {
            if (xhr.status === 200) {
                if (xhr.responseText == true) {
                    alert("Job card number already exist");
                    document.getElementById("jc_number").value = "";
                    document.getElementById("jc_number").focus();
                }
            } else {
                alert('Request failed.  Returned status of ' + xhr.status);
            }
        };
        xhr.send("jc_number=" + jc_number);

    }

</script>

<?php
$ajax = new js_ajax();
$ajax->function_name("add_jobcard");
$ajax->submit_btn_id("submit");
$ajax->insert("job_cards");
$ajax->on_success("SUCCESSFULLY ADDED THE JOB CARD");
$ajax->redirect("jobcard_home.php");
?>