<?php
include "../../root.class.php";
$db = new db_safeguard();

$date = date("Y-m-d H:i", strtotime("+2 Hours"));

echo "<br><br>";
echo "<br><br>";
echo "<br><br>";
var_dump($_POST);

echo "<br><br>";
echo "<br><br>";
echo "<br><br>";

$test_sec_id = $db->query("test_attempts", "INSERT INTO `test_attempts`(`test_id`, `user_id`, `name_surname`, `designation`, `booking_no`, `datetime`,`passmark`, , `test_name`, `test_description`) VALUES ('{$_POST['test_id']}','{$_SESSION['user_id']}','{$_POST['name_surname']}','{$_POST['designation']}','{$_POST['booking_no']}','{$_POST['datetime']}','{$_POST['passmark']}','{$_POST['test_name']}','{$_POST['test_desc']}')");

echo "<br><br>";

echo "INSERT INTO `test_attempts`(`test_id`, `user_id`, `name_surname`, `designation`, `booking_no`, `datetime`,`passmark`, , `test_name`, `test_description`) VALUES ('{$_POST['test_id']}','{$_SESSION['user_id']}','{$_POST['name_surname']}','{$_POST['designation']}','{$_POST['booking_no']}','{$_POST['datetime']}','{$_POST['passmark']}','{$_POST['test_name']}','{$_POST['test_desc']}')";

echo "<br>test_sec_id : " . $test_sec_id . "<br>";
echo "<br><br>";

$db->query("test_timeline", "INSERT INTO `test_timeline`(`test_sec_id`,`booking_no`, `datetime_start`, `datetime_stop`) VALUES ('$test_sec_id','{$_POST['booking_no']}','{$_POST['datetime_start']}',{$date})");

echo "INSERT INTO `test_timeline`(`test_sec_id`,`booking_no`, `datetime_start`, `datetime_stop`) VALUES ('$test_sec_id','{$_POST['booking_no']}','{$_POST['datetime_start']}',{$date})";

echo "<br><br>";

$timeline_calcs = $db->query('test_timeline', "SELECT * FROM `test_timeline` WHERE `test_sec_id` = $test_sec_id ORDER BY `test_sec_id` DESC LIMIT 1");
$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->query("test_timeline", "UPDATE `test_timeline` SET `duration` = '['$hours:$minutes:$seconds']' WHERE `test_sec_id` = $test_sec_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['question_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')";

    $question_id = $db->query("tests_questions_sec", "INSERT INTO `tests_questions_sec` (`tests_sec_id`,`questions`,`subtotal`) VALUES ($test_sec_id,'{$_POST[$q_index . '_section_name']}','0')");

    echo "<br>question_id : " . $question_id;

    $question_index = 0;
    $question_results = 0;
    $correct_answers_total = 0;
    $correct_selected_answers = 0;

    while ($question_index <= ($_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`, `options`) VALUES ($question_id,'{$_POST[$q_index . '_question_' . $question_index . '_answer']}','{$_POST[$q_index . '_question_' . $question_index . '_options']}')";

        echo "<br>" . $answer = $_POST[$q_index . '_question_' . $question_index];
        echo "<br> SELECTED : " . $test_option = $_POST[$q_index . '_option_' . $question_index];

        $db->query("tests_answers_sec", "INSERT INTO `tests_answers_sec` (`tests_questions_sec_id`, `answer`, `options`) VALUES ($question_id,'{$_POST[$q_index . '_question_' . $question_index . '_answer']}','{$_POST[$q_index . '_question_' . $question_index . '_options']}')");

        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) {
            $question_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;
    $db->query("tests_questions_sec", "UPDATE `tests_questions_sec` SET `subtotal` = '$subtotal' WHERE `record_id` = $question_id");
    echo "<br><br>";
    $q_index++;

    if($question_results == $question_index){
        $marks++;
    }

    echo "<br>question_results : " . $question_results . "<br>";
    echo "<br>question_index : " . $question_index . "<br>";

}

echo "$results : results <br>";
echo "$index : index <br>";

echo "correct_answers_total_complete : " . $correct_answers_total_complete . "<br>";
echo "correct_selected_answers_complete : " . $correct_selected_answers_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 "<br>C";

} else {
    $db->Update('tests_sec', ['results', 'current_mark'], ['NYC', $current_mark], "record_id = $test_sec_id");
    echo "<br>NYC";
}

// echo "<script>window.location.href='../training/attempt_test.php?booking_no={$_POST['booking_no']}'; </script>";