<?
session_start();

include "../classes/db.class.php";
include "../classes/telegram.class.php";

$db = new db();
$telegram = new telegram();
$farming_action = $_POST['farming_action'];
$farming_type = $_POST['farming_type'];
$farm_id = $_POST['farm_id'];
$action_date = $_POST['action_date'];
$cluster = strtoupper($_POST['cluster']);
$rating = $_POST['rating'];
$instructions = strtoupper($_POST['instruction']);
$user_id = strtoupper($_POST['username']);
$completed = '';
$comments = '';
$date_created = date('Y-m-d H:i', strtotime("+ 2 Hours"));
$date_completed = '';

//split the cluster into an array then check the record id that matches that value

// function cluster_id($cluster) {
//     $db = new db();
//     $cluster_id = $db->query("SELECT cluster FROM clusters WHERE cluster = '$cluster'");
//     return $cluster_id;
// }

$res = $db->insert("INSERT INTO tasks (`farming_action_id`, `farming_type_id`, `cluster_ids`, `rating`, `instruction`, `user_id`, `completed`, `comments`, `date_created`, `date_completed`,`farm_id`,`user_id_created`,`action_date`, `image`) VALUES ('$farming_action','$farming_type','$cluster','$rating','$instructions','$user_id','$completed','$comments','$date_created','$date_completed','$farm_id','{$_SESSION['user_id']}','$action_date','')");

if ($res) {

    if (!empty($_FILES['image']['tmp_name'])) {
        $target_dir = "../taskimages/";
        $target_file = $target_dir . basename($_FILES["image"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if ($check !== false) {
            $uploadOk = 1;
        } else {
            $uploadOk = 0;
        }
        if ($uploadOk == 0) {
            echo 0;
        } else {
            if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
                $db->update("tasks", ['image'], [basename($_FILES["image"]["name"])], "record_id = $res");
                echo 1;
            } else {
                echo 0;
            }
        }
    } else {
        echo 1;
    }


    // check if assinged user has telegram chat id:
    $tel_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$user_id} AND telegram_chat_id != ''");
    if ($tel_res->num_rows > 0) {
        $tel = $tel_res->fetch_assoc();

        // get farm name 
        if ($farm_id = 0) {
            $farm_name = 'ALL';
        } else {
            $farm_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$farm_id}");
            $farm = $farm_res->fetch_assoc();
            $farm_name = $farm['farm_name'];
        }
        // get task name
        $task_res = $db->exec_query('farming_action', ['*'], '', '', '', '', "record_id = {$farming_action}");
        $task = $task_res->fetch_assoc();
        $task_name = $task['name'];
        //get task type
        $task_type_res = $db->exec_query('farming_types', ['*'], '', '', '', '', "record_id = {$farming_type}");
        $task_type = $task_type_res->fetch_assoc();
        $task_type = $task_type['name'];

        $telegram->send(
            "NEW TASK 
        \n--------------------
        \n Created by: {$_SESSION['username']}
        \n Date Created: $date_created 
        \n Action Date: $action_date 
        \n--------------------
        \n Farm: $farm_name 
        \n Cluster: $cluster
        \n Rating: $rating 
        \n Farming Type: $task_name 
        \n Farming Action: $task_type 
        \n--------------------
        \n Instructions: 
        \n $instructions"
            ,
            $tel['telegram_chat_id']
        );
    }

} else {
    echo "INSERT INTO tasks (`farming_action_id`, `farming_type_id`, `cluster_ids`, `rating`, `instruction`, `user_id`, `completed`, `comments`, `date_created`, `date_completed`,`farm_id`,`user_id_created`,`action_date`, `image`) VALUES ('$farming_action','$farming_type','$cluster','$rating','$instructions','$user_id','$completed','$comments','$date_created','$date_completed','$farm_id','{$_SESSION['user_id']}','$action_date','')";
}

