<?
include $_SERVER['DOCUMENT_ROOT'] . "/WebBuilder/pdf.class.php";
include $_SERVER['DOCUMENT_ROOT'] . "/WebBuilder/fpdf.php";

$db = new DBMain();
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('P');

$booking_id = $_GET['booking_id'];

$booking_res = $db->exec_query('bookings', ['*'], '', '', '', '', "record_id = '{$booking_id}'", "", false);
// echo "SELECT * FROM bookings WHERE record_id = {$_GET['id']}";
$booking = $booking_res->fetch_assoc();

if (explode(',', substr($booking['assessments'], 1))) {
    // echo "ASSESSMENTS";
    $assess = explode(',', substr($booking['assessments'], 1));

    $client_employee = explode(',', substr($booking['client_employees'], 1));

    $assesment_res = $db->exec_query('assesses', ['*'], '', '', '', '', "booking = {$booking_id}", "", false);
    // echo "SELECT * FROM assesses WHERE booking = {$booking_id}";
    $assessor = $assesment_res->fetch_assoc();

    $candidate_res = $db->exec_query('client_employees', ['*'], '', '', '', '', "record_id = {$assessor['client_employees_id']}", "", false);
    // echo "SELECT * FROM client_employees WHERE record_id = {$assessor['client_employees_id']}";
    $candidate = $candidate_res->fetch_assoc();

    $client_res = $db->exec_query('clients', ['*'], '', '', '', '', "record_id = {$assessor['clients_id']}", 'ORDER BY clients_name ASC');
    // echo "SELECT * FROM clients WHERE record_id = {$assessor['clients_id']}";
    $client = $client_res->fetch_assoc();

    $date = date("Y-m-d", strtotime($booking['date_done']));

    $newDate = date("Y-m-d", strtotime($date . " +12 months"));

    $pdf->SetFont('Arial', 'B', 20);
    $pdf->Cell(200, 15, "ASSESSOR LICENSE : ", 0, 1, 'C');
    $pdf->Cell(10, 20, '', 0, 1, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(180, 10, '', "LTR", 1, 'L');

    $pdf->Image("../uploads/license_background.jpg", 12, 48, 175, 90);

    if (file_exists("../uploads/$assessor[image_file_path]")) {
        $pdf->Image("../uploads/$assessor[image_file_path]", 20, 50, 50, 50);
    }

    if (file_exists("../uploads/$client[client_logo]")) {
        $pdf->Image("../uploads/$client[client_logo]", 30, 105, 30, 30);
    }

    $pdf->Cell(110, 8, "INDUCTION EXP  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $booking['induction_date'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "MEDICAL EXP  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $booking['medical_date'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "EMPLOYEE  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $candidate['client_employees_name'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "INDUSTRY #  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $booking['industry'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');


    $pdf->Cell(180, 15, '', "LR", 1, 'L');


    $pdf->Cell(110, 8, "ASSESSMENT DATE  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $date, 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "EXPIRY DATE  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $newDate, 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 15, '', "LRB", 1, 'L');

    $pdf->Cell(10, 20, '', 0, 1, 'L');



    $pdf->Cell(180, 15, '', "LTR", 1, 'L');
    $pdf->Image("../uploads/license_background.jpg", 12, 163, 175, 92);

    $pdf->Cell(110, 8, "AUTHORISED TO OPERATE : ", "L", 0, 'R');
    $pdf->Cell(65, 8, $assessor['assesses_name'], 1, 0, 'C');
    $pdf->Cell(5, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "EXP DATE  :  ", "L", 0, 'R');
    $pdf->Cell(65, 8, $newDate, 1, 0, 'C');
    $pdf->Cell(5, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 10, '', "LR", 1, 'L');

    $pdf->Cell(110, 30, '', "LR", 0, 'L');
    $pdf->Cell(65, 30, '', "TB", 0, 'C');
    $pdf->Cell(5, 30, '', "LR", 1, 'R');

    if (file_exists("../signatures/{$assessor['client_employees_id']}-{$booking_id}-signature.png")) {
        $pdf->Image("../signatures/{$assessor['client_employees_id']}-{$booking_id}-signature.png", 140, 205, 35, 35);
    }

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(172, 15, "EMPLOYEE SIGNATURE  :  ", "L", 0, 'R');
    $pdf->Cell(8, 13, "", "R", 1, 'R');

    $pdf->Cell(110, 8, "DATE  :  ", "L", 0, 'R');
    $pdf->Cell(65, 8, $date, 1, 0, 'C');
    $pdf->Cell(5, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 3, '', "LBR", 1, 'L');


    $pdf->Output("I");
} else {
    // echo "TESTS";
    $tests = explode(',', substr($booking['tests'], 1));

    // TEST 
    $test_sec_res = $db->exec_query('tests_sec', ['*'], '', '', '', '', "test_id = {$tests[0]}");
    // echo "SELECT * FROM tests_sec WHERE test_id = {$tests[0]}";
    $tests_sec = $test_sec_res->fetch_assoc();

    $test_res = $db->exec_query('tests', ['*'], '', '', '', '', "record_id = {$tests_sec['test_id']}", "ORDER BY test_name ASC");
    // echo "SELECT * FROM tests WHERE record_id = {$tests[0]}";
    $test = $test_res->fetch_assoc();

    $candidate_res = $db->exec_query('client_employees', ['*'], '', '', '', '', "record_id = {$tests_sec['client_employees_id']}");
    // echo "SELECT * FROM client_employees WHERE record_id = {$tests_sec['client_employees_id']}";
    $test_candidate = $candidate_res->fetch_assoc();

    $client_res = $db->exec_query('clients', ['*'], '', '', '', '', "record_id = {$tests_sec['clients_id']}", 'ORDER BY clients_name ASC');
    // echo "SELECT * FROM clients WHERE record_id = {$tests_sec['clients_id']}";
    $test_client = $client_res->fetch_assoc();

    $newTestDate = date("Y-m-d", strtotime($test_date . " +12 months"));

    $test_date = date("Y-m-d", strtotime($booking['date_done']));


    $pdf->SetFont('Arial', 'B', 20);
    $pdf->Cell(200, 15, "ASSESSOR LICENSE : ", 0, 1, 'C');
    $pdf->Cell(10, 20, '', 0, 1, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(180, 10, '', "LTR", 1, 'L');

    $pdf->Image("../uploads/license_background.jpg", 12, 48, 175, 90);

    $pdf->Image("../uploads/$tests_sec[image_file_path]", 20, 50, 50, 50);

    $pdf->Image("../uploads/$test_client[client_logo]", 30, 105, 30, 30);



    $pdf->Cell(110, 8, "INDUCTION EXP  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $booking['induction_date'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "MEDICAL EXP  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $booking['medical_date'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "EMPLOYEE  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $test_candidate['client_employees_name'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "INDUSTRY #  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $booking['industry'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');


    $pdf->Cell(180, 15, '', "LR", 1, 'L');


    $pdf->Cell(110, 8, "ASSESSMENT DATE  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $test_date, 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(110, 8, "EXPIRY DATE  :  ", "L", 0, 'R');
    $pdf->Cell(60, 8, $newTestDate, 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 15, '', "LRB", 1, 'L');

    $pdf->Cell(10, 20, '', 0, 1, 'L');



    $pdf->Cell(180, 15, '', "LTR", 1, 'L');
    $pdf->Image("../uploads/license_background.jpg", 12, 163, 175, 92);

    $pdf->Cell(120, 8, "AUTHORISED TO OPERATE  :  ", "L", 0, 'R');
    $pdf->Cell(50, 8, $test['test_name'], 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(120, 8, "EXP DATE  :  ", "L", 0, 'R');
    $pdf->Cell(50, 8, $newTestDate, 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 10, '', "LR", 1, 'L');

    $pdf->Cell(120, 27, '', "LR", 0, 'L');
    $pdf->Cell(50, 27, '', "TB", 0, 'C');
    $pdf->Cell(10, 27, '', "LR", 1, 'R');

    if (file_exists("../signatures/1-1-signature.png")) {
        $pdf->Image("../signatures/{$assessor['client_employees_id']}-{$_GET[id]}-signature.png", 140, 198, 40, 40);
    }
    $pdf->Cell(180, 2, '', "LR", 1, 'L');

    $pdf->Cell(172, 15, "EMPLOYEE SIGNATURE  :  ", "L", 0, 'R');
    $pdf->Cell(8, 13, "", "R", 1, 'R');

    // $pdf->Cell(100, 10, '', "LR", 1, 'L');

    // $pdf->Cell(160, 10, "DATE : " . "  " . $test_date, "LR", 0, 'C');

    $pdf->Cell(120, 8, "DATE  :  ", "L", 0, 'R');
    $pdf->Cell(50, 8, $test_date, 1, 0, 'C');
    $pdf->Cell(10, 8, "  ", "R", 1, 'R');

    $pdf->Cell(180, 3, '', "LBR", 1, 'L');


    $pdf->Output("I");

}
