<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();


$pdf = new FPDF();
$pdf->AliasNbPages();


function get_username($user_id)
{
    global $db;
    $data_res = $db->query("SELECT username FROM users WHERE record_id = $user_id");
    $data = $data_res->fetch_assoc();
    return $data['username'];
}
$where_data = $_POST['where_data'];
if ($_GET['errand_record_id'] > 0) {

    $pdf->AddPage('P');

    $errand_res = $db->exec_query('management', ['*'], '', '', '', '', "record_id = {$_GET['errand_record_id']} ");
    $errand = $errand_res->fetch_assoc();

    $user_type_res = $db->exec_query('users', ["*"], '', '', '', '', "record_id = '{$errand['user_id_assigned']}'", "", false);
    $user = $user_type_res->fetch_assoc();

    $user_type_res = $db->exec_query('users', ["*"], '', '', '', '', "record_id = '{$errand['user_id_created']}'", "", false);
    $user_1 = $user_type_res->fetch_assoc();

    $farm_res = $db->exec_query('farms', ["*"], '', '', '', '', "record_id = '{$errand['farm_id']}'", "", false);
    $farm = $farm_res->fetch_assoc();

    $farming_type_res = $db->exec_query('farming_types', ["*"], '', '', '', '', "record_id = '{$errand['farming_type_id']}'", "", false);
    $farming_type = $farming_type_res->fetch_assoc();

    $farming_action_res = $db->exec_query('farming_action', ["*"], '', '', '', '', "record_id = '{$errand['farming_action_id']}'", "", false);
    $farming_action = $farming_action_res->fetch_assoc();

    $pdf->SetFont('Arial', 'B', 20);
    $pdf->Cell(105, 15, "ERRAND REPORT FOR {$user['username']} ", 0, 1, 'C');
    $pdf->Cell(10, 2, '', 0, 1, 'L');

    $pdf->SetFont('Arial', 'B', 12);
    $pdf->Cell(40, 15, "CREATED BY {$user_1['username']} ", 0, 1, 'C');
    $pdf->Cell(10, 5, '', 0, 1, 'L');

    $pdf->SetFont('Arial', 'B', 12);
    $pdf->Cell(90, 15, "DATE CREATED", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(90, 15, $errand['date_created'], "", 1, 'l');

    $pdf->SetFont('Arial', 'B', 12);

    $pdf->Cell(90, 15, "FARMING TYPE", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $farming_type['name'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 12);

    $pdf->Cell(90, 15, "FARMING ACTION", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $farming_action['name'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 12);

    $pdf->Cell(90, 15, "FARM", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $farm['farm_name'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 12);

    $pdf->Cell(90, 15, "ACTION DATE", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $errand['action_date'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 12);

    $pdf->Cell(90, 15, "RATING", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $errand['rating'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 12);


    $pdf->Cell(90, 15, "COMPLETED", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    if ($errand['completed'] = ' ') {
        $pdf->Cell(90, 15, 'NO', "", 1, 'l');
        $pdf->SetFont('Arial', 'B', 12);

    } else {
        $pdf->Cell(90, 15, 'YES', "", 1, 'l');
        $pdf->SetFont('Arial', 'B', 12);

        $pdf->Cell(90, 15, "DATE COMPLETED", "R", 0, 'L');
        $pdf->SetFont('Arial', '', 10);

        $pdf->Cell(90, 15, $errand['date_completed'], "", 1, 'l');
        $pdf->SetFont('Arial', 'B', 12);

    }

    $pdf->Cell(200, 20, "INSTRUCTIONS:", '', 1, 'C');
    $pdf->SetFont('Arial', '', 10);

    $pdf->MultiCell(190, 10, $errand['instructions'], 1, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(200, 12, "COMMENTS:", '', 1, 'C');
    $pdf->SetFont('Arial', '', 10);

    $res = $db->exec_query('coms', ['*'], '', '', '', '', "`management_id` = {$_GET['errand_record_id']}");
    while ($comm = $res->fetch_assoc()) {
        $pdf->multiCell(90, 15, $comm['date_time'] . " : " . $comm['message']);
        $pdf->Cell(10, 15, 'Sent By: ' . get_username($comm['user_id']), 0, 1, 'L');

        $pdf->Cell(10, 15, '_________________________________________________', 0, 1, 'L');
    }

    $pdf->AddPage('P');

    $image_res = $db->exec_query('management', ["*"], '', '', '', '', "record_id = '{$errand['record_id']}'", '', false);
    while ($image = $image_res->fetch_assoc()) {
        $imgArray = explode(',', $image['image']);

        $image_array1 = [10, 75, 140];

        foreach ($imgArray as $imgFile) {
            if ($imgFile == '') {
                continue;
            } else {
                $image = "taskimages/$imgFile";
                $pdf->Image($image, $image_array1[0], 10, 60);

            }
        }
    }


    $pdf->Output("I");
} else {

    $pdf->AddPage('l');

    $pdf->SetFont('Arial', 'B', 20);
    $pdf->Cell(200, 15, "ERRAND REPORT ", 0, 1, 'C');
    $pdf->Cell(10, 5, '', 0, 1, 'L');
    $pdf->SetFont('Arial', '', 10);
    // farms data
    $farms_res = $db->query("SELECT DISTINCT(farm_id) FROM management WHERE $where_data");
    while ($farms_tasks = $farms_res->fetch_assoc()) {
        // get farm name
        $farm_name_res = $db->query("SELECT farm_name FROM farms WHERE record_id = {$farms_tasks['farm_id']}");
        $farm_name = $farm_name_res->fetch_assoc();

        $pdf->SetFont('Arial', 'B', 15);
        $pdf->Cell(200, 15, "{$farm_name['farm_name']} errend types summary", 0, 1, 'C');
        $pdf->SetFont('Arial', '', 10);


        $farming_types_table = [83, 38, 38, 95];
        $pdf->Cell($farming_types_table[0], 8, "TYPE", 1, 0, 'L');
        $pdf->Cell($farming_types_table[1], 8, "TOTAL ERRANDS", 1, 0, 'L');
        $pdf->Cell($farming_types_table[2], 8, "TOTAL COMPELTED", 1, 0, 'L');
        $pdf->Cell($farming_types_table[3], 8, "BLOCKS DONE", 'LTB', 1, 'L');
        // get distict farming_types in the tasks table from where data
        $farming_types_res = $db->query("SELECT DISTINCT(farming_type_id) FROM management WHERE $where_data AND farm_id = {$farms_tasks['farm_id']}");
        while ($farming_type = $farming_types_res->fetch_assoc()) {
            $farming_type_id = $farming_type['farming_type_id'];

            // get the farming type name
            $farming_type_name_res = $db->query("SELECT name FROM farming_types WHERE record_id = $farming_type_id");
            $farming_type_name = $farming_type_name_res->fetch_assoc();

            // get the count of the farming type
            $farming_type_count_res = $db->query("SELECT * FROM management WHERE $where_data AND farming_type_id = $farming_type_id AND farm_id = {$farms_tasks['farm_id']}");
            $farming_type_count = $farming_type_count_res->num_rows;

            // name
            $pdf->Cell($farming_types_table[0], 8, $farming_type_name['name'], 1, 0, 'L');

            // amount of tasks
            $pdf->Cell($farming_types_table[1], 8, $farming_type_count, 1, 0, 'L');

            // get the count of the farming types completed
            $farming_type_count_res = $db->query("SELECT * FROM management WHERE $where_data AND farming_type_id = $farming_type_id AND farm_id = {$farms_tasks['farm_id']} AND date_completed != ''");
            $farming_type_count = $farming_type_count_res->num_rows;
            $pdf->Cell($farming_types_table[1], 8, $farming_type_count, 1, 0, 'L');


            // get all blocks targeted
            $farming_type_blocks_res = $db->query("SELECT cluster_ids FROM management WHERE $where_data AND farming_type_id = $farming_type_id AND farm_id = {$farms_tasks['farm_id']}");
            $blocks = $blocks . "";
            while ($farming_type_block = $farming_type_blocks_res->fetch_assoc()) {
                $blocks = $blocks . $farming_type_block['cluster_ids'] . ",";
            }
            $pdf->Cell($farming_types_table[3], 8, $blocks, 'LTB', 1, 'L');
            $blocks = "";
        }
    }

    $pdf->AddPage('l');
    $pdf->SetFont('Arial', 'B', 20);
    $pdf->Cell(200, 15, "DETAILS ", 0, 1, 'C');
    $pdf->Cell(10, 5, '', 0, 1, 'L');
    $pdf->SetFont('Arial', '', 10);
    $farms_res = $db->query("SELECT DISTINCT(farm_id) FROM management WHERE $where_data");
    while ($farms_tasks = $farms_res->fetch_assoc()) {
        // get farm name
        $farm_name_res = $db->query("SELECT farm_name FROM farms WHERE record_id = {$farms_tasks['farm_id']}");
        $farm_name = $farm_name_res->fetch_assoc();

        $pdf->SetFont('Arial', 'B', 15);
        $pdf->Cell(200, 15, "{$farm_name['farm_name']}", 0, 1, 'C');
        $pdf->SetFont('Arial', '', 7);

        $farming_types_table = [15, 25, 25, 95, 30, 30, 25, 25, 25, 63];
        $pdf->Cell($farming_types_table[0], 6, "Farm", 1, 0, 'L');
        $pdf->Cell($farming_types_table[1], 6, "Type", 1, 0, 'L');
        $pdf->Cell($farming_types_table[2], 6, "Action", 1, 0, 'L');

        $pdf->Cell($farming_types_table[4], 6, "Assinged", 1, 0, 'L');
        $pdf->Cell($farming_types_table[5], 6, "Created", 1, 0, 'L');
        $pdf->Cell($farming_types_table[3], 6, "Blocks", 1, 1, 'L');

        // get distict farming_types in the tasks table from where data
        $errand_res = $db->query("SELECT * FROM management WHERE $where_data AND farm_id = {$farms_tasks['farm_id']}");
        while ($errand = $errand_res->fetch_assoc()) {
            $pdf->Cell($farming_types_table[6], 3, "", 0, 1, 'L');

            $farming_type_id = $errand['farming_type_id'];
            $farming_action_id = $errand['farming_action_id'];

            $farming_type_name_res = $db->query("SELECT * FROM farming_types WHERE record_id = $farming_type_id");
            $farming_type_name = $farming_type_name_res->fetch_assoc();

            $farming_action_name_res = $db->query("SELECT * FROM farming_action WHERE record_id = $farming_action_id");
            $farming_action_name = $farming_action_name_res->fetch_assoc();

            $pdf->Cell($farming_types_table[0], 6, $farm_name['farm_name'], 1, 0, 'L');
            $pdf->Cell($farming_types_table[9], 6, $farming_type_name['name'], 1, 0, 'L');
            $pdf->Cell($farming_types_table[2], 6, $farming_action_name['name'], 1, 0, 'L');

            $pdf->Cell($farming_types_table[4], 6, get_username($errand['user_id_created']), 1, 0, 'L');
            $pdf->Cell($farming_types_table[5], 6, get_username($errand['user_id_created']), 1, 0, 'L');
            $pdf->Cell($farming_types_table[3], 6, $errand['cluster_ids'], 1, 1, 'L');
            $pdf->Cell($farming_types_table[6], 6, "", 0, 1, 'L');
            $pdf->Cell($farming_types_table[6], 6, "Created", 1, 0, 'L');
            $pdf->Cell($farming_types_table[7], 6, "Completed", 1, 0, 'L');
            $pdf->Cell($farming_types_table[8], 6, "Action Date", 1, 1, 'L');
            $pdf->Cell($farming_types_table[6], 6, $errand['date_created'], 1, 0, 'L');
            $pdf->Cell($farming_types_table[7], 6, $errand['date_completed'], 1, 0, 'L');
            $pdf->Cell($farming_types_table[8], 6, $errand['action_date'], 1, 1, 'L');
            $pdf->SetFont('Arial', '', 10);
            $pdf->Cell($farming_types_table[6], 6, "", 0, 1, 'L');

            $pdf->Cell(100, 6, "Instructions", 0, 1, 'L');
            $pdf->SetFont('Arial', '', 7);
            $pdf->multiCell(100, 6, $errand['instructions'], 0, 'sw');

            // Coments on that task
            $pdf->SetFont('Arial', '', 10);
            $pdf->Cell($farming_types_table[6], 6, "", 0, 1, 'L');

            $pdf->Cell(100, 6, "COMENTS / TIME LOG", 0, 1, 'L');
            $pdf->SetFont('Arial', '', 7);
            $coments_res = $db->query("SELECT * FROM coms WHERE management_id = {$errand['record_id']}");
            while ($coment = $coments_res->fetch_assoc()) {
                    $user_id_res = $db->exec_query("users", ['*'], '', '', '', '', "record_id = {$coment['user_id']}");
                    $user_id_info = $user_id_res->fetch_assoc();
                    $username = $user_id_info['username'];

                $pdf->multiCell(100, 6, $coment['date_time'] . " : " . $coment['message'] . " -> " . $username, 0, 'L');
            }
            $pdf->Cell(150, 8, "", "B", 1, 'L');
        }
    }

    $pdf->Output("I");
}