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



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

$status = new select();
$status->name("status");
$status->id("status");
$status->class("inputs");
$status->required();
$status->add_option("0", "INACTIVE");
$status->add_option("1", "SEND TO TECHNICIAN");
$status->add_option("2", "PJ TO APPROVE");
$status->add_option("3", "SENT FOR QUOTATION");
$status->add_option("4", "WAITING PO");
$status->add_option("5", "WAITING PARTS");
$status->add_option("6", "DECLINED");
$status->add_option("8", "SEND FOR INVOICING");
$status->add_option("9", "INVOICED");
$status->add_option("10", "PJ TO ASSIGN");
$status->add_option("11", "WARRANTY NO CHARGE");

$status->value_from_db("job_cards_tech", "status", "record_id = {$_GET['record_id']}");

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

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

$user_id = new input();
$user_id->name("user_id");
$user_id->id("user_id");
$user_id->class("hidden");
$user_id->value($_SESSION["user_id"]);
$user_id->type("hidden");
$user_id->value_from_db("job_cards_tech", "user_id", "record_id = '" . $_GET["record_id"] . "'");
$user_id->required();


$po_number = new input();
$po_number->name("client_po_number");
$po_number->id("client_po_number");
$po_number->placeholder("CLIENT PURCHASE ORDER NUMBER");
$po_number->class("inputs");
$po_number->type("text");
$po_number->value_from_db("job_cards_tech", "client_po_number", "record_id = '" . $_GET["record_id"] . "'");
// $po_number->required();


$quote_number = new input();
$quote_number->name("quote_number");
$quote_number->id("quote_number");
$quote_number->placeholder("QUOTE NUMBER");
$quote_number->class("inputs");
$quote_number->type("text");
$quote_number->value_from_db("job_cards_tech", "quote_number", "record_id = '" . $_GET["record_id"] . "'");
// $quote_number->required();

$user_to_do = new select();
$user_to_do->name("user_to_do");
$user_to_do->id("user_to_do");
$user_to_do->class("hidden");
$user_to_do->value_from_db("job_cards_tech", "user_to_do", "record_id = '" . $_GET["record_id"] . "'");
$user_to_do->add_option("", "TECHNICIAN");
$user_to_do->fill_from_db("users", "record_id", "username", "user_type = 'TECHNICIANS' OR record_id = 25");
$user_to_do->required();

$submit = new button();
$submit->name("submit");
$submit->id("submit");
$submit->class("submit_btn");
$submit->onclick("submit_form()");
$submit->value("UPDATE");
$call = new call_functions();
?>
<form action="update_tech_jobcard_process_quotation.php" method="POST" 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" hidden
            style="width: 96%;background-color: aliceblue;min-height:4vh; border-radius: 2vw;padding: 1em;">
            <?php

            $user_id->add();

            $user_to_do->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 "<textarea style='width:90%;min_height:5vh; border-radius: 2vw;padding: 1em;' name='client_reported_issue' readonly>" . html_entity_decode($data['client_reported_issue']) . "</textarea>";
            ?>

        </div>
        <br>

        <!-- TECHNICIAN -->
        <div style="border:4px solid black; height: 0px; width: 100%;"></div>
        <br>
        <h1> TECHNICAL DETAILS</h1>
        <div id="tech_details" 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;
            while ($details = $details_res->fetch_assoc()) {
                ?>
                <div style="background-color: aliceblue; padding: 10px;border-radius: 2vw;">
                    <div class="row">
                        <div class="column">
                            <h4>AREA <?php echo $index; ?></h4>
                            <textarea
                                onchange="edit_jc_details('area',this.value,<?php echo $details['record_id']; ?>,this)"
                                style="width:80%;min-height: 3.5vh;" name="area_1" id="area_1"
                                class="inputs"><?php echo $details['area']; ?></textarea>
                        </div>
                        <div class="column">
                            <h4>DESCRIPTION <?php echo $index; ?>:</h4>
                            <textarea class="inputs"
                                onchange="edit_jc_details('issue',this.value,<?php echo $details['record_id']; ?>,this)"
                                style="width: 80%;" name="issue_1" id="issue_1"><?php echo $details['issue']; ?></textarea>
                        </div>
                        <div class="column">
                            <h4>FAULTY PARTS<?php echo $index; ?></h4>
                            <textarea
                                onchange="edit_jc_details('part_number',this.value,<?php echo $details['record_id']; ?>,this)"
                                style="width:80%;min-height: 3.5vh;" name="part_number_1" id="part_number_1"
                                class="inputs"><?php echo $details['part_number']; ?></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="column">
                            <?php if ($details['image'] != "") {
                                ?>
                                <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>
                    <!-- TECHNICIAN FINDINGS -->
                    <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>
                    <div id="details_notes_<?php echo $index; ?>" class="column">
                    </div>
                    <input type="number" hidden id="detail_notes_index_<?php echo $index; ?>" value="1" />
                    <input type="number" hidden id="detail_record_id_index_<?php echo $index; ?>"
                        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>
                <br><br><br>
                <?php $index++;
            } ?>

            <!-- FINDINGS SCRIPT -->
            <script>

                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);
                }

                //<!-- EDIT DETAILS SCRIPT -->
                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);
                }

            </script>
        </div>
        <br>



        <div style="border:4px solid black; height: 0px; width: 100%;"></div>
        <!-- STICKY NOTES -->
        <br>
        <h1> STICKY NOTES</h1>
        <div id="tech_sticky_notes" class="column">
            <?php
            $notes_res = $db->query("job_card_tech_sticky_notes", "SELECT * FROM job_card_tech_sticky_notes WHERE job_card_tech_id = {$_GET['record_id']}");
            $index_sticky_notes = 1;
            while ($notes = $notes_res->fetch_assoc()) {
                ?>
                <div class="row">
                    <div class="column">

                        <textarea readonly class="inputs"
                            style="width: 100%;"><?php echo $notes['date_time'] . " : " . $call->get_username($notes['user_id']) . " -> " . $notes['note']; ?></textarea>
                    </div>
                </div>
                <?php $index_sticky_notes++;
            } ?>

        </div>
        <input type="button" onclick="add_row_tech_sticky_notes_details()" style="width: 50%;font-size:1em;"
            class="submit_btn" value="ADD STICKY NOTE">
        <script>
            index_sticky_notes = <?php echo $index_sticky_notes; ?>;
            function add_row_tech_sticky_notes_details() {

                var div = document.createElement('div');
                div.className = "row";
                div.innerHTML = '<div class="column"><textarea class="inputs" style="width: 100%;" name="sticky_notes_' + index_sticky_notes + '" id="sticky_notes_' + index_sticky_notes + '"></textarea></div>';
                document.getElementById("tech_sticky_notes").appendChild(div);
                index_sticky_notes++;
            }

        </script>
        <br>




        <!-- General Notes -->
        <div style="border:4px solid black; height: 0px; width: 100%;"></div>
        <br>
        <h1>GENERAL NOTES</h1>
        <div id="tech_notes" class="column">
            <?php
            $notes_res = $db->query("job_card_tech_notes", "SELECT * FROM job_card_tech_notes WHERE job_card_tech_id = {$_GET['record_id']}");
            $index_notes = 1;
            while ($notes = $notes_res->fetch_assoc()) {
                ?>
                <div class="row">
                    <div class="column">
                        <textarea readonly class="inputs"
                            style="width: 100%;"><?php echo $notes['date_time'] . " : " . $call->get_username($notes['user_id']) . " -> " . $notes['notes']; ?></textarea>
                    </div>
                </div>
                <?php $index_notes++;
            } ?>

        </div>
        <input type="button" onclick="add_row_tech_notes_details()" style="width: 50%;font-size:1em;" class="submit_btn"
            value="ADD NOTES">
        <script>
            index_notes = <?php echo $index_notes; ?>;
            function add_row_tech_notes_details() {

                var div = document.createElement('div');
                div.className = "row";
                div.innerHTML = '<div class="column"><textarea class="inputs" style="width: 100%;" name="notes_' + index_notes + '" id="issue_' + index_notes + '"></textarea></div>';
                document.getElementById("tech_notes").appendChild(div);
                index_notes++;
            }

        </script>
        <br>
        <div style="border:4px solid black; height: 0px; width: 100%;"></div>
        <!-- LOG -->
        <br>
        <h1>LOG</h1>
        <div id="log" class="column">
            <?php
            $log_res = $db->query("job_card_tech_status_timeline", "SELECT * FROM job_card_tech_status_timeline WHERE job_card_tech_id = {$_GET['record_id']} ORDER BY record_id ASC");
            while ($log = $log_res->fetch_assoc()) {
                ?>
                <div class="row">
                    <div class="column">
                        <textarea readonly class="inputs"
                            style="width: 100%;"><?php echo $log['date_time'] . " : " . $call->get_username($log['user_id']) . " -> " . $call->get_job_card_status($log['status']); ?></textarea>
                    </div>
                </div>
            <?php } ?>

        </div>

        <br>
        <?php
        $parts = $db->query("job_card_tech_returns", "SELECT * FROM job_card_tech_returns WHERE job_card_tech_id = {$_GET['record_id']}");
        if ($parts->num_rows > 0) {
            ?>
            <h1> PARTS RETURNED:</h1>
            <div id="returned_parts" class="column">
                <?php
                while ($part = $parts->fetch_assoc()) {
                    ?>
                    <div class="row"><textarea class="inputs"
                            style="width: 100%;"><?php echo $part['returned_part']; ?></textarea></div>
                    <?php
                }
                ?>
            </div>
            <br>

            <div style="border:4px solid black; height: 0px; width: 100%;"></div>

        <?php } ?>
        <br>
        <div class="column" style="background-color: aliceblue;">
            <?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();

            ?>
            <br>
            <h2> QUOTE </h2>

            <!-- QUOTE SECTION -->
            <?php $quote_number->add(); ?>

            <input type="file" name="quote_pdf_1" class="inputs" id="quote_pdf_1"
                onchange="upload_image(this,1,'quote_pdf')" />
            <input type="text" name="quote_file_1" class="inputs" id="quote_file_1" />
            <br>
            <div id="quote_pdf_progress_1"></div>
            <div id="quote_pdf_1">
                <?php if ($data['quote_pdf'] != null) {
                    echo "<a href='images/" . $data['quote_pdf'] . "' class='submit_btn' target='_blank'>VIEW</a>";
                } else {
                    echo "<span class='submit_btn'>NO QUOTE DOCUMENT UPLOADED YET</span>";
                } ?>
            </div>
            <br><br>
            <!-- PO SECTION -->
            <h2> PURCHASE ORDER </h2>
            <?php $po_number->add(); ?>
            <input type="file" name="po_pdf_1" class="inputs" id="po_pdf_1" onchange="upload_image(this,1,'po_pdf')" />
            <input type="text" name="po_file_1" class="inputs" id="po_file_1" />
            <br>
            <div id="po_pdf_progress_1"></div>
            <div id="po_pdf_1">
                <?php if ($data['po_pdf'] != null) {
                    echo "<a href='images/" . $data['po_pdf'] . "' class='submit_btn' target='_blank'>VIEW PO</a>";
                } else {
                    echo "<span class='submit_btn'>NO PURCHASE ORDER DOCUMENT UPLOADED YET</span>";
                } ?>
            </div>
            <br><br>
            <br>
            <script>
                function upload_image(input, index, name) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', 'image_upload.php?post_name=' + name + "_" + index, 'true', true);
                    xhr.upload.addEventListener('progress', function (e) {
                        if (e.lengthComputable) {
                            var percentage = (e.loaded / e.total) * 100;
                            document.getElementById(name + "_progress_" + index).innerHTML = percentage + "%";
                        }
                    });
                    xhr.onload = function () {
                        if (xhr.status == 200) {
                            if(name == 'po_pdf'){
                                document.getElementById("po_file_1").value = xhr.responseText.replace("images/", "");
                            }
                            if(name == 'quote_pdf'){
                                document.getElementById("quote_file_1").value = xhr.responseText.replace("images/", "");
                            }
                            document.getElementById(name + "_progress_" + index).innerHTML = "";
                            console.log(xhr.responseText);
                            document.getElementById(name + "_" + index).innerHTML = "<a href='" + xhr.responseText + "' target='_blank' class='submit_btn'>VIEW</a>";
                        }
                    };
                    xhr.send(new FormData(input.form));
                }
            </script>
        </div>


        <!-- TECHNICIAN -->
        <h1>CONCLUSION</h1>
        <div class="row" style="background-color: aliceblue;">
            <?php
            $status->add();
            ?>
        </div>


        <?php
        $submit->add();
        ?>
    </div>
    <br><br>
    <h2>PLEASE SAVE THE J.C. IF ANY CHANGES WAS MADE BEFORE OPENING THE PDF</h2>
    <div id="image_1">
        <input type="button" class="submit_btn" style="width: 50%;font-size:1em;" value="OPEN CLIENT J.C. PDF"
            onclick="open_pdf('client_jc_pdf.php?record_id=<?php echo $_GET['record_id']; ?>')" />
    </div>
    <script>
        function open_pdf(url) {
            window.open(url, '_blank');
        }
    </script>
</form>

<script>
    function submit_form() {
        document.querySelector("form").dispatchEvent(new Event('submit'));
    }

</script>