<?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();

// get project details
$project_res = $db->select_query("projects", "*", "record_id = {$work_order_data['project_id']}");
$project = $project_res->fetch_assoc();

$html->open_new_window("open_system_link", "https://{$project['system_links']}", "SYSTEM");
$html->open_new_window("go_to_database", "/app/databases/db.php?record_id={$project['database_id']}", "DB VIEWER");
?>

<form id='resources_form' style="padding:0px" name='log_form' action="add_to_work_order.php" method="post">
    <div class="row" style="width:95%">
        <div id="Actions" class="column"
            style="margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">
            <h2>Actions</h2>
            <?php
            if ($work_order_data['in_review'] == 1 || $work_order_data['complete'] == 1 || $work_order_data['sent_for_review'] == 1) {
            } else {
                if ($work_order['status'] == 'start') {
                    echo $html->submit_btn("submit", "STOP", "mark_as_not_inprogress();stop();");
                } elseif ($work_order['status'] == 'stop') {
                    echo $html->submit_btn("submit", "START", "mark_as_inprogress();start();");
                    echo $html->submit_btn("submit", "SEND FOR REVIEW", 'mark_for_review();send_for_review();');
                } elseif ($work_order['status'] == 'complete') {
                } else {
                    echo $html->submit_btn("submit", "START", "mark_as_inprogress();start()");
                }

            }
            ?>
        </div>
        <div class="row"
            style="margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">
            <div class="column" style="width: auto;">
                <label>Work Order</label>
                <?php echo $html->input_html("NAME", $work_order_data['name'], "", "", "inputs", "", "", 'text', 'readonly'); ?>
            </div>
            <div class="column" style="width: auto;">
                <label>Deadline</label>
                <?php echo $html->date_input_html("", "", 'inputs', "", $work_order_data['deadline_date_time'], "", 'readonly'); ?>
            </div>
            <div class="column" style="width: auto;">
                <label>Type</label>
                <?php echo $html->input_html("", $type['name'], '', '', 'inputs', 'width:auto;', '', '', 'readonly'); ?>
            </div>
        </div>
        <br>
        <div class="row">
            <div id="resources" class="column"
                style="margin-left:-1%;margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">
                <input type="hidden" value="<?php echo $_GET['record_id']; ?>" name="work_order_id">
                <h1> Resources</h1>
                <?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>

            <div id="resourcess" class="column"
                style="margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">
                <input type="button" onclick="add_row_resources_details()" class="submit_btn" style="font-size:1em;"
                    value="ADD RESOURCE">
            </div>
            <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>
            <br><br>
            <div id="instructions" class="column"
                style="margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">
                <h1> TO DO </h1>
                <?php
                $db = new db_safeguard();
                $instructions_res = $db->query("work_orders_instructions", "SELECT * FROM work_orders_instructions WHERE work_order_id = {$_GET['record_id']}");
                $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%;height: auto;min-height: 150px;"><?php echo $instruction['instruction']; ?></textarea>
                    </div>
                    <?php
                } ?>
            </div>

        </div>
        <div id="logs" class="column"
            style="margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">

            <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: 100%;height: auto;min-height: 150px;"><?php echo $log['log']; ?></textarea>

                    </div>
                </div>
                <?php
                $index++;
            } ?>

        </div>

        <div id="logs" class="column"
            style="margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">

            <input type="button" onclick="add_row_log_details()" class="submit_btn" style="font-size:1em;"
                value="ADD LOG">
            <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>
        </div>
        <div id="Actions" class="column"
            style="margin-top:2vh;border: 1px solid black;padding: 2vw;border-radius:2vw;background-color: #e3e3e3;">
            <?php echo $html->submit_btn("submit", "SAVE", ""); ?>
        </div>

    </div>

</form>



<script>
    function open_link(link) {
        window.open(link, '_blank');
    }
</script>
<?php

$date = date("Y-m-d H:i", strtotime("+2 Hours"));

$html->html_form_ajax("add_work_order", "UPDATE", "work_orders", '', false, [], '', [], 'date_time,user_id');

$html->html_form_ajax("mark_for_review", "UPDATE", "work_orders", '', false, [], '', [], 'date_time,user_id', "record_id={$_GET['record_id']},sent_for_review=1");

$html->html_form_ajax("mark_as_inprogress", "UPDATE", "work_orders", '', false, [], '', [], 'date_time,user_id', "record_id={$_GET['record_id']},inprogress=1");

$html->html_form_ajax("mark_as_not_inprogress", "UPDATE", "work_orders", '', false, [], '', [], '', "record_id={$_GET['record_id']},inprogress=0");

$html->html_form_ajax("start", "custom_insert", "work_order_timeline", '', false, [], '', [], "record_id", "status=start,work_order_id={$_GET['record_id']},user_id={$_SESSION['user_id']},date_time=$date");

$html->html_form_ajax("stop", "custom_insert", "work_order_timeline", '', false, [], '', [], "record_id", "status=stop,work_order_id={$_GET['record_id']},user_id={$_SESSION['user_id']},date_time=$date");

$html->html_form_ajax("send_for_review", "custom_insert", "work_order_timeline", '', false, [], '', [], "record_id", "status=sent_for_review,work_order_id={$_GET['record_id']},user_id={$_SESSION['user_id']},date_time=$date");
?>