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

$client_data_list = "<datalist id='companies'>";
$res = $db->query("job_cards_tech", "SELECT DISTINCT company_name,  company_address,  contact_person,  contact_number FROM job_cards_tech WHERE 1");
while ($data = $res->fetch_assoc()) {
    $client_data_list .= "<option value='" . $data['company_name'] . "~" . $data['company_address'] . "~" . $data['contact_person'] . "~" . $data['contact_number'] . "'></option>";
}
$client_data_list .= "</datalist>";

$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");


$date_time_created = new input();
$date_time_created->name("date_time_created");
$date_time_created->id("date_time_created");
$date_time_created->class("inputs");
$date_time_created->type("text");
$date_time_created->required();
$date_time_created->readonly();

$action_date_time = new input();
$action_date_time->name("action_date_time");
$action_date_time->id("action_date_time");
$action_date_time->class("inputs");
$action_date_time->type("datetime-local");
$action_date_time->value(date("Y-m-d H:i"));
$action_date_time->required();

$company_name = new input();
$company_name->name("company_name");
$company_name->id("company_name");
$company_name->class("inputs");
$company_name->style("width: 100%");
$company_name->placeholder("COMPANY NAME");
$company_name->type("text");
$company_name->datalist("companies");
$company_name->onchange("get_client_data(this);");
$company_name->required();

$company_address = new input();
$company_address->name("company_address");
$company_address->id("company_address");
$company_address->class("inputs");
$company_address->style("width: 100%");
$company_address->placeholder("COMPANY ADDRESS");
$company_address->type("text");
$company_address->required();

$contact_person = new input();
$contact_person->name("contact_person");
$contact_person->id("contact_person");
$contact_person->style("width: 100%");
$contact_person->class("inputs");
$contact_person->placeholder("CONTACT PERSON NAME");
$contact_person->type("text");
$contact_person->required();

$contact_person_contact = new input();
$contact_person_contact->name("contact_person_contact");
$contact_person_contact->id("contact_person_contact");
$contact_person_contact->class("inputs");
$contact_person_contact->style("width: 100%");
$contact_person_contact->placeholder("CONTACT PERSON NUMBER");
$contact_person_contact->type("text");
$contact_person_contact->required();

$quote_number = new input();
$quote_number->name("quote_number");
$quote_number->id("quote_number");
$quote_number->class("inputs");
$quote_number->placeholder("QUOTE NUMBER");
$quote_number->type("text");

$po_number = new input();
$po_number->name("client_po_number");
$po_number->id("client_po_number");
$po_number->class("inputs");
$po_number->placeholder("CLIENT PO NUMBER");
$po_number->type("text");

$user_id = new input();
$user_id->name("user_id");
$user_id->id("user_id");
$user_id->class("inputs");
$user_id->value($_SESSION["user_id"]);
$user_id->type("hidden");
$user_id->required();

$status = new input();
$status->name("status");
$status->id("status");
$status->class("inputs");
$status->type("hidden");
$status->value(1);
$status->required();
// $status->add_option("", "STATUS");
// $status->add_option("10", "SEND TO PJ TO ASSIGN");
// $status->add_option("1", "SEND TO TECHNICIAN");
// $status->add_option("2", "SEND TO PJ APPROVE");
// $status->add_option("3", "SEND TO QUOTATION");
// $status->add_option("4", "WAITING FOR CLIENT APPROVAL");
// $status->add_option("5", "WAITING FOR PARTS");
// $status->add_option("6", "CLIENT DECLINED");
// $status->add_option("7", "SEND TO TECHNICIAN FOR WORK");
// $status->add_option("8", "SEND FOR INVOICING");
// $status->add_option("9", "INVOICED");
// $status->add_option("0", "INACTIVE");

$user_to_do = new input();
$user_to_do->name("user_to_do");
$user_to_do->id("user_to_do");
$user_to_do->class("hidden");
$user_to_do->value($_SESSION['user_id']);
$user_to_do->required();

$equipment_needed = new select();
$equipment_needed->name("equipment_required");
$equipment_needed->id("equipment_required");
$equipment_needed->class("inputs");
$equipment_needed->add_option("", "EQUIPMENT");
$equipment_needed->add_option("LONG LADDER", "LONG LADDER");

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

?>

<?php echo $client_data_list; ?>

<style>
    input {
        height: 6em;
    }

    body {
        font-size: 2em;
    }
</style>
<form action="save_tech_jobcard_from_tech.php" method="POST" style="width: 100%;" autocomplete="off">
    <div class="column">
        <h1>TECHNICAL JOB CARD</h1>
        <div class="row" style="background-color: aliceblue;display:flex;flex-direction: column;flex-wrap: nowrap;">
            <?php
            $date_time_created->add();
            $jc_number->add();
            ?>
            <div class="column" style="text-align:center">
                <label class="label">ACTION DATE</label>
                <?php
                $action_date_time->add();
                ?>
            </div>
        </div>
        <h1>CLIENT DETAILS</h1>
        <div class="column">
            <?php
            $company_name->add();
            $company_address->add();
            $contact_person->add();
            $contact_person_contact->add();
            ?>
        </div>

        <div class="row" style="background-color: aliceblue;">
            <?php
            $user_id->add();
            $user_to_do->add();
            ?>
        </div>
        <h1>REPORTED ISSUE</h1>
        <div class="row" style="background-color: aliceblue;">
            <textarea class='inputs' style='width:90%;min_height:5vh; border-radius: 2vw;padding: 1em;'
                name='client_reported_issue'></textarea>
        </div>
        <h1>EQUIPMENT</h1>
        <div class="row" style="background-color: aliceblue;">
            <?php
            $equipment_needed->add();
            ?>
        </div>
        <div class="row" style="background-color: aliceblue;">
            <?php
            $status->add();
            ?>
        </div>
        <?php
        $submit->add();
        ?>
    </div>
</form>

<script>

    function check_jc_number(jc_number) {
        let xhr = new XMLHttpRequest();
        xhr.open('POST', 'check_jc_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);

    }


    var date_time_created = document.getElementById("date_time_created");
    function updateDateTimeCreated() {
        let date = new Date();
        let year = date.getFullYear();
        let month = ("0" + (date.getMonth() + 1)).slice(-2);
        let day = ("0" + date.getDate()).slice(-2);
        let hour = ("0" + date.getHours()).slice(-2);
        let minute = ("0" + date.getMinutes()).slice(-2);
        date_time_created.value = year + "-" + month + "-" + day + " " + hour + ":" + minute;
    }
    updateDateTimeCreated();
    setInterval(updateDateTimeCreated, 1000);

    function get_client_data(input) {
        data = input.value;
        if(data.indexOf("~") == -1){
            return;
        }
        data = data.split("~");
        input.value = '';
        document.getElementById("company_name").value = data[0].replace(/  +/g, ' ');
        document.getElementById("company_address").value = data[1];
        document.getElementById("contact_person").value = data[2];
        document.getElementById("contact_person_contact").value = data[3];
    }
</script>