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


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




$description = new input();
$description->name("description");
$description->id("description");
$description->class("inputs");
$description->placeholder("DESCRIPTION");
$description->type("text");
$description->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("", "ACCOUNT 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_account()");
?>

<div class="form_down">
    <h2>ADD JOB CARD ACCOUNT</h2>
    <?php
    $name->add();
    $description->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_acc_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 account number already exist");
                    document.getElementById("name").value = "";
                    document.getElementById("name").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_account");
$ajax->submit_btn_id("submit");
$ajax->insert("job_card_accounts");
$ajax->on_success("SUCCESSFULLY ADDED THE ACCOUNT");
$ajax->redirect("jobcard_accounts_home.php");
?>