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

function get_username($user_id)
{
    $db = new db();

    $res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = $user_id");
    $values = $res->fetch_assoc();

    return $values['username'];
}

if (isset($_GET['inspection_id'])) {
    $db = new db();

    $inspection_id = $_GET['inspection_id'];

    $inspection_details = $db->exec_query('inspections', ['*'], '', '', '', '', "record_id = $inspection_id");
    $inspection_info = $inspection_details->fetch_assoc();

    $inspection_type = $db->exec_query('pre_inspection_content', ['*'], '', '', '', '', "record_id = {$inspection_info['inpection_id']}");
    $inspection_type_info = $inspection_type->fetch_assoc();

    $farm_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$inspection_info['farm_id']}");
    $farm = $farm_res->fetch_assoc();

    $asset_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = {$inspection_info['asset_record_id']}");
    $asset_info = $asset_res->fetch_assoc();
    $pdf = new FPDF();
    $pdf->AliasNbPages();
    $text_size = 8;

    $pdf->AddPage('P');
    /// HEADER ///////////////////
    $pdf->SetFont('Arial', 'B', 8);
    $header_table = [80, 30, 20, 20];
    $pdf->Cell($header_table[0], -8, '', 0, 1, 'L');
    $pdf->Cell(-5);

    $pdf->SetFont('Arial', 'BU', 20);
    $pdf->Cell($header_table[0], 18, 'PRE INSPECTION', "B", 0, 'C');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell($header_table[2], 9, 'No: ', "B", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[2], 9, $inspection_id, "B", 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell($header_table[2], 9, 'ASSET: ', "B", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[2], 9, $asset_info['asset_id'], "B", 0, 'l');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell($header_table[2], 9, 'FARM: ', "B", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[2], 9, $farm['farm_name'], "B", 1, 'L');

    $pdf->Cell(75);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell($header_table[1], 9, 'USER: ', "L", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[1], 9, get_username($inspection_info['user_id']), "", 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell($header_table[1], 9, 'DATE: ', "", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[1], 9, $inspection_info['date_time'], "R", 1, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(-5);

    $pdf->Cell(80, 9, $asset_info['description'], 1, 1, 'C');

    $pdf->Cell($content[0], 5, '', 0, 1, 'C');

    /// HEADER ///////////////////

    $pdf->SetFont('Arial', 'B', 12);
    // $pdf->SetXY(10, 25);
    $pdf->Cell(10);

    $pdf->MultiCell(170, 6, "LIST", "B", 'C');
    $pdf->Cell($content[0], 5, '', 0, 1, 'C');

    // $q_a_array = explode(',', $jobcard_info['warning_questions']);
    $questions_array = explode('||', $inspection_info['questions']);
    $answers_array = explode('||', $inspection_info['answers']);
    $question_notes = explode('||', $inspection_info['question_notes']);
    $list_content = [100, 20];
    // $pdf->SetXY(150, 45);

    $index = 0;
    foreach ($questions_array as $question) {
        if ($index == 0) {
            $pdf->Cell(10);
            $pdf->Cell($content[0], 2, "", "", 1, 'C');
            $pdf->Cell(10);
        } else {
            $pdf->Cell(10);
        }
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->MultiCell($list_content[0], 5, strtoupper($question), "", 'L');
        $pdf->Cell(10);
        $pdf->SetFont('Arial', "", 7);
        $pdf->MultiCell($list_content[0], 7, strtoupper($answers_array[$index]), 0, 'L');
        if (strlen($question_notes[$index]) > 1) {
            $pdf->Cell(10);
            $pdf->SetFont('Arial', "", 7);
            $pdf->MultiCell($list_content[0], 7, "REASON: " . strtoupper($question_notes[$index]), 0, 'L');
        }
        $index++;
    }

    $pdf->Cell(10);

    $pdf->SetFont('Arial', 'B', 12);
    // $pdf->SetXY(8, 25);
    $pdf->MultiCell(170, 6, "NOTES", "B", 'C');
    // $pdf->SetXY(8, 35);
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(10);

    $pdf->MultiCell(130, 6, $inspection_info['notes'], "", 'L');
    /// OUTPUT????
    $pdf->Output("I");
}
