<?php

include "classes/html.class.php";

$html = new html("CURRENT TASK");


$db = new db();


$tasks = $db->exec_query('tasks', ['*'], '', '', '', '', "record_id = {$_GET['task_id']}", '', false);
$row = $tasks->fetch_assoc();

if ($row['completed'] == "YES") {
    $completed = "disabled";
    $new_comment_hide = "display:none;";
} else {
    $new_comment_hide = "";
    $completed = "";
}
$task_name_id = $row['farming_action_id'];
$task_type_id = $row['farming_type_id'];
$farm_id_num = $row['farm_id'];


$task_name_find = $db->exec_query('farming_action', ['*'], '', '', '', '', "record_id='$task_name_id'", '', false);
$task_type_find = $db->exec_query('farming_types', ['*'], '', '', '', '', "record_id='$task_type_id'", '', false);
$farm_find = $db->exec_query('farms', ['*'], '', '', '', '', "record_id='$farm_id_num'", '', false);

$task_name = $task_name_find->fetch_assoc();
$task_type = $task_type_find->fetch_assoc();
$farm = $farm_find->fetch_assoc();

?>

<div class="form">
    <br>
    <label style="font-size:1.5vw">Created On:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $row['date_created'] ?>" readonly>
    <br>
    <label style="font-size:1.5vw">Action Date:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $row['action_date'] ?>" readonly>
    <br>
    <label style="font-size:1.5vw">Task Type:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $task_type['name'] ?>" readonly>
    <br>
    <label style="font-size:1.5vw">Task Name:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $task_name['name'] ?>" readonly>
    <br>
    <label style="font-size:1.5vw">Farm:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $farm['farm_name'] ?>" readonly>
    <br>
    <label style="font-size:1.5vw">Clusers:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $row['cluster_ids'] ?>" readonly>
    <br>
    <br>
    <hr>
    <label style="font-size:1.5vw">Instructions:</label>
    <br>
    <textarea class="text_input" style="width:90%; font-size:1vw"><?php echo $row['instruction'] ?></textarea>
    <br>
    <br>
    <img src="/taskimages/<?php echo $row['image'] ?>" style="width:90%;<?php echo $new_comment_hide ?>">
    <hr>
    <br style="<?php echo $new_comment_hide ?>">
    <label style="font-size:1.5vw; <?php echo $new_comment_hide ?>">New Comment:</label>
    <br style="<?php echo $new_comment_hide ?>">
    <br style="<?php echo $new_comment_hide ?>">
    <textarea id="new_comment" class="text_input"
        style="width:90%;height:20vw;<?php echo $new_comment_hide ?>"> </textarea>
    <br style="<?php echo $new_comment_hide ?>">
    <br style="<?php echo $new_comment_hide ?>">
    <button style="<?php echo $new_comment_hide ?>" class="form_btn" <?php echo $completed ?>
        onclick="save_new_comment()">SAVE</button>
    <br style="<?php echo $new_comment_hide ?>">
    <br style="<?php echo $new_comment_hide ?>">
    <br style="<?php echo $new_comment_hide ?>">
    <hr>
    <button class="form_btn" style="transform:scale(1);font-size: 1.5vw;<?php echo $new_comment_hide ?>"
        onclick="complete_task()" <?php echo $completed ?>>COMPLETE
        TASK</button>
    <hr>
    <label style="font-size:1.5vw">Old Comments:</label>
    <br>
    <?php
    $old_comments_query = $db->exec_query('comment_logs', ['*'], '', '', '', '', "task_id={$_GET['task_id']}", 'ORDER BY record_id DESC', false);
    if ($old_comments_query->num_rows >= 1) {
        while ($task_comments = $old_comments_query->fetch_assoc()) {
            if($task_comments['user_id'] == 0){
                $username = "SYSTEM [Data before users was added to comments]";
            }else{
                $user_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$task_comments['user_id']}", '', false);
                $user = $user_res->fetch_assoc();
                $username = $user['username'];
            }
            echo "<br><textarea class='text_input' style='width:90%;height:fit-content;' readonly>" . $task_comments['date_time'] . " : " . $task_comments['comments'] . " -> " . $username . "</textarea><br>";
        }
    } else {
        echo "<br><textarea class='text_input' style='width:90%;' value='NO PREVIOUS COMMENTS' readonly></textarea>";
    }

    ?>
    <br>
    <br>
</div>

<br>
<br>
<br>

<script>
    function save_new_comment() {
        new_comment_values = document.getElementById("new_comment").value;
        if (new_comment_values == "" || new_comment_values == " ") {
            alert("EMPTY COMMENTS");
        } else {
            // Create an XMLHttpRequest object
            const xhttp = new XMLHttpRequest();
            // Define a callback function
            xhttp.onload = function () {
                // alert(this.responseText);
                // Here you can use the Data
                if (this.responseText != 1) {
                    alert(this.responseText);
                } else {
                    alert('COMMENT ADDED');
                    location.reload();
                }
            }
            xhttp.open("GET", "ajax/save_comment.ajax.php?task_id=" + <?php echo $_GET['task_id'] ?> + "&new_comment=" + new_comment_values);
            xhttp.send();
        }
    }

    function complete_task() {
        new_comment_values = document.getElementById("new_comment").value;
        console.log("before");
        if (new_comment_values == "" || new_comment_values == " ") {

        } else {
            // Create an XMLHttpRequest object
            const xhttp = new XMLHttpRequest();
            // Define a callback function
            xhttp.onload = function () {
                // alert(this.responseText);
                // Here you can use the Data
                if (this.responseText == 0) {
                    alert(this.responseText);
                    window.location.href = "manage_errands.php";
                    // location.reload();

                } else {

                }
            }
            xhttp.open("GET", "ajax/save_comment.ajax.php?task_id=" + <?php echo $_GET['task_id'] ?> + "&new_comment=" + new_comment_values);
            xhttp.send();
        }


        // Create an XMLHttpRequest object
        const xhttp1 = new XMLHttpRequest();
        // Define a callback function
        xhttp1.onload = function () {

            // Here you can use the Data
            if (this.responseText != 1) {
                alert(this.responseText);
                location.reload();
            } else {
            }
        }
        xhttp1.open("GET", "ajax/complete_task.ajax.php?task_id=" + <?php echo $_GET['task_id'] ?>);
        xhttp1.send();

        // Create an XMLHttpRequest object
        const xhttp2 = new XMLHttpRequest();
        // Define a callback function
        xhttp2.onload = function () {
            // Here you can use the Data
            if (this.responseText != 1) {
                alert(this.responseText);
                location.reload();
            } else {
                alert('MARKED AS COMPLETED');
                window.location.href = "manage_errands.php";
            }
        }
        xhttp2.open("GET", "ajax/save_comment.ajax.php?task_id=" + <?php echo $_GET['task_id'] ?> + "&new_comment=" + "Marked Completed");
        xhttp2.send();
        location.reload();
    }

</script>