<?php include $_SERVER['DOCUMENT_ROOT'] . "/WebBuilder/pdf.class.php";
$db = new DBMain();

$test_sec_res = $db->exec_query('tests_sec', ['*'], '', '', '', '', "record_id = {$_GET['id']}");
$tests_sec = $test_sec_res->fetch_assoc();

$test_res = $db->exec_query('tests', ['*'], '', '', '', '', '1', "ORDER BY test_name ASC");
$test = $test_res->fetch_assoc();

$test_questions_res = $db->exec_query('test_questions', ['*'], '', '', '', '', "test_id = {$test['record_id']}");
$test_questions = $test_questions_res->fetch_assoc();

$test_answers_res = $db->exec_query('test_answers', ['*'], '', '', '', '', "test_question_id = {$test_questions['record_id']}");
$test_answers = $test_answers_res->fetch_assoc();

$candidate_res = $db->exec_query('client_employees', ['*'], '', '', '', '', "record_id = {$tests_sec['client_employees_id']}");
$candidate = $candidate_res->fetch_assoc();

$client_res = $db->exec_query('clients', ['*'], '', '', '', '', "record_id = {$tests_sec['clients_id']}", 'ORDER BY clients_name ASC');
$client = $client_res->fetch_assoc();

$padding = '10px';

$date_now = date('Y-m-d H:i', strtotime("+ 2 Hours"));

$html .= "

<style>

    td {
        padding: 20px;
        font-size: 12px;
    }
    tr{
    padding: 20px;
}
</style>

<table style='width:80%;float:left;top:5%;border: 0px solid #038c5a;'>
    <tr style='padding:10px;'>
        <td colspan='2'>
            <h1 style='margin-left:50px;'>SAFETY & TRAINING PRACTITIONERS</h1>
        </td>
    </tr>
    <tr>
        <td colspan='2'>
            <h1 style='margin-left:50px;'>{$test['test_name']}</h1>
        </td>
    </tr>
        <tr>
        <td colspan='2'>
            <h3 style='margin-left:50px;'>{$test['test_description']}</h3>
        </td>
    </tr>
        <tr>
        <td>
            <h3 style='margin-left:50px;'>Candidate: $candidate[client_employees_name]</h3>
        </td>
    </tr>
    <tr>
        <td>
            <h3 style='margin-left:50px;'>Client: $client[clients_name]</h3>
        </td>
    </tr>
    <tr>
        <td>
           <h3 style='margin-left:50px;'> NQF LEVEL: $test[nqf_level]</h3>
        </td>
        <td>
            <h3 style='margin-left:50px;'>Credits : $test[credits]</h3>
        </td>
        <td>
            <h3 style='margin-left:50px;'>Date: $date_now</h3>
        </td>
    </tr>

</table>
";


$questions_res = $db->exec_query('tests_questions_sec', ['*'], '', '', '', '', "tests_sec_id = {$tests_sec['record_id']}", '');
while ($questions = $questions_res->fetch_assoc()) {

    $html .= "
<table style='width:80%;top:5%;border: 1px solid BLACK;margion-left:auto;margion-right:auto;'>
    <tr style='padding:10px;'>
        <td style='width:5%;border: 1px solid BLACK;'>
            <h1> </h1>
        </td>
        <td style='width:70%;border: 1px solid BLACK;'>
            <h3 >$questions[questions]</h3>
        </td>
        <td style='width:5%;border: 1px solid BLACK;'>
            <h3 >YES</h3>
        </td>
        <td style='width:5%;border: 1px solid BLACK;'>
            <h3 >NO</h3>
        </td>
        <td style='width:15%;border: 1px solid BLACK;'>
            <h3>COMMENTS</h3>
        </td>
        <td style='width:25%;border: 1px solid BLACK;'>
            <h3>CORRECT ANSWER</h3>
        </td>
    </tr>

";


    $answer_res = $db->exec_query('tests_answers_sec', ['*'], '', '', '', '', "tests_questions_sec_id = {$questions['record_id']}");
    $answers_index = 1;
    $anser_nyc = 0;
    $anser_c = 0;
    while ($answers = $answer_res->fetch_assoc()) {

        $html .= "
    <tr style='padding:10px;'>
        <td style='width:5%; border: 1px solid BLACK;'>
            <p>$answers_index</p>
        </td>
        <td style='width:70%;border: 1px solid BLACK;'>
            <p>$answers[answer]</p>
        </td>
        <td style='width:5%;border: 1px solid BLACK;'>

        ";

        if ($answers['option'] == "1") {
            $html .= "
            <h3>X</h3>
        </td> 
   
  <td style='width:5%;border: 1px solid BLACK;'>
  </td>
    ";

            $anser_c++;
        } else {
            $html .= "
    </td>
  <td style='width:5%;border: 1px solid BLACK;'>
            <h3>X</h3>
        </td>
    ";

            $anser_nyc++;
        }
        $html .= "
       <td style='width:15%;border: 1px solid BLACK;'>
            <h3>$answers[comment]</h3>
        </td>

        <td style='width:25%;border: 1px solid BLACK;'>
        ";
            if ($test_answers['option'] == 1) {
                $html .= "
            <h3>YES</h3>
            ";
            } else {
                $html .= "
            <h3>NO</h3>
            ";
            }
            
        $html .= "
        </td>
    </tr>
    ";
        $answers_index++;
    }

    $html .= "
    <tr>
        <td style='width:5%;border: 1px solid BLACK;' colspan='2'>
        <p>TOTAL</p>
        </td>
        <td style='width:5%;border: 1px solid BLACK;'>
        <p>$anser_c</p>
        </td>
        <td style='width:5%;border: 1px solid BLACK;'>
        <p>$anser_nyc</p>
        </td>
        <td style='width:15%;border: 1px solid BLACK;'>
        </td>
        <td style='width:15%;border: 1px solid BLACK;'>
        </td>
    </tr>
</table>
<br><br>
";
}


$html .= "
<br><br>
<div style='height:fit-content; margin-left:auto; margin-right:auto;border: 1px solid black;padding:40px'><h2>RECOMMENDATIONS FOR IMPROVEMENT RELATED TO OPERATING TECHNIQUES</h2>$tests_sec[notes]</div>
";


$html .= "<br><br>
<h2><u>FINAL TEST - ACCEPTANCE</u></h2>
<table style='width:80%;top:5%;border: 0px solid black;'>
<tr>
<td style='border: 1px solid black; ' colspan='2'>
<b><p>RESULT</p></b>
</td>
<td style='border: 1px solid black; '>
<b><p>ASSESSOR’S NAME</p></b>
</td>
<td style='border: 1px solid black; '>
<b><p>ASSESSOR’S SIGNATURE</p></b>
</td>
<td style='border: 1px solid black; '>
<b><p>DATE</p></b>
</td>
</tr>
<tr>
<td style='border: 1px solid black; '>
<b><p>C</p></b>
</td>
<td style='border: 1px solid black; '>
<b><p>NYC</p></b>
</td>
<td style='border: 1px solid black; '>
<b><p>$candidate[client_employees_name]</p></b>
</td>
<td style='border: 1px solid black; '>
<img src='../signatures/a-$tests_sec[safesure_users_id]-signature.png' width='80px'>
</td>
<td style='border: 1px solid black; '>
$tests_sec[date]</td>
</tr>
<tr>
";
if ($tests_sec['results'] == 'C') {
    $html .= "
<td style='border: 1px solid black; '>
<b><p>X</p></b>
</td>
<td style='border: 1px solid black; '>
<b><p></p></b>
</td>";
} else {
    $html .= "
<td style='border: 1px solid black; '>
<b><p></p></b>
</td>
<td style='border: 1px solid black; '>
<b><p>X</p></b>
</td>";
}

$html .= "
<td style='border: 1px solid black; '>
<b><p>$test_candidate[client_employees_name]</p></b>
</td>
<td style='border: 1px solid black; '>
<img src='../signatures/$tests_sec[client_employees_id]-$tests_sec[booking]-signature.png'  width='80px'>
</td>
<td style='border: 1px solid black; '>
$tests_sec[date]</td>
</tr>

</table>

<br><br>

<table style='width:80%;top:5%;border: 1px solid black;'>
<tr>
    <td style='width:100%;border: 1px solid black;' colspan='2'>
        <p><b>I declare that I am in good health and medically fit to undergo this training session and do not
        suffer from any of the following conditions:
        </b></p>
    </td>
</tr>
<tr>
    <td style='border: 1px solid black;'>
    <p><b>
        1
    </b></p>
    </td>
    <td style='border: 1px solid black;'>
    <p><b>
        Uncontrolled Epilepsy.
    </b></p>
    </td>
</tr>
<tr>
<td style='border: 1px solid black;'>
<p><b>2</b></p>
</td>
<td style='border: 1px solid black;'>
<p><b>Liability to sudden attacks of disabling giddiness of fainting due to Hypertension or any other
causes</b></p>
</td>
</tr>
<tr>
<td style='border: 1px solid black;'>
<p><b>3</b></p>
</td>
<td style='border: 1px solid black;'>
<p><b>Any form of mental disorder or defect referred to in section 2(1) and classified in (section 3 of the
Mental Disorder act 38 of 1916).</b></p>
</td>
</tr>
<tr>
<td style='border: 1px solid black;'>
<p><b>4</b></p>
</td>
<td style='border: 1px solid black;'>
<p><b>Addiction to narcotic drugs and/or addiction to excessive intoxicating liquor.</b></p>
</td>
</tr>
<tr>
<td style='border: 1px solid black;'>
<p><b>5</b></p>
</td>
<td style='border: 1px solid black;'>
<p><b>Any condition causing poor muscular co-ordination.</b></p>
</td>
</tr>
<tr>
<td style='border: 1px solid black;'>
<p><b>6</b></p>
</td>
<td style='border: 1px solid black;'>
<p><b>Uncontrolled Diabetes Mellitus.</b></p>
</td>
</tr>
<tr>
<td style='border: 1px solid black;'>
<p><b>7</b></p>
</td>
<td style='border: 1px solid black;'>
<p><b>Any other disease or disability including vision, which is likely to render me incapable of effective
operating the class of machine which the certificate of competence referred and without
endangering the public or fellow workers.</b></p>
</td>
</tr>
<tr>
<td style='border: 1px solid black;'>
<p><b>8</b></p>
</td>
<td style='border: 1px solid black;'>
<p><b>Vision deficiencies or impairment.</b></p>
</td>
</tr>
</table>

<br>
<div>
<p><b>
    I fully understand the above and was under no pressure or obligation to sign this document.
</b></p>
<br>
<p><b>
    Signed at ________________________________ on this __________day of __
_______________20_________
</b></p>
</div>
<br>
";
echo $html;