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

// if (isset($_GET['structure_id'])) {


$db = new db();
$where_data = str_replace('|', "'", $_POST['where_data']);
$jobcards_res = $db->exec_query('inspections', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC');
$total = 0;

while ($jobcards_res->fetch_assoc()) {
    $total++;
}


$pdf = new FPDF();
$pdf->AliasNbPages();



$pdf->AddPage('L');

$pdf->Image('images/logo_1.png', 255, 2, 40);

// Arial bold 15
$pdf->SetFont('Arial', 'B', 8);
// Move to the right
$pdf->Cell(20);
$pdf->Cell(60, -4, '', 0, 1, 'C');
$pdf->Cell(19);

$table_top = [50, 35, 30, 30, 30, 20, 20, 20, 25, 20, 20, 20];
// Title
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'PRE INSPECTION REPORT', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(10);

$pdf->Cell($table_top[1], 7, 'TOTAL INSPECTIONS', "BR", 1, 'L');

$pdf->Cell(10);
$pdf->Cell($table_top[1], 5, $total, "BR", 1, 'L');


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

$pdf->Cell(10);
$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, "FARM", "BR", 0, 'l');
$pdf->Cell($table_top[1], 5, "ASSET ID", "BR", 1, 'l');

$jobcards_res = $db->exec_query('inspections', ['*'], '', '', '', '', $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_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();
    $farm_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$jobcard_info['farm_id']}");
    $farm = $farm_res->fetch_assoc();

    $pdf->Cell(10);
    $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, $farm['farm_name'], "B", 0, 'l');
    $pdf->Cell($table_top[1], 6, $asset_info['asset_id'], "B", 1, 'l');

}

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

$index = 0;
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;
        if ($day == "Mon") {
            $Week_dates[] = $date;
            $index++;
        }
    }
}

// $where_data = str_replace('|', "'", $_POST['where_data']);
// $asset_array[] = '';
// $select_assets = $db->Insert("SELECT DISTINCT asset_id FROM job_cards WHERE $where_data");
// while ($asset_rows = $select_assets->fetch_assoc()) {


//     $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', $where_data . " AND asset_id = '{$asset_rows['asset_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()) {

//         if ($jobcard_info['job_card_status'] == 'OPEN') {
//             $total_open = $total_open + 1;
//         }

//         if ($jobcard_info['job_card_status'] == 'CLOSED') {
//             $total_closed = $total_closed + 1;
//         }

//         if ($jobcard_info['job_card_status'] == 'AWAITING PARTS') {
//             $total_waiting = $total_waiting + 1;
//         }
//         $total_jobcards++;
//     }
//     $asset_info_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = {$asset_rows['asset_id']}");
//     $asset_info = $asset_info_res->fetch_assoc();
//     $asset_array_open["{$asset_info['asset_id']}"] = $total_open;
//     $asset_array_closed["{$asset_info['asset_id']}"] = $total_closed;
//     $asset_array_waiting["{$asset_info['asset_id']}"] = $total_waiting;
// }




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

// $pdf->SetXY(10, 40);
// $pdf->Cell(10, 5, "ASSET JOBCARDS", 0, 1, 'L');

// $data_sal_att = array(
//     'OPEN' => $asset_array_open,
//     'CLOSED' => $asset_array_closed,
//     'WAITING' => $asset_array_waiting,
// );
// $colors_sal_att = array(
//     'OPEN' => array(0, 0, 255),
//     'CLOSED' => array(0, 255, 0),
//     'WAITING' => array(255, 0, 0),
// );


// $pdf->SetXY(1, 50);

// $pdf->LineGraph(100, 45, $data_sal_att, 'VkB', $colors_sal_att);


$pdf->Output("I");

// echo "Structure?";
