<?php

include "classes/html.class.php";

$html = new html("CURRENT ERRAND");

$db = new db();

$errand = $db->exec_query('management', ['*'], '', '', '', '', "record_id = {$_GET['errand_id']}", '', false);
$row = $errand->fetch_assoc();

if ($row['completed'] == "YES") {
    $completed = "disabled";
    $new_comment_hide = "display:none;";
} else {
    $new_comment_hide = "";
    $completed = "";
}

$farm_name_id = $row['farming_action_id'];
$farm_type_id = $row['farming_type_id'];
$farm_id_num = $row['farm_id'];

$farm_name_find = $db->exec_query('farming_action', ['*'], '', '', '', '', "record_id='$farm_name_id'", '', false);
$farm_type_find = $db->exec_query('farming_types', ['*'], '', '', '', '', "record_id='$farm_type_id'", '', false);
$farm_find = $db->exec_query('farms', ['*'], '', '', '', '', "record_id='$farm_id_num'", '', false);

$farm_name = $farm_name_find->fetch_assoc();
$farm_type = $farm_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">Start Date:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $row['action_date'] ?>" readonly>
    <br>
    <label style="font-size:1.5vw">Errand Type:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $farm_type['name'] ?>" readonly>
    <br>
    <label style="font-size:1.5vw">Task Name:</label>
    <br>
    <input class="form_input" style="width:75%;" value="<?php echo $farm_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['instructions'] ?></textarea>
    <br>
    <br>
    <br>

    <hr>
    <?php

    $image_res = $db->exec_query('management', ["*"], '', '', '', '', "record_id = '{$_GET['errand_id']}'", '', false);
    while ($image = $image_res->fetch_assoc()) {
        $imgArray = explode(',', $image['image']);

        foreach ($imgArray as $imgFile) {
            if ($imgFile == '') {
                continue;
            } else {
                echo "<img src='/taskimages/$imgFile' id='uploadedImage' style='width:60%;'><br><br>";
            }
        }
    }
    ?>

    <input type="text" id="errand_id" value="<?php echo $_GET['errand_id'] ?>" hidden>

    <hr>
    <br style="<?php echo $new_comment_hide ?>">
    <label style="font-size:1.5vw; <?php echo $new_comment_hide ?>">New Comment:</label>

    <textarea id="new_comment" class="text_input" errand_id
        style="width:90%;height:20vw;<?php echo $new_comment_hide ?>"> </textarea>

    <br><br>
    <button class="form_btn" onclick="save_new_comment()">SAVE</button>

    <hr>
    <button class="form_btn" style="transform:scale(1);font-size: 1.5vw;<?php echo $new_comment_hide ?>"
        onclick="complete_errand()" <?php echo $completed ?>>COMPLETE
        ERREND</button>
    <hr>
    <label style="font-size:1.5vw">Old Comments:</label>
    <br>
    <?php
    $old_comments_query = $db->exec_query('coms', ['*'], '', '', '', '', "management_id = {$_GET['errand_id']}", 'ORDER BY record_id DESC', false);
    if ($old_comments_query->num_rows >= 1) {
        while ($errand_comments = $old_comments_query->fetch_assoc()) {
            $user_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$errand_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>" . $errand_comments['date_time'] . " : " . $errand_comments['message'] . " -> " . $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 = document.getElementById("new_comment").value;
        errand_id = document.getElementById("errand_id").value;
        if (new_comment == "" || new_comment == " ") {
            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/add_coms.ajax.php?errand_id=" + errand_id + "&new_comment=" + new_comment);
            xhttp.send();
        }
    }

    function complete_errand() {
        new_comment = document.getElementById("new_comment").value;
        errand_id = document.getElementById("errand_id").value;

        console.log("before");
        if (new_comment == "" || new_comment == " ") {

        } 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);
                    // location.reload();

                } else {

                }
            }
            xhttp.open("GET", "ajax/add_coms.ajax.php?errand_id=" + errand_id + "&new_comment=" + new_comment);

            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 {
                alert('MARKED AS COMPLETED');
            }
        }
        xhttp1.open("GET", "ajax/complete_errand.ajax.php?errand_id=" + errand_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('COMMENT ADDED');
                window.location.href = "manage_errands.php";
                // location.reload();
            }
        }
        xhttp2.open("GET", "ajax/add_coms.ajax.php?errand_id=" + errand_id + "&new_comment=" + "Marked Completed");
        xhttp2.send();
    }
</script>