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

// Time In / Time Out (auto-captured, display only)
$time_label = new label();
$time_label->for("time_in_out");
$time_label->value("Time In / Time Out");

// Work Performed
$work_label = new label();
$work_label->for("work_performed");
$work_label->value("Work Performed (Description)");

$work_input = new input();
$work_input->type("text");
$work_input->id("work_performed");
$work_input->name("work_performed");
$work_input->style("width:80%");

// Add picture of work performed
$pic_label = new label();
$pic_label->for("work_picture");
$pic_label->value("Add picture of work performed (Not compulsory)");

$pic_upload = new file_upload();
$pic_upload->id("work_picture");
$pic_upload->name("work_picture");

// Parts Used
$parts_label = new label();
$parts_label->for("parts_used");
$parts_label->value("Parts Used");

$parts_input = new input();
$parts_input->type("text");
$parts_input->id("parts_used");
$parts_input->name("parts_used");
$parts_input->style("width:80%");

// Chemicals / Lubricants used?
$chem_label = new label();
$chem_label->for("chem_used");
$chem_label->value("Chemicals / Lubricants used?");

$chem_select = new select();
$chem_select->id("chem_used");
$chem_select->name("chem_used");
$chem_select->add_option("No", "No");
$chem_select->add_option("Yes", "Yes");

// Food contact surfaces touched?
$food_label = new label();
$food_label->for("food_contact");
$food_label->value("Was food contact surface touched or worked on?");

$food_select = new select();
$food_select->id("food_contact");
$food_select->name("food_contact");
$food_select->add_option("No", "No");
$food_select->add_option("Yes", "Yes");

// Job Completed?
$done_label = new label();
$done_label->for("job_completed");
$done_label->value("Has the job been completed / equipment fixed?");

$done_select = new select();
$done_select->id("job_completed");
$done_select->name("job_completed");
$done_select->add_option("Yes", "Yes");
$done_select->add_option("No", "No");

// Technician Notes
$notes_label = new label();
$notes_label->for("tech_notes");
$notes_label->value("Technician Notes");


$notes_select = new select();
$notes_select->id("tech_notes");
$notes_select->name("tech_notes");
$notes_select->add_option("Need to order a part - not available on site", "Need to order a part - not available on site");
$notes_select->add_option("Need external assistance - unsure of problem", "Need external assistance - unsure of problem");
$notes_select->add_option("Requires external contractor / specialist", "Requires external contractor / specialist");
$notes_select->add_option("Calibration needed by external service provider", "Calibration needed by external service provider");
$notes_select->add_option("Equipment is beyond repair", "Equipment is beyond repair");
$notes_select->add_option("Can only be attended when production is stopped", "Can only be attended when production is stopped");
$notes_select->add_option("Other", "Other");

// Picture for technician notes
$note_pic_label = new label();
$note_pic_label->for("note_picture");
$note_pic_label->value("Add picture to support technician notes (Compulsory)");

$note_pic_upload = new file_upload();
$note_pic_upload->id("note_picture");
$note_pic_upload->name("note_picture");

// Actioned By
$action_label = new label();
$action_label->for("actioned_by");
$action_label->value("Actioned By (Technician signature)");

$technician_siganture = new signature();
$technician_siganture->id("technician_signature");
$technician_siganture->name("technician_signature");
$technician_siganture->send_file_path("../maintenance/upload_signature.php");
$technician_siganture->submit_btn_id("submit_btn");

$submit_btn = new input();
$submit_btn->class("submit_btn");
$submit_btn->value("Submit");
$submit_btn->onclick("submit_form();");
$submit_btn->id("submit_btn");

?>
<form action="maintenance_jobcards.php">
    <div class="form_down">
        <h1>Maintenance Job Card - Work Done</h1>
        <?php

        // $time_label->add();
        // $time_select->add();
        $work_label->add();
        $work_input->add();
        $pic_label->add();
        // $pic_upload->add();
        ?>

        <div id="" class="column segment" style=""></div>
        <div id="images" class="column " style=""></div>
        <br><br>
        <div id="resourcess" class="column " style="">
            <input type="button" onclick="add_row_image_details()" class="submit_btn" style="font-size:1em;"
                value="ADD IMAGE">
        </div>

        <?php
        $parts_label->add();
        $parts_input->add();
        $chem_label->add();
        $chem_select->add();
        $food_label->add();
        $food_select->add();
        $done_label->add();
        $done_select->add();
        $notes_label->add();
        $notes_select->add();
        $note_pic_label->add();
        // $note_pic_upload->add();
        // $note_picture->add();
        
        ?>
        <div id="" class="column segment" style=""></div>
        <div id="images" class="column " style=""></div>
        <br><br>
        <div id="resourcess" class="column " style="">
            <input type="button" onclick="add_row_image_details()" class="submit_btn" style="font-size:1em;"
                value="ADD IMAGE">
        </div>

        <script>

            function open_file($file_path) {
                var win = window.open($file_path, '_blank');
                win.focus();
            }

            index_file = 1;

            function add_row_image_details() {
                var div = document.createElement('div');
                div.innerHTML = '<div class="row"><div class="column"><h2>ADD IMAGE</h2><input type="file" class="inputs" name="file_' + index_file + '" id="file_' + index_file + '" onchange="upload_image(this,' + index_file + ',\'file\')" /><div id="progress_' + index_file + '"></div><div id="image_' + index_file + '"></div></div></div>';
                document.getElementById("images").appendChild(div);
                index_file++;
            }

            function upload_image(input, index, name) {
                var xhr = new XMLHttpRequest();
                xhr.open('POST', 'file_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("progress_" + index).innerHTML = percentage + "%";
                    }
                });
                xhr.onload = function () {
                    if (xhr.status == 200) {
                        var img = document.createElement("img");
                        img.src = xhr.responseText;
                        console.log(xhr.responseText);
                        document.getElementById("image_" + index).innerHTML = "<img src='" + xhr.responseText + "' style='width: 100%;' />";
                    }
                };
                xhr.send(new FormData(input.form));
            }
        </script>

        <?php
        $action_label->add();
        $technician_siganture->add();

        // $action_input->add();
        
        $submit_btn->add();
        ?>

    </div>

</form>

<?php

