<?php
include "../../root.class.php";
$db = new db_safeguard();

echo "<br>name: " . $_POST['name'];
echo "<br>info: " . $_POST['test_description'];
echo "<br>passmark: " . $_POST['passmark'];
echo "<br>Sections total: " . $_POST['question_total'];

echo "<br><br>";

echo "<br>test_id: " . $_POST['test_id'];

var_dump($_POST);

echo "<br><br>";

if ($_POST['passmark'] == "") {
    $test_id = $_POST['test_id'];
    echo "<br>test_id: " . $_POST['test_id'] . "<br>";

    $update_test = $db->query("tests", " UPDATE tests SET name = '{$_POST['name']}', test_description = '{$_POST['test_description']}' WHERE record_id = {$_POST['test_id']}");

    echo "UPDATE tests SET name = '{$_POST['name']}', test_description = '{$_POST['test_description']}' WHERE record_id = {$_POST['test_id']}";
} else {
    $test_id = $_POST['test_id'];
    echo "<br>test_id: " . $_POST['test_id'] . "<br>";

    $update_test = $db->query("tests", " UPDATE tests SET name = '{$_POST['name']}', test_description = '{$_POST['test_description']}', passmark = '{$_POST['passmark']}' WHERE record_id = {$_POST['test_id']}");

    echo "UPDATE tests SET name = '{$_POST['name']}', test_description = '{$_POST['test_description']}', passmark = '{$_POST['passmark']}' WHERE record_id = {$_POST['test_id']}";
}

echo "<br>update_test: " . $update_test . "<br>";

$delete_questions = $db->query("test_questions", "DELETE FROM test_questions WHERE test_id = {$test_id}");

echo "<br>delete_questions: " . $delete_questions . "<br>";

$index = 0;

while ($index <= $_POST['question_total']) {
    echo "<br> I: " . $index;
    echo "<br>question_name: " . $_POST["question_" . $index . "_name"];
    echo "<br>question_id: " . $_POST["question_" . $index . "_id"];
    $question_name = $_POST["question_" . $index . "_name"];
    $question_record_id = $_POST["question_" . $index . "_id"];

    if (strlen($question_name) != 0) {
        echo "<br> I: " . $index;
        // echo "<br>question_name: " . $question_name;

        $db->query("test_questions", "UPDATE test_questions SET `question_name` = '{$question_name}' WHERE `record_id` = '{$question_record_id}'");

        echo "<br> UPDATE test_questions SET `question_name` = '{$question_name}' WHERE `record_id` = '{$question_record_id}' <br>";

        $question_id = $db->query("test_questions", "INSERT INTO `test_questions` (`test_id`, `question_name`) VALUES ('{$test_id}', '{$question_name}')");

        echo "<br> INSERT INTO `test_questions` (`test_id`, `question_name`) VALUES ('{$test_id}', '{$question_name}') <br>";

        echo "<br>test_question_id: " . $question_id . "<br>";

        $db->query("", "DELETE FROM `test_answers` WHERE `test_question_id` = '{$_POST["question_" . $index . "_id"]}'");

        echo "<br> DELETE FROM `test_answers` WHERE `test_question_id` = '{$_POST["question_" . $index . "_id"]}' <br>";

        $a_index = 0;
        while ($a_index <= $_POST['row_count_' . $index]) {
            echo "<br> A: " . $a_index;
            echo "<br>answer: " . $_POST[$index . "_answer_$a_index"];
            echo "<br>option: " . $_POST[$index . "_answer_option_$a_index"];
            $answer = $_POST[$index . "_answer_$a_index"];
            $option = $_POST[$index . "_answer_option_$a_index"];

            if (strlen($answer) != 0) {
                echo "<br> A: " . $a_index;

                $db->insert("INSERT INTO `test_answers` (`test_question_id`,`answer`,`options`) VALUES ($question_id,'$answer','$option')");

                echo "<br> INSERT INTO `test_answers` (`test_question_id`,`answer`,`options`) VALUES ($question_id,'$answer','$option')" . "<br>";

                echo "<br>answer: " . $answer . "<br>";
                echo "<br>option: " . $option . "<br>";
            } else {
                echo "<br> SKIPPED answer : " . $answer;
                echo "<br> SKIPPED ". $index . "_answer_option_$a_index";
                echo "<br> SKIPPED option: " . $option . "<br><br><br>";
            }
            $a_index++;
        }

    } else {
        echo "<br>question_name: " . $question_name . "<br>";
    }
    $index++;
}

// echo "<script>window.location.href = '../training/tests.php';</script>";