<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();
$where_data = ' 1 ';

$record_id = $_GET['task_record_id'];

$task_info_res = $db->exec_query('tasks', ['*'], '', '', '', '', "record_id = $record_id");
$task_info = $task_info_res->fetch_assoc();

if ($task_info['farm_id'] == 0) {
    $farm = 'ALL';
} else {
    $farm_res = $db->exec_query("farms", ['*'], '', '', '', '', "record_id = {$task_info['farm_id']}");
    $farm_info = $farm_res->fetch_assoc();
    $farm = $farm_info["farm_name"];
}

function getTimeDifference($date1, $date2)
{
    $datetime1 = new DateTime($date1);
    $datetime2 = new DateTime($date2);
    $interval = $datetime1->diff($datetime2);
    return $interval->format('%a days %h hours %i minutes');
}



$task_name_res = $db->exec_query("farming_action", ['*'], '', '', '', '', "record_id = {$task_info['farming_action_id']}");
$task_name_info = $task_name_res->fetch_assoc();

$task_type_res = $db->exec_query("farming_types", ['*'], '', '', '', '', "record_id = {$task_info['farming_type_id']}");
$task_type_info = $task_type_res->fetch_assoc();

$user_id_res = $db->exec_query("users", ['*'], '', '', '', '', "record_id = {$task_info['user_id']}");
$user_id_info = $user_id_res->fetch_assoc();

$user_id_res2 = $db->exec_query("users", ['*'], '', '', '', '', "record_id = {$task_info['user_id_created']}");
$user_created = $user_id_res2->fetch_assoc();

$old_comments_query = $db->exec_query('comment_logs', ['*'], '', '', '', '', "task_id = {$task_info['record_id']}");

$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('p');

$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(200, 15, "TASK FORM ", 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

if (strlen($task_info['image']) > 1) {
    $file = "taskimages/" . $task_info['image'];
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    $pdf->Cell(200, 10, '', 0, 1, 'C'); // Add some space before the image
    $pdf->Image($file, 110, 50, 90, 0, $ext); // Adjust path, x, y, and width as needed
    $pdf->Cell(200, 10, '', 0, 1, 'C'); // Add some space after the image
}
$pdf->Cell(50, 8, "CREATED BY:", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(50, 8, $user_created['username'], "", 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(50, 8, "FARM: ", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(100, 8, $farm, 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "DATE CREATED:", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(90, 8, $task_info['date_created'], "", 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "ACTION DATE:", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(90, 8, $task_info['action_date'], "", 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "DATE COMPLETED:", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(90, 8, $task_info['date_completed'], "", 1, 'L');
$pdf->SetFont('Arial', '', 10);

if (strlen($task_info['date_completed']) > 1) {

    $pdf->Cell(50, 8, "TOTAL TIME:", "R", 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(90, 8, getTimeDifference($task_info['date_created'], $task_info['date_completed']), "", 1, 'L');
    $pdf->SetFont('Arial', '', 10);
}
$pdf->Cell(50, 8, 'BLOCKS:', "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(100, 8, $task_info['cluster_ids'], "", 1, 'l');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "RATING:", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(100, 8, $task_info['rating'], "", 1, 'l');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "SECTION MANAGER:", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(100, 8, $user_id_info['username'], "", 1, 'l');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "FARMING TYPE: ", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(100, 8, $task_type_info['name'], "", 1, 'l');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "FARMING ACTION: ", "R", 0, 'L');
$pdf->SetFont('Arial', 'B', 10);

$pdf->Cell(100, 8, $task_name_info['name'], "", 1, 'l');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(200, 20, "INSTRUCTIONS:", '', 1, 'C');
$pdf->SetFont('Arial', '', 10);

$pdf->MultiCell(190, 10, $task_info['instruction'], 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(200, 12, "COMMENTS:", '', 1, 'C');
$pdf->SetFont('Arial', '', 10);

if ($old_comments_query->num_rows >= 1) {
    while ($task_comments = $old_comments_query->fetch_assoc()) {
        if ($task_comments['user_id'] == 0) {
            $username = "SYSTEM [data from before user was added to comments]";
        } else {
            $user_id_res = $db->exec_query("users", ['*'], '', '', '', '', "record_id = {$task_comments['user_id']}");
            $user_id_info = $user_id_res->fetch_assoc();
            $username = $user_id_info['username'];
        }

        $pdf->MultiCell(190, 10, $task_comments['date_time'] . " : " . $task_comments['comments'] . " -> " . $username, 1, 'L');
        $pdf->Cell(200, 5, "", '', 1, 'C');
    }
} else {
    $pdf->MultiCell(190, 10, 'NO COMMENTS', 1, 'L');
}




$pdf->Output("I");
