<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$call = new call_functions();
// $html->prevent_enter_script();
$db = new db_safeguard();
$jc_res = $db->query("job_cards_tech", "SELECT * FROM job_cards_tech WHERE record_id = {$_GET['record_id']}");
$jc = $jc_res->fetch_assoc();


$record_id = new input();
$record_id->name("record_id");
$record_id->id("record_id");
$record_id->class("inputs");
$record_id->value($_GET["record_id"]);
$record_id->type("hidden");
$record_id->readonly();

$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->value_from_db("job_cards_tech", "jc_number", "record_id = '" . $_GET["record_id"] . "'");
$jc_number->readonly();


$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->value_from_db("job_cards_tech", "date_time_created", "record_id = '" . $_GET["record_id"] . "'");
$date_time_created->readonly();
$date_time_created->readonly();

$new_jc = new select();
$new_jc->name("new_jc");
$new_jc->id("new_jc");
$new_jc->class("inputs");
$new_jc->add_option("0", "NO");
$new_jc->add_option("1", "YES");

$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_from_db("job_cards_tech", "action_date_time", "record_id = '" . $_GET["record_id"] . "'");
$action_date_time->required();
$action_date_time->readonly();


$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->value_from_db("job_cards_tech", "company_name", "record_id = '" . $_GET["record_id"] . "'");
$company_name->required();
$company_name->readonly();

$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->value_from_db("job_cards_tech", "company_address", "record_id = '" . $_GET["record_id"] . "'");
$company_address->required();
$company_address->readonly();

$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->value_from_db("job_cards_tech", "contact_person", "record_id = '" . $_GET["record_id"] . "'");
$contact_person->required();
$contact_person->readonly();

$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->value_from_db("job_cards_tech", "contact_number", "record_id = '" . $_GET["record_id"] . "'");
$contact_person_contact->required();
$contact_person_contact->readonly();

$equipment_needed = new select();
$equipment_needed->name("equipment_required");
$equipment_needed->id("equipment_required");
$equipment_needed->class("inputs");
$equipment_needed->value_from_db("job_cards_tech", "equipment_required", "record_id = '" . $_GET["record_id"] . "'");
$equipment_needed->add_option("", "EQUIPMENT");
$equipment_needed->add_option("LONG LADDER", "LONG LADDER");

$submit = new input();
$submit->name("submit");
$submit->type("submit");
$submit->id("submit");
$submit->class("submit_btn");
$submit->onclick("submit_form()");
$submit->value("SEND FOR QUOTATION");
?>

<form id="form_" style="width: 100%;">
    <div class="column">
        <h1>TECHNICAL JOB CARD</h1>
        <div class="row"
            style="width: 96%;background-color: aliceblue;min-height:4vh; border-radius: 2vw;padding: 1em;">
            <?php
            $record_id->add();
            $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="row">
            <?php
            $company_name->add();
            $company_address->add();
            ?>
        </div>

        <div class="row">
            <?php
            $contact_person->add();
            $contact_person_contact->add();
            ?>
        </div>
        <h1>EQUIPMENT</h1>
        <div class="row"
            style="width: 96%;background-color: aliceblue;min-height:4vh; border-radius: 2vw;padding: 1em;">
            <?php
            $equipment_needed->add();
            ?>
        </div>
        <h1>REPORTED ISSUE</h1>
        <div class="row"
            style="width: 96%;background-color: aliceblue;min-height:4vh; border-radius: 2vw;padding: 1em;">
            <?php
            $db = new db_safeguard();
            $res = $db->query("job_cards_tech", "SELECT * FROM job_cards_tech WHERE record_id = {$_GET['record_id']}");
            $data = $res->fetch_assoc();
            echo html_entity_decode($data['client_reported_issue']);
            ?>

        </div>
        <h1> TECHNICAL DETAILS</h1>
        <?php $get_start_res = $db->query("job_cards_tech_timeline", "SELECT * FROM job_cards_tech_timeline WHERE job_card_tech_id = {$_GET['record_id']} AND `type` = 'START'");
        if ($get_start_res->num_rows < 1) {
            ?>
            <input type="button" class="submit_btn" id="start_tech" onclick="start_tech_func()" value="START">
            <script>
                function start_tech_func() {
                    var xhr = new XMLHttpRequest();
                    xhr.open('GET', 'job_card_tech_timeline_add.php?record_id=<?php echo $_GET["record_id"]; ?>', true);
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            document.getElementById("tech_details").removeAttribute("hidden");
                            document.getElementById("start_tech").setAttribute("hidden", "true");
                        }
                    };
                    xhr.send();
                }
            </script>

            <div id="tech_details" hidden class='column'>
            <?php } ?>
            <div class='column'>
                <?php
                $details_res = $db->query("job_card_tech_details", "SELECT * FROM job_card_tech_details WHERE job_card_tech_id = {$_GET['record_id']}");
                $index = 1;
                if ($details_res->num_rows > 0) {
                    while ($details = $details_res->fetch_assoc()) {
                        if (strtotime($details['date_time']) < strtotime('+24 hours')) {
                            $area = "onchange=\"edit_jc_details('area',this.value,{$details['record_id']},this)\"";
                            $desc = "onchange=\"edit_jc_details('issue',this.value,{$details['record_id']},this)\"";
                            $part = "onchange=\"edit_jc_details('part_number',this.value,{$details['record_id']},this)\"";
                        } else {
                            $area = "";
                            $desc = "";
                            $part = "";
                        }
                        ?>
                        <div style="background-color: aliceblue; padding: 10px;border-radius: 2vw;">
                            <div class="row">
                                <div class="column">
                                    <h4>AREA <?php echo $index; ?></h4>
                                    <textarea <?php echo $area; ?> style="width:80%;min-height: 3.5vh;"
                                        class="inputs"><?php echo $details['area']; ?></textarea>
                                    <input type="text" hidden style="width:100%;" value="<?php echo $details['record_id']; ?>"
                                        class="inputs" name="detail_record_id_<?php echo $index; ?>" />
                                </div>
                                <div class="column">
                                    <h4>DESCRIPTION <?php echo $index; ?>:</h4>
                                    <textarea <?php echo $desc; ?> class="inputs"
                                    style="width:80%;min-height: 3.5vh;"><?php echo $details['issue']; ?></textarea>
                                </div>
                                <div class="column">
                                    <h4>PART NUMBER <?php echo $index; ?></h4>
                                    <textarea <?php echo $part; ?> style="width:80%;min-height: 3.5vh;"
                                        class="inputs"><?php echo $details['part_number']; ?></textarea>
                                </div>
                            </div>

                            <div class="row">
                                <div class="column">
                                    <a class='submit_btn' onclick="show_hide(<?php echo $index; ?>)"
                                        style="cursor: pointer; width:40%;">Show/Hide Image</a>
                                    <div style="width:100%;display:none;" id="imagess_<?php echo $index; ?>">
                                        <?php
                                        $images = explode(",", $details['image']);
                                        foreach ($images as $image) {
                                            if ($image != "") {
                                                ?>
                                                <img src="<?php echo "images/" . $image; ?>" alt=""
                                                    style="width: 50%; max-width: 30vw; margin-left: auto; margin-right: auto;"
                                                    onclick="window.open('<?php echo 'images/' . $image; ?>', '_blank');">
                                                <?php
                                            }
                                        }

                                        ?>
                                    </div>

                                </div>
                            </div>

                            <div id="details_notes" class="column">
                                <h2>FINDINGS</h2>
                                <?php
                                $detail_notes_res = $db->query("job_card_tech_detail_notes", "SELECT * FROM job_card_tech_detail_notes WHERE job_card_tech_detail_id = {$details['record_id']}");
                                $details_notes_index = 1;
                                while ($detail_notes = $detail_notes_res->fetch_assoc()) {
                                    ?>
                                    <div class="row"><textarea class="inputs"
                                            style="width: 100%;"><?php echo $detail_notes['date_time'] . " : " . $call->get_username($detail_notes['user_id']) . " -> " . $detail_notes['note']; ?></textarea>
                                    </div>
                                    <?php $details_notes_index++;
                                } ?>
                                <div id="details_notes_<?php echo $index; ?>" class="column">

                                </div>
                                <input type="number" id="detail_notes_index_<?php echo $index; ?>" hidden value="1" />
                                <input type="number" id="detail_record_id_index_<?php echo $index; ?>" hidden
                                    name="detail_record_id_index_<?php echo $index; ?>"
                                    value="<?php echo $details['record_id']; ?>" />
                                <!-- <input type="button" onclick="add_row_tech_detail_notes(<?php echo $index; ?>)"
                                    class="submit_btn" value="ADD FINDING"> -->
                            </div>
                        </div>
                        <br><br>
                        <?php $index++;
                    } ?>


                    <?php
                    $has_prev = 1;
                } else {
                    $has_prev = 0;
                    ?>

                <?php } ?>
                <br>
                <br>
            </div>
        </div>

        <div id='tech_details' class='column'>

        </div>
        <!-- <input type="button" onclick="add_row_tech_details()" class="submit_btn" value="ADD NEW ROW"> -->
        <script>

            <?php if ($has_prev == 1) { ?>
                var index = <?php echo $index; ?>;
            <?php } else { ?>
                var index = 1;
            <?php } ?>

            function edit_jc_details(column, value, record_id, input) {
                var xhr = new XMLHttpRequest();
                xhr.open('POST', 'edit_jc_details.ajax.php', true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        console.log(xhr.responseText);
                        input.style.backgroundColor = "green";
                    } else {
                        input.style.backgroundColor = "red";
                    }
                };
                xhr.send('record_id=' + record_id + '&value=' + value + '&column=' + column);
            }

            function show_hide(index_image) {
                var image = document.getElementById("imagess_" + index_image);
                if (image.style.display == "none") {
                    image.style.display = "block";
                } else {
                    image.style.display = "none";
                }
            }

            function add_row_tech_detail_notes(notes_index) {
                detials_index = document.getElementById("detail_notes_index_" + notes_index);
                index_details = detials_index.value = parseInt(detials_index.value) + 1;
                var div = document.createElement('div');
                div.className = "row";
                div.innerHTML = '<div class="column"><textarea class="inputs" style="width: 100%;" name="detial_notes_' + notes_index + '_' + index_details + '" id="detial_notes_' + notes_index + '_' + index_details + '"></textarea></div>';
                document.getElementById("details_notes_" + notes_index + "").appendChild(div);
            }


        </script>
        <br><br>
</form>



</div>

<script>
    function submit_form() {
        if (document.getElementById('signature').value == '') {
            alert('Please sign');
            return;
        } else {
            let form = document.forms[0];
            // let overlay = document.createElement('div');
            // overlay.style.position = 'fixed';
            // overlay.style.top = '0';
            // overlay.style.left = '0';
            // overlay.style.width = '100%';
            // overlay.style.height = '100%';
            // overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
            // overlay.style.zIndex = '9999';
            // document.body.appendChild(overlay);
            form.submit();
        }
    }

</script>