<?php
include $_SERVER['DOCUMENT_ROOT'] . "/WebApp/assesor/html.class.php";
$db = new DBMain();

echo "<br><br>";
echo "<br><br>";
echo "<br><br>";
var_dump($_POST);

echo "<br><br>";
echo "<br><br>";
echo "<br><br>";
$date = date('Y-m-d H:i', strtotime("+ 2 Hours"));

$target_dir = "../uploads/";
$target_file = $target_dir . "{$_POST['client_employees_id']}-image-" . basename($_FILES["fileToUpload"]["name"]);
$FILE_NAME = "{$_POST['client_employees_id']}-image-" . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$uploadOk = 1;

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

    echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";

} else {
    echo "Sorry, there was an error uploading your file.";
}

$test_sec_id = $db->insert("INSERT INTO `tests_sec`(`test_id`, `clients_id`, `client_employees_id`, `safesure_users_id`, `results`, `date`, `booking`, `test_timeline`, `passmark`, `current_mark`, `test_name`, `test_info`, `nqf_level`, `credits`, `notes`, `special_instructions`, `image_file_path`, `vehicle_model`) VALUES ('{$_POST['test_id']}','{$_POST['clients_id']}','{$_POST['client_employees_id']}','{$_SESSION['user_id']}','NYC','$date','{$_POST['booking_id']}','{$_POST['timeline_id']}','{$_POST['passmark']}','{$_POST['current_mark']}','{$_POST['test_name']}','{$_POST['test_info']}','{$_POST['nqf_level']}','{$_POST['credits']}','{$_POST['notes']}','{$_POST['special_instructions']}','$FILE_NAME','{$_POST['vehicle_model']}')");

echo "test_sec_id : " . $test_sec_id;

$db->Update('test_timeline', ['datetime_stop'], ["$date"], "record_id = {$_POST['timeline_id']}");

echo "'test_timeline', ['datetime_stop'], ['" . $date . "'], 'record_id = {$_POST['timeline_id']}';";

$timeline_calcs = $db->exec_query('test_timeline', ['*'], '', '', '', ',', '', "record_id = {$_POST['timeline_id']}");
$timeline = $timeline_calcs->fetch_assoc();
$time_start = $timeline['datetime_start'];
$time_stop = $timeline['datetime_stop'];

$duration = strtotime($time_stop) - strtotime($time_start);
$hours = floor($duration / 3600);
$minutes = floor(($duration / 60) % 60);
$seconds = $duration % 60;
echo "<br> Duration = " . $hours . " hours " . $minutes . " minutes " . $seconds . " seconds";
echo $duration . " : duration";

// $db->Update('test_timeline', ['t_delta'], ["$hours:$minutes:$seconds"], "record_id = {$_POST['timeline_id']}");
echo "'test_timeline', ['t_delta'], ['$hours:$minutes:$seconds'], 'record_id = {$_POST['timeline_id']}';";



$results = 0;
$index = 0;
$q_index = 0;
$marks = 0;
$total_possible_answers = 0;
$total_correct_answers = 0;
$correct_answers_total_complete = 0;
$correct_selected_answers_complete = 0;
while ($q_index <= ($_POST['section_count'] - 1)) {
    echo "<br>question index : " . $q_index;
    echo "<br>question_count : " . $_POST['section_count'];
    echo $_POST[$q_index . 'questions'];
    echo "<br>" . "INSERT INTO `tests_questions_sec` (`tests_sec_id`,`questions`,`subtotal`) VALUES ($test_sec_id,'{$_POST[$q_index . '_section_name']}','0')";

    $section_id = $db->insert("INSERT INTO `tests_questions_sec` (`tests_sec_id`,`questions`,`subtotal`) VALUES ($test_sec_id,'{$_POST[$q_index . '_section_name']}','0')");

    echo "<br> questions" . " : " . $_POST[$q_index . 'question'];

    echo "<br>section_id : " . $section_id;

    $question_index = 0;
    $section_results = 0;
    $correct_answers_total = 0;
    $correct_selected_answers = 0;

    while ($question_index <= ($_POST[$q_index . '_question_count'] - 1)) {

        // echo "<br> _question_count : "($_POST[$q_index . '_question_count'] - 1);

        echo "<br> answer_index : " . $question_index;
        echo "<br> answer_count : " . $num_questions = $_POST[$q_index . '_question_count'];
        echo "<br>" . "INSERT INTO tests_answers_sec (tests_questions_sec_id,answer,comment,option) VALUES ('{$section_id}','{$_POST[$q_index . '_question_' . $question_index]}','{$_POST[$question_index . '_comment_' . $question_index]}','{$_POST[$q_index . '_option_' . $question_index]}')";

        echo "<br>" . $answer = $_POST[$q_index . '_question_' . $question_index];
        echo "<br> SELECTED : " . $test_option = $_POST[$q_index . '_option_' . $question_index];
        echo "<br>" . $comment = $_POST[$q_index . '_comment_' . $question_index];

        $db->insert("INSERT INTO tests_answers_sec (tests_questions_sec_id,answer,comment,option) VALUES ('{$section_id}','{$_POST[$q_index . '_question_' . $question_index]}','{$_POST[$question_index . '_comment_' . $question_index]}','{$_POST[$q_index . '_option_' . $question_index]}')");

        echo "<br>db_option :  " . $db_correct_answer = $_POST[$q_index . '_check_' . $question_index];

        if ($test_option == $db_correct_answer) {
            echo "<br>Correct Section" . "<br>";
            $correct_selected_answers++;
            echo "<br>nice" . "<br>";
        } else {
            echo "<br>Wrong Section" . "<br>";
            // break;
        }

        if ($_POST[$q_index . '_option_' . $question_index] == 1) {
            $section_results++;
            $results++;
        }
        $correct_answers_total++;
        $question_index++;
        $index++;
    }

    $correct_selected_answers_complete += $correct_selected_answers;
    $correct_answers_total_complete += $correct_answers_total;
    $subtotal = $correct_answers_total . "-" . $correct_selected_answers;
    echo "<br> subtotal" . $subtotal;
    echo "<br> total_correct_answers" . $total_correct_answers;
    $db->Update('tests_questions_sec', ['subtotal'], [$subtotal], "record_id = $section_id");
    echo "";
    $q_index++;


    if ($section_results == $question_index) {
        $marks++;
    }

    echo "<br>section_results : " . $section_results;
    echo "<br>question_index : " . $question_index;

}

echo "$results : results <br>";
echo "$index : index <br>";

echo $correct_selected_answers_complete . " : correct_selected_answers_complete <br>";
echo $correct_answers_total_complete . " : correct_answers_total_complete <br>";

echo $current_mark = round((($correct_selected_answers_complete / $correct_answers_total_complete)),2);
if ($correct_selected_answers_complete == $correct_answers_total_complete) {
    $db->Update('tests_sec', ['results', 'current_mark'], ['C', $current_mark], "record_id = $test_sec_id");
    echo "C";

} else {
    $db->Update('tests_sec', ['results', 'current_mark'], ['NYC', $current_mark], "record_id = $test_sec_id");
    echo "NYC";
}

echo "<script>window.location.href='/WebApp/assesor/attendance_registar.php?record_id={$_POST['booking_id']}';</script>";