<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();

$res = $db->select_query("work_orders", "*", "record_id='" . $_GET['record_id'] . "'");
$work_order_data = $res->fetch_assoc();
$type_res = $db->select_query("work_order_types", "*", "record_id = {$work_order_data['work_order_type_id']}");
$type = $type_res->fetch_assoc();

$work_order_res = $db->select_query("work_order_timeline", "*", "work_order_id = {$_GET['record_id']} ORDER BY record_id DESC LIMIT 1");
$work_order = $work_order_res->fetch_assoc();

$status = new select();
$status->name("status");
$status->id("status");
$status->add_option("", "");
$status->add_option("reopen", "RE OPEN WORK ORDER");
$status->add_option("close", "CLOSE WORK ORDER");
$status->required();
?>
<form method="post" action="update_work_order.php">

    <div class="form_down">
        <h1>REVIEW WORK ORDER</h1>
        <div style="display: flex; flex-direction: row; justify-content: space-around;align-items: baseline;"></div>
        <hr>
        <?php echo $html->select_html_from_db("projects", "record_id", "name", "date_time_finished = '--'", "project_id", "project_id", "select", "", "", "required"); ?>
        <?php echo $html->select_from_db_add_current_selected_column('work_orders', 'project_id', "record_id = {$_GET['record_id']}", "project_id"); ?>
        <?php echo $html->select_html_from_db("work_order_types", "record_id", "name", "", "work_order_type_id", "work_order_type_id", "select", "", "", "required"); ?>
        <?php echo $html->select_from_db_add_current_selected_column('work_orders', 'work_order_type_id', "record_id = {$_GET['record_id']}", "work_order_type_id"); ?>
        <?php echo $html->input_html_from_db('work_orders', 'name', " record_id = {$_GET['record_id']}", "", "name", "name", "inputs", "", "", "required"); ?>
        <?php echo $html->input_html_from_db('work_orders', 'record_id', " record_id = {$_GET['record_id']}", "", "record_id", "record_id", "inputs", "", "", "hidden"); ?>
        <?php echo $html->input_html("", $_SESSION['user_id'], "user_id", "user_id", "inputs", "", "", 'text', "hidden"); ?>
        <?php echo $html->select_html_from_db("users", "record_id", "username", "", "user_id_assigned", "user_id_assigned"); ?>
        <h1>REVIEW USER</h1>
        <?php echo $html->select_html_from_db("users", "record_id", "username", "", "user_id_review", "user_id_review"); ?>
        <?php echo $html->select_from_db_add_current_selected_column('work_orders', 'user_id_review', "record_id = {$_GET['record_id']}", "user_id_review"); ?>

        <?php echo $html->date_input_html_from_db('work_orders', 'deadline_date_time', " record_id = {$_GET['record_id']}", "deadline_date_time", "deadline_date_time"); ?>
        <?php echo $html->date_input_html_from_db('work_orders', 'action_date_time', " record_id = {$_GET['record_id']}", "action_date_time", "action_date_time"); ?>
        <?php echo $html->select_from_db_add_current_selected_column('work_orders', 'user_id_assigned', "record_id = {$_GET['record_id']}", "user_id_assigned"); ?>
        <?php echo $html->date_input_html("date_time_created", "date_time_created", "inputs", "", date('Y-m-d H:i', strtotime("+2 Hours")), "", "readonly"); ?>

        <h1> RESOURCES</h1>
        <div id="resources" class="column segment" style="">
            <input type="hidden" value="<?php echo $_GET['record_id']; ?>" name="work_order_id">
            <?php
            $db = new db_safeguard();
            $resources_res = $db->query("work_order_resources", "SELECT * FROM work_order_resources WHERE work_order_id = {$_GET['record_id']}");
            $index_res = 1;
            $call = new call();
            while ($resources = $resources_res->fetch_assoc()) {
                ?>
                <br>
                <h2>Resources: <?php echo $index_res; ?></h2>
                <div class="row">
                    <input type=text class="inputs" style="width: 20%;"
                        value="<?php echo $call->get_from_users($resources['user_id'], 'username'); ?>" readonly>
                    <input type='text' class="inputs" style="width: 40%;" value="<?php echo $resources['date_time']; ?>"
                        readonly>
                    <?php if ($resources['link'] != "") { ?><input type="button" class="submit_btn"
                            onclick="open_file('<?php echo 'resources/' . $resources['link']; ?>')"
                            value="<?php echo $resources['name']; ?>">
                    <?php } ?>

                </div>
                <?php
                $index_res++;
            } ?>

        </div>
        <br><br>
        <div id="resourcess" class="column " style="">

            <input type="button" onclick="add_row_resources_details()" class="submit_btn" style="font-size:1em;"
                value="ADD RESOURCE">
        </div>
        <br><br>
        <script>
            function open_file($file_path) {
                var win = window.open($file_path, '_blank');
                win.focus();
            }

            index_file = <?php echo $index_res; ?>;

            function add_row_resources_details() {
                var div = document.createElement('div');
                div.className = "row";
                div.innerHTML = '<div class="column"><h4>Resource:  ' + index_file + '</h4><input type="text" class="inputs" style="width: 100%;" name="resource_' + index_file + '" id="resource_' + index_file + '"></div>';
                document.getElementById("resources").appendChild(div);
                var div = document.createElement('div');
                div.className = "row";
                div.innerHTML = '<h2>CLICK TO ADD RESOURCE</h2><div class="row"><div class="column"><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("resources").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>
        <h1> TO DO </h1>

        <div id="instructions" class="column segment" style="">
            <?php
            $db = new db_safeguard();
            $instructions_res = $db->query("work_orders_instructions", "SELECT * FROM work_orders_instructions WHERE work_order_id = {$_GET['record_id']}");
            $index = 1;
            $call = new call();

            while ($instruction = $instructions_res->fetch_assoc()) {
                ?>

                <div class="row">
                    <input type=text class="inputs" style="width: 20%;"
                        value="<?php echo $call->get_from_users($instruction['user_id'], 'username'); ?>" readonly>
                    <input type=text class="inputs" style="width: 40%;"
                        value="<?php echo $instruction['date_time_created']; ?>" readonly>
                </div>
                <div class="row">
                    <textarea class="inputs" style="width: 100%;"><?php echo $instruction['instruction']; ?></textarea>
                </div>
                <?php
                $index++;
            } ?>
        </div>
        <br><br>
        <input type="button" onclick="add_row_instruction_details()" class="submit_btn" style="font-size:1em;"
            value="ADD INSTRUCTION">
        <script>
            index_notes = <?php echo $index; ?>;
            function add_row_instruction_details() {
                var div = document.createElement('div');
                div.className = "row";
                div.innerHTML = '<div class="column"><textarea class="inputs" style="width: 100%;" name="instruction_' + index_notes + '" id="instruction_' + index_notes + '"></textarea></div>';
                document.getElementById("instructions").appendChild(div);
                index_notes++;
            }

        </script>
        <br><br>

        <div id="logs" class="column segment" style="">

            <input type="hidden" value="<?php echo $_GET['record_id']; ?>" name="work_order_id">
            <h1> COMMENTS & LOGS</h1>
            <?php
            $db = new db_safeguard();
            $log_res = $db->query("work_order_logs", "SELECT * FROM work_order_logs WHERE work_order_id = {$_GET['record_id']}");
            $index = 1;
            $call = new call();

            while ($log = $log_res->fetch_assoc()) {
                ?>
                <br>
                <div class="row">
                    <input type=text class="inputs" style="width: 20%;"
                        value="<?php echo $call->get_from_users($log['user_id'], 'username'); ?>" readonly>
                    <input type=text class="inputs" style="width: 40%;" value="<?php echo $log['date_time']; ?>" readonly>
                </div>
                <div class="row">
                    <div class="column">

                        <textarea class="inputs" style="width: 95%;"><?php echo $log['log']; ?></textarea>

                    </div>
                </div>
                <?php
                $index++;
            } ?>

        </div>
        <br><br>

        <div id="logs" class="column" style="">
            <input type="button" onclick="add_row_log_details()" class="submit_btn" style="font-size:1em;"
                value="ADD LOG">
        </div>
        <script>
            index_notes = <?php echo $index; ?>;
            function add_row_log_details() {
                var div = document.createElement('div');
                div.className = "row";
                div.innerHTML = '<div class="column"><h4>Log:  ' + index_notes + '</h4><textarea class="inputs" style="width: 100%;" name="log_' + index_notes + '" id="log_' + index_notes + '"></textarea></div>';
                document.getElementById("logs").appendChild(div);
                index_notes++;
            }

        </script>
        <br><br>


        <?php
        echo "<h1>STATUS</h1>";
        $status->add();
        echo $html->submit_btn("submit", "UPDATE", "");
        ?>
    </div>
</form>