<?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['job_card_no'])) {
    $db = new db();

    $job_card_no = $_GET['job_card_no'];

    $jobcard_info_res = $db->exec_query('job_cards', ['*'], '', '', '', '', "record_id = $job_card_no");
    $jobcard_info = $jobcard_info_res->fetch_assoc();
    $asset_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = {$jobcard_info['asset_id']}");
    $asset_info = $asset_res->fetch_assoc();
    $pdf = new FPDF();
    $pdf->AliasNbPages();

    $text_size = 12;
    $heading_size = 15;

    $pdf->AddPage('L');
    /// HEADER ///////////////////
    $pdf->Image('images/logo_1.png', 255, 180, 40);

    $pdf->SetFont('Arial', 'B', 8);
    $header_table = [80, 35, 30,50];
    $pdf->Cell($header_table[0], -8, '', 0, 1, 'L');
    $pdf->Cell(-5);

    $pdf->SetFont('Arial', 'BU', 20);
    $pdf->Cell($header_table[0], 18, $jobcard_info['job_card_type'], "B", 0, 'C');
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($header_table[1], 9, 'JOB CARD: ', "", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[1], 9, $job_card_no, "", 0, 'L');
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($header_table[1], 9, 'ASSET: ', "", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[1], 9, $asset_info['asset_id'], "", 0, 'l');
    $pdf->Cell($header_table[1], 9, $asset_info['description'], "", 1, 'l');

    $pdf->Cell(75);
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($header_table[3], 9, 'USER OPENED: ', "TL", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[3], 9, get_username($jobcard_info['user_id_opened']), "T", 0, 'L');
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($header_table[3], 9, 'USER CLOSED: ', "T", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($header_table[3], 9, get_username($jobcard_info['user_id_closed']), "TR", 1, 'L');

    $pdf->Cell(-5);
    $sub_header = [46, 38, 62, 50];
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($sub_header[0], 9, 'CREATED: ', "L", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($sub_header[0], 9, $jobcard_info['date_time_created'], "", 0, 'L');
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($sub_header[1], 9, 'OPEND: ', "", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($sub_header[1], 9, $jobcard_info['date_time_opened'], "", 0, 'l');
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($sub_header[2], 9, 'CLOSED: ', "", 0, 'R');
    $pdf->SetFont('Arial', "", $text_size);
    $pdf->Cell($sub_header[3], 9, $jobcard_info['date_time_closed'], 'R', 1, 'L');
    $pdf->Line(5, 29, 285, 29);

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

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

    $pdf->Cell(-5);
    $content = [140, 140, 62, 50];
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell($content[0], 9, 'PARTS: ', "BR", 0, 'C');
    $pdf->Cell($content[0], 9, "INSPECTION LIST", "B", 1, 'C');
    $pdf->Cell(-5);
    $pdf->SetFont('Arial', '', $text_size);
    $pdf->SetXY(8, 45);
    $pdf->MultiCell(125, 7, $jobcard_info['parts'], 0, "L");
    $pdf->SetXY(8, 120);
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell(140, 9, 'NOTES: ', "BR", 1, 'C');
    $pdf->SetFont('Arial', '', $text_size);
    $pdf->SetXY(8, 135);
    $pdf->MultiCell(125, 7, $jobcard_info['notes'], 0, "c");
    $pdf->SetXY(145, 45);


    $q_a_array = explode(',', $jobcard_info['warning_questions']);
    $questions_array = explode('||', $q_a_array[0]);
    $answers_array = explode('||', $q_a_array[1]);
    $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(145);
        } else {
            $pdf->Cell(145);
        }
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->MultiCell($list_content[0], 6, strtoupper($question), "", 'L');
        $pdf->Cell(145);
        $pdf->Cell($content[0], 2, "", "", 1, 'C');
        $pdf->Cell(145);
        $pdf->SetFont('Arial', "", $text_size);

        $pdf->MultiCell($list_content[0], 9, strtoupper($answers_array[$index]), 0, 'L');
        $pdf->Cell(145);
        $pdf->Cell($content[0], 4, "", "", 1, 'C');
        $index++;
    }

    /// OUTPUT????
    $pdf->Output("I");
}
