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

$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('P');

$pdf->Image('images/logo_1.png', 165, 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(10);
///////////// 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11
$table_top = [50, 35, 15, 30, 30, 30, 30, 20, 155, 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], 5, "", 0, 1, 'L');

$pdf->Cell(10);
$pdf->Cell($table_top[2], 5, "NO", "LBR", 0, 'L');
$pdf->Cell($table_top[3], 5, "DATE", "BR", 0, 'l');
$pdf->Cell($table_top[4], 5, "TYPE", "BR", 0, 'l');
$pdf->Cell($table_top[5], 5, "USER", "BR", 0, 'l');
$pdf->Cell($table_top[6], 5, "FARM", "BR", 0, 'l');
$pdf->Cell($table_top[7], 5, "FLEET", "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($table_top[8], 3, "", "", 1, 'l');
    $pdf->SetFont('Arial', 'B', 6);

    $pdf->Cell(10);
    $pdf->Cell($table_top[2], 6, $jobcard_info['record_id'], "T", 0, 'L');
    $pdf->Cell($table_top[3], 6, $jobcard_info['date_time'], "T", 0, 'l');
    $pdf->Cell($table_top[4], 6, $pre_inspection_info['name'], "T", 0, 'l');
    $pdf->Cell($table_top[5], 6, $user_info['username'], "T", 0, 'l');
    $pdf->Cell($table_top[6], 6, $farm['farm_name'], "T", 0, 'l');
    $pdf->Cell($table_top[7], 6, $asset_info['fleet_no'], "T", 1, 'l');
    $pdf->Cell(10);
    $pdf->Cell($table_top[8], 6, $asset_info['description'], "", 1, 'l');

    $note_asnwers = explode('||', $jobcard_info['question_notes']);
    $index = 0;
    foreach (explode("||", $jobcard_info['questions']) as $question) {

        if (strlen($note_asnwers[$index]) < 1) {

            continue;
        } else {
            // echo $question . " : " . $note_asnwers[$index];

            $pdf->Cell(10);
            $pdf->SetFont('Arial', 'B', 5);
            $pdf->Cell($table_top[8], 6, $question . " : " . $note_asnwers[$index], "", 1, 'l');
            $pdf->SetFont('Arial', 'B', 8);
        }
        $index++;
    }
    if (strlen($jobcard_info['notes']) > 1) {

        $pdf->Cell(10);
        $pdf->MultiCell($table_top[8], 6, $jobcard_info['notes'],"","L");
    }
}


$pdf->Output("I");

// echo "Structure?";
