<?php
require('classes/fpdf.php');
require('classes/db.class.php');

function get_asset_id($asset_id)
{
    if ($asset_id == "NONE") {
    } else {

        $db = new db();

        $res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = $asset_id");
        if ($res) {
            $values = $res->fetch_assoc();

            return $values['asset_id'];
        } else {
            return "NONE";
        }
    }
}

if (isset($_GET['asset_record_id'])) {
    $year = date('Y');
    $db = new db();

    for ($m = 1; $m < 13; $m++) {
        for ($d = 1; $d < 32; $d++) {
            $day = date('D', $time = strtotime($year . "-" . $m . "-" . $d));
            if (strlen($m) < 2) {
                $m = '0' . $m;
            }
            if (strlen($d) < 2) {
                $d = '0' . $d;
            }
            $date = $m . "-" . $d;
            // echo $day;
            if ($day == "Sun") {
                $Week_dates[] = $date;
            }
        }
    }

    $record_id = $_GET['asset_record_id'];
    $index = 0;
    foreach ($Week_dates as $date) {

        if ($index == 0) {

            $services_info_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = $record_id AND date_time_closed BETWEEN '$year-01-01 00:00' AND '$year-" . $date . " 23:59' AND job_card_type != 'SERVICE' AND job_card_type != 'BREAKDOWN'", '');
            $job_cards_other["$date"] = $services_info_res->num_rows;
            $services_info_res = '';

            $services_info_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = $record_id AND date_time_closed BETWEEN '$year-01-01 00:00' AND '$year-" . $date . " 23:59' AND job_card_type = 'SERVICE'", '');
            $job_cards_service["$date"] = $services_info_res->num_rows;
            $services_info_res = '';

            $services_info_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = $record_id AND date_time_closed BETWEEN '$year-01-01 00:00' AND '$year-" . $date . " 23:59' AND job_card_type = 'BREAKDOWN'", '');
            $job_cards_breakdown["$date"] = $services_info_res->num_rows;
            $services_info_res = '';

            $services_info_res = $db->exec_query('inspections', ['*'], '', '', '', '', "asset_record_id = $record_id AND date_time BETWEEN '$year-01-01 00:00' AND '$year-" . $date . " 23:59'", '');
            $inspections["$date"] = $services_info_res->num_rows;
            $services_info_res = '';

            $odo_res = $db->exec_query('asset_odo', ['*'], '', '', '', '', "asset_record_id = $record_id AND date BETWEEN '$year-01-01 00:00' AND '$year-" . $date . " 23:59'", "ORDER BY record_id ASC");
            $odo_total = 0;
            $odo_index = 0;
            while ($odo = $odo_res->fetch_assoc()) {
                if ($odo_index == 0) {
                    $old_odo = $odo['odo'];
                } else {
                    $odo_total = $odo_total + ($odo['odo'] - $old_odo);
                    $old_odo = $odo['odo'];
                }
                $odo_index++;
            }
            $odo_array["$date"] = $odo_total;
            $odo_res = '';
            $odo_total = 0;
        } else {
            $services_info_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = $record_id AND date_time_closed BETWEEN '$year-" . $Week_dates[$index - 1] . " 00:00' AND '$year-" . $date . " 23:59' AND job_card_type != 'SERVICE' AND job_card_type != 'BREAKDOWN'", '');
            $job_cards_other["$date"] = $services_info_res->num_rows;
            $services_info_res = '';


            $services_info_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = $record_id AND date_time_closed BETWEEN '$year-" . $Week_dates[$index - 1] . " 00:00' AND '$year-" . $date . " 23:59' AND job_card_type = 'SERVICE'", '');
            $job_cards_service["$date"] = $services_info_res->num_rows;
            $services_info_res = '';


            $services_info_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = $record_id AND date_time_closed BETWEEN '$year-" . $Week_dates[$index - 1] . " 00:00' AND '$year-" . $date . " 23:59' AND job_card_type = 'BREAKDOWN'", '');
            $job_cards_breakdown["$date"] = $services_info_res->num_rows;
            $services_info_res = '';


            $services_info_res = $db->exec_query('inspections', ['*'], '', '', '', '', "asset_record_id = $record_id AND date_time BETWEEN '$year-" . $Week_dates[$index - 1] . " 00:00' AND '$year-" . $date . " 23:59'", '');
            $inspections["$date"] = $services_info_res->num_rows;
            $services_info_res = '';

            $odo_res = $db->exec_query('asset_odo', ['*'], '', '', '', '', "asset_record_id = $record_id AND date BETWEEN '$year-" . $Week_dates[$index - 1] . " 00:00' AND '$year-" . $date . " 23:59'", "ORDER BY record_id ASC");
            $odo_total = 0;
            $odo_index = 0;
            while ($odo = $odo_res->fetch_assoc()) {
                if ($odo_index == 0) {
                    $old_odo = $odo['odo'];
                } else {
                    // echo '<br>'.$old_odo;
                    // echo '<br>'.$odo['odo'];
                    $odo_total = $odo_total + ($odo['odo'] - $old_odo);
                    // echo '<br>'.$odo_total;
                    $old_odo = $odo['odo'];
                }
                $odo_index++;
            }
            $odo_array["$date"] = $odo_total;
            $odo_res = '';
            $odo_total = 0;
        }
        $index++;
    }


    // var_dump($job_cards_other);
    // var_dump($job_cards_service);
    // var_dump($odo_array);

    $asset_info_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = $record_id");
    $asset_info = $asset_info_res->fetch_assoc();


    $farm_info_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$asset_info['farm_id']}");
    $farm_info = $farm_info_res->fetch_assoc();

    $type_info_res = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$asset_info['asset_type_id']}");
    $type_info = $type_info_res->fetch_assoc();

    $pdf = new FPDF();
    $pdf->AliasNbPages();
    $pdf->AddPage('p');

    // $pdf->Cell(-3);
    $pdf->SetFont('Arial', 'B', 50);
    $pdf->Cell(10, 2, $asset_info['fleet_no'], 0, 1, 'L');
    $pdf->Cell(10, 15, '', 0, 1, 'L');
    $pdf->SetFont('Arial', 'B', 20);

    // $pdf->Cell(-2);
    $pdf->Cell(90, 15, "ASSET ID", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['asset_id'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "FLEET NO", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['fleet_no'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "STATUS", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['status'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "FARM", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $farm_info['farm_name'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "QR CODE", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['qr_code'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "LICENSE EXPIRY DATE", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['license_exp_date'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "DESCRPTION", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['description'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "VIN", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['vin'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "TANK SIZE", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $asset_info['fuel_size'], "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 20);

    $pdf->Cell(90, 15, "GROUP", "R", 0, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(90, 15, $type_info['asset_type_name'], "", 1, 'l');
    $pdf->AddPage('L');

    $pdf->Cell(-3);
    $pdf->SetFont('Arial', 'B', 22);
    $pdf->Cell(10, 2, 'ASSET REPORT', 0, 1, 'L');
    $pdf->SetFont('Arial', 'B', 8);

    $pdf->SetFont('Arial', 'B', 15);
    $pdf->SetXY(10, 20);
    $pdf->Cell(10, 10, "JOB CARDS & INSPECTIONS", 0, 1, 'L');

    $data_hc_att = array(
        'SERVICE' => $job_cards_service,
        'BREAKDOWNS' => $job_cards_breakdown,
        'OTHER J.C.' => $job_cards_other,
        'INSPECTIONS' => $inspections
    );
    $colors_hc_att = array(
        'SERVICE' => array(0, 0, 255),
        'BREAKDOWNS' => array(255, 0, 0),
        'OTHER J.C.' => array(180, 180, 180),
        'INSPECTIONS' => array(0, 0, 0)
    );

    $pdf->SetXY(0, 30);

    $pdf->LineGraph(342, 70, $data_hc_att, 'V', $colors_hc_att, $top_val_wens + 2, 2);

    $pdf->SetFont('Arial', 'B', 15);
    $pdf->SetXY(10, 110);
    $pdf->Cell(10, 10, "WEEKLY ODO", 0, 1, 'L');

    $data_hc_att = array(
        'ODO' => $odo_array

    );
    $colors_hc_att = array(
        'ODO' => array(0, 0, 0)

    );

    $pdf->SetXY(0, 120);

    $pdf->LineGraph(342, 70, $data_hc_att, 'V', $colors_hc_att, $top_val_wens + 2, 2);

    $pdf->AddPage('P');

    $pdf->Cell(-3);
    $pdf->SetFont('Arial', 'B', 22);
    $pdf->Cell(10, 2, 'ODO LOG', 0, 1, 'L');
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(10, 10, '', 0, 1, 'L');

    $odo_table = [50, 30];

    $pdf->Cell(-2);

    $pdf->Cell($odo_table[0], 7, 'DATE', "BR", 0, 'L');
    $pdf->Cell($odo_table[1], 7, 'ODO', "BR", 0, 'L');
    $pdf->Cell($odo_table[1], 7, 'DIFF', "B", 1, 'L');

    $odo_log_res = $db->exec_query('asset_odo', ['*'], '', '', '', '', "asset_record_id = $record_id", "ORDER BY record_id DESC");
    $old_odo = 0;
    while ($odo = $odo_log_res->fetch_assoc()) {
        $pdf->Cell(-2);
        $pdf->Cell($odo_table[0], 7, $odo['date'], "BR", 0, 'L');
        $pdf->Cell($odo_table[1], 7, $odo['odo'], "BR", 0, 'L');
        $diff =  $new_odo - $odo['odo'];
        if ($diff < 0) {
            $pdf->Cell($odo_table[1], 7, 0, "B", 1, 'L');
        } else {
            $pdf->Cell($odo_table[1], 7, $diff, "B", 1, 'L');
        }
        $new_odo = $odo['odo'];
    }



    $pdf->AddPage('P');

    $pdf->Cell(-3);
    $pdf->SetFont('Arial', 'B', 22);
    $pdf->Cell(10, 2, 'INSPECTIONS LOG', 0, 1, 'L');
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(10, 10, '', 0, 1, 'L');

    $odo_table = [50, 30];

    $table_top = [50, 35, 120, 30, 30, 20, 20, 20, 25, 20, 20, 20];

    $pdf->Cell(-2);
    $pdf->Cell($table_top[1], 5, "NUMBER", "LBR", 0, 'L');
    $pdf->Cell($table_top[1], 5, "TYPE", "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, "DATE", "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, "USER", "BR", 0, 'l');

    $pdf->Cell($table_top[1], 5, "INSPECTION PDF", "BR", 1, 'l');

    $jobcards_res = $db->exec_query('inspections', ['*'], '', '', '', '', "asset_record_id = $record_id", 'ORDER BY record_id DESC');
    $total_jobcards = 0;
    $total_open = 0;
    $total_closed = 0;
    $total_waiting = 0;
    while ($jobcard_info = $jobcards_res->fetch_assoc()) {
        $asset_info_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = {$jobcard_info['asset_record_id']}");
        $asset_info = $asset_info_res->fetch_assoc();
        $user_info_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$jobcard_info['user_id']}");
        $user_info = $user_info_res->fetch_assoc();
        $pre_inspection_res = $db->exec_query('pre_inspection_content', ['*'], '', '', '', '', '', "record_id = {$jobcard_info['inpection_id']}");
        $pre_inspection_info = $pre_inspection_res->fetch_assoc();

        $pdf->Cell(-2);
        $pdf->Cell($table_top[1], 6, $jobcard_info['record_id'], "B", 0, 'L');
        $pdf->Cell($table_top[1], 6, $pre_inspection_info['name'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, $jobcard_info['date_time'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, $user_info['username'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, "CLICK HERE", "B", 1, 'l', false, "https://muell.elegantwork.co.za/inspection.pdf.php?inspection_id={$jobcard_info['record_id']}");
        // $pdf->Cell(20,8 ,'','','','',false, "http://www.intranet.com/mb/rprh06/final.php?folio="); 
        // $pdf->link(10,10,10,10,"www.elegantwork.co.za");
        // pdf.link(x=0, y=0, w=width, h=line_height, link="https://github.com/py-pdf/fpdf2")


        $index = 0;
    }






    // var_dump($least_asset);
    // var_dump($least_num);

    $pdf->AddPage('L');


    $where_data = "asset_id = $record_id";

    $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC');
    $jobcard_totals["TOTAL"] = $jobcards_res->num_rows;

    $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "job_card_status = 'CLOSED' AND " . $where_data, 'ORDER BY record_id DESC');
    $jobcard_totals["CLOSED"] = $jobcards_res->num_rows;

    $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "job_card_status = 'OPEN' AND " . $where_data, 'ORDER BY record_id DESC');
    $jobcard_totals["OPEN"] = $jobcards_res->num_rows;

    $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "job_card_status = 'AWAITING PARTS' AND " . $where_data, 'ORDER BY record_id DESC');
    $jobcard_totals["AWAITING PARTS"] = $jobcards_res->num_rows;

    $job_card_types_res = $db->exec_query('jobcard_types', ['*']);
    // $jobcard_totals = [];
    while ($jobcard_types = $job_card_types_res->fetch_assoc()) {
        // echo '<br>'.$jobcard_types['name'];

        $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "job_card_type = '{$jobcard_types['name']}' AND " . $where_data, 'ORDER BY record_id DESC');
        $jobcard_totals["{$jobcard_types['name']}"] = $jobcards_res->num_rows;
    }

    $most_num = [];
    $most_asset = [];
    $least_num = [];
    $least_asset = [];


    $asset_arrray_res = $db->query("SELECT DISTINCT asset_id FROM job_cards WHERE $where_data");
    while ($assets = $asset_arrray_res->fetch_assoc()) {
        // echo $assets['asset_id'];
        $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = {$assets['asset_id']} AND " . $where_data, 'ORDER BY record_id DESC');
        if (strlen($most_num["TOTAL"]) > 0) {
            if ($most_num["TOTAL"] < $jobcards_res->num_rows) {
                $most_num["TOTAL"] = $jobcards_res->num_rows;
                $most_asset["TOTAL"] = $assets['asset_id'];
            }
            if ($least_num["TOTAL"] > $jobcards_res->num_rows) {
                $least_num["TOTAL"] = $jobcards_res->num_rows;
                $least_asset["TOTAL"] = $assets['asset_id'];
            }
        } else {
            $most_num["TOTAL"] = 0;
            $most_asset["TOTAL"] = "NONE";
            $least_num["TOTAL"] = 0;
            $least_asset["TOTAL"] = "NONE";
        }
        $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = {$assets['asset_id']} AND job_card_status = 'CLOSED' AND " . $where_data, 'ORDER BY record_id DESC');

        if (strlen($most_num["CLOSED"]) > 0) {
            if ($most_num["CLOSED"] < $jobcards_res->num_rows) {
                $most_num["CLOSED"] = $jobcards_res->num_rows;
                $most_asset["CLOSED"] = $assets['asset_id'];
            }
            if ($least_num["CLOSED"] > $jobcards_res->num_rows) {
                $least_num["CLOSED"] = $jobcards_res->num_rows;
                $least_asset["CLOSED"] = $assets['asset_id'];
            }
        } else {
            $most_num["CLOSED"] = 0;
            $most_asset["CLOSED"] = "NONE";
            $least_num["CLOSED"] = 0;
            $least_asset["CLOSED"] = "NONE";
        }

        $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = {$assets['asset_id']} AND job_card_status = 'OPEN' AND " . $where_data, 'ORDER BY record_id DESC');
        if (strlen($most_num["OPEN"]) > 0) {
            if ($most_num["OPEN"] < $jobcards_res->num_rows) {
                $most_num["OPEN"] = $jobcards_res->num_rows;
                $most_asset["OPEN"] = $assets['asset_id'];
            }
            if ($least_num["OPEN"] > $jobcards_res->num_rows) {
                $least_num["OPEN"] = $jobcards_res->num_rows;
                $least_asset["OPEN"] = $assets['asset_id'];
            }
        } else {
            $most_num["OPEN"] = 0;
            $most_asset["OPEN"] = "NONE";
            $least_num["OPEN"] = 0;
            $least_asset["OPEN"] = "NONE";
        }

        $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = {$assets['asset_id']} AND job_card_status = 'AWAITING PARTS' AND " . $where_data, 'ORDER BY record_id DESC');
        if (strlen($most_num["AWAITING PARTS"]) > 0) {
            if ($most_num["AWAITING PARTS"] < $jobcards_res->num_rows) {
                $most_num["AWAITING PARTS"] = $jobcards_res->num_rows;
                $most_asset["AWAITING PARTS"] = $assets['asset_id'];
            }
            if ($least_num["AWAITING PARTS"] > $jobcards_res->num_rows) {
                $least_num["AWAITING PARTS"] = $jobcards_res->num_rows;
                $least_asset["AWAITING PARTS"] = $assets['asset_id'];
            }
        } else {
            $most_num["AWAITING PARTS"] = 0;
            $most_asset["AWAITING PARTS"] = "NONE";
            $least_num["AWAITING PARTS"] = 0;
            $least_asset["AWAITING PARTS"] = "NONE";
        }



        $job_card_types_res = $db->exec_query('jobcard_types', ['*']);
        // $jobcard_totals = [];
        while ($jobcard_types = $job_card_types_res->fetch_assoc()) {
            // echo '<br>'.$jobcard_types['name'];

            $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "asset_id = {$assets['asset_id']} AND job_card_type = '{$jobcard_types['name']}' AND " . $where_data, 'ORDER BY record_id DESC');
            if (strlen($most_num["{$jobcard_types['name']}"]) > 0) {
                if ($most_num["{$jobcard_types['name']}"] < $jobcards_res->num_rows) {
                    $most_num["{$jobcard_types['name']}"] = $jobcards_res->num_rows;
                    $most_asset["{$jobcard_types['name']}"] = $assets['asset_id'];
                }
                if ($least_num["{$jobcard_types['name']}"] > $jobcards_res->num_rows) {
                    $least_num["{$jobcard_types['name']}"] = $jobcards_res->num_rows;
                    $least_asset["{$jobcard_types['name']}"] = $assets['asset_id'];
                }
            } else {
                $most_num["{$jobcard_types['name']}"] = 0;
                $most_asset["{$jobcard_types['name']}"] = "NONE";
                $least_num["{$jobcard_types['name']}"] = 0;
                $least_asset["{$jobcard_types['name']}"] = "NONE";
            }
        }
    }


    // Arial bold 15
    $pdf->SetFont('Arial', 'B', 8);
    // Move to the right

    $pdf->Cell(20);
    $pdf->Cell(60, -4, '', 0, 1, 'C');

    $header = [110, 168];
    // Title
    $pdf->SetFont('Arial', 'B', 30);
    $pdf->Cell(1);
    $pdf->Cell($header[0], 15, 'JOB CARD LOG', "BR", 0, 'L');
    $pdf->SetFont('Arial', '', 20);

    $pdf->SetFont('Arial', 'B', 8);

    $pdf->Cell($header[0], 10, "", "", 1, 'L');

    // $pdf->Line(11, 22, 290, 22);

    $totals_table = [30];
    $index = 0;
    $numbers = 0;
    $y = 28;

    foreach ($jobcard_totals as $key => $value) {
        $move = 5;
        if (($index == 6)) {
            $numbers = 0;
            $y = 45;
        } elseif ($index == 12) {
            $numbers = 0;
            $y = 50;
        }
        $pdf->SetXY(10 + ($numbers * 45), $y);
        $pdf->Cell($totals_table[0], 5, $key, "B", 1, 'C');


        $pdf->SetXY(10 + ($numbers * 45), $y + $move);
        $pdf->Cell($totals_table[0], 5, $value, "", 1, 'C');




        $index++;
        $numbers++;
    }

    // $pdf->AddPage('L');

    $table_top = [10, 32];
    $pdf->Cell(-2);
    $pdf->Cell($table_top[1], 10, "", "", 1, 'L');
    $pdf->Cell(-2);

    $pdf->Cell($table_top[1], 5, "JOBCARD NUMBER", "LBR", 0, 'L');
    $pdf->Cell($table_top[1], 5, "STATUS", "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, "DATE OPENED", "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, "USER OPENED", "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, 'DATE CLOSED', "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, "USER CLOSED", "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, "JOBCARD TYPE", "BR", 0, 'l');
    $pdf->Cell($table_top[1], 5, "JOB CARD", "BR", 1, 'l');


    $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC');
    $total_jobcards = 0;
    $total_open = 0;
    $total_closed = 0;
    $total_waiting = 0;
    while ($jobcard_info = $jobcards_res->fetch_assoc()) {
        $asset_info_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = {$jobcard_info['asset_id']}");
        $asset_info = $asset_info_res->fetch_assoc();
        $user_info_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$jobcard_info['user_id_opened']}");
        $user_info = $user_info_res->fetch_assoc();
        $pdf->Cell(-2);
        $pdf->Cell($table_top[1], 6, $jobcard_info['record_id'], "B", 0, 'L');
        $pdf->Cell($table_top[1], 6, $jobcard_info['job_card_status'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, $jobcard_info['date_time_opened'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, $user_info['username'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, $jobcard_info['date_time_closed'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, $user_info['username'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, $jobcard_info['job_card_type'], "B", 0, 'l');
        $pdf->Cell($table_top[1], 6, "CLICK HERE", "B", 1, 'l', false, "https://muell.elegantwork.co.za/jobcard.pdf.php?job_card_no={$jobcard_info['record_id']}");
    }




    $pdf->Output("I");

    // echo "Structure?";
}
