<?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"] . "'");
$jc_number->required();
$jc_number->readonly();


$status = new select();
$status->name("status");
$status->id("status");
$status->class("inputs");
$status->required();
$status->add_option("7", "IN PROGRESS");
$status->add_option("2", "COMPLETED");


$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");

$submit = new button();
$submit->name("submit");
$submit->id("submit");
$submit->class("submit_btn");
$submit->onclick("submit_form()");
$submit->value("COMPLETE");

$call = new call_functions();
?>
<form action="update_tech_jobcard_process_action.php" method="POST" style="width: 100%;">
    <div class="column">
        <h1>TECHNICAL JOB CARD</h1>
        <div class="row" style="background-color: aliceblue;">
            <?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="background-color: aliceblue;">
            <?php
            $equipment_needed->add();
            ?>
        </div>
        <h1>REPORTED ISSUE</h1>
        <div class="row" 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();
            echo html_entity_decode($data['client_reported_issue']);
            ?>

        </div>
        <h1> INSTRUCTIONS </h1>
        <div id="tech_instructions" class="column">
            <?php
            $instruction_res = $db->query("job_card_tech_instructions", "SELECT * FROM job_card_tech_instructions WHERE job_card_tech_id = {$_GET['record_id']}");
            $index_instruction = 1;

            while ($instruction = $instruction_res->fetch_assoc()) {
                ?>
                <div class="row">
                    <div class="column">
                        <h4>INSTRUCTIONS <?php echo $index_instruction; ?></h4>
                        <div class="row">
                            <input readonly type="text" value="<?php echo $instruction['date_time']; ?>" class="inputs" />
                            <input readonly type="text" value="<?php echo $call->get_username($instruction['user_id']); ?>"
                                class="inputs" />
                        </div>
                        <textarea readonly class="inputs"
                            style="width: 100%;"><?php echo $instruction['instruction']; ?></textarea>
                    </div>
                </div>
                <?php $index_instruction++;
            } ?>
        </div>
        <h1> TECHNICAL DETAILS</h1>
        <div id="tech_details">

            <?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 class="row">
                    <div class="column">
                        <h4>ISSUE <?php echo $index; ?>:</h4>
                        <textarea class="inputs" style="width: 100%;" name="issue_1"
                            id="issue_1"><?php echo $details['issue']; ?></textarea>
                    </div>
                    <div class="column">
                        <h4>PART NUMBER <?php echo $index; ?></h4>
                        <input type="text" style="width:100%;" name="part_number_1" id="part_number_1" class="inputs"
                            value="<?php echo $details['part_number']; ?>" />
                    </div>
                    <div class="column">
                        <h4>AREA <?php echo $index; ?></h4>
                        <input type="text" style="width:100%;" name="area_1" id="area_1"
                            value="<?php echo $details['area']; ?>" class="inputs" />
                    </div>
                </div>
                <div class="row">
                    <div class="column">
                        <div id="image_<?php echo $index; ?>" style="width: 100%;height:50vh;overflow: scroll;">
                            <?php if ($details['image'] != "") { ?><img src="<?php echo "images/" . $details['image']; ?>"
                                    alt="" style="width:100%;">
                            <?php } ?>
                        </div>

                    </div>
                </div>
                <div id="details_notes" class="column">
                    <h2>NOTES</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>
                    </div>
                    <?php $details_notes_index++;
                    } ?>
                <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 NOTE">
            </div>
            <?php
            $index++;
            } ?>

    </div>
    <script>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>
    <h1>PARTS TO RETURN:</h1>
    <div id="returned_parts" class="column">

    </div>

    <input type="button" onclick="add_row_tech_return_parts()" class="submit_btn" value="ADD PART">

    <script>
        parts_index = 1;
        function add_row_tech_return_parts() {
            var div = document.createElement('div');
            div.className = "row";
            div.innerHTML = '<div class="column"><textarea class="inputs" style="width: 100%;" name = "returned_parts_' + parts_index + '" id = "returned_parts_' + parts_index + '" ></textarea ></div > ';
            document.getElementById("returned_parts").appendChild(div);
            parts_index++;
        }
    </script>
    </div>
    <h1>CONCLUSION</h1>
    <div class="column" style="background-color: aliceblue;">

        <?php
        $status->add();
        ?>
        <input type="text" style="width:100%;" name="signature" hidden id="signature" class="inputs" />
        <input type="text" style="width:100%;" name="singed" placeholder="NAME" id="singed" class="inputs" />
        <?php
        $signaturePad = new signature("signature", 1);
        ?>
    </div>
    </div>
</form>

<script>

</script>