<?php
include $_SERVER['DOCUMENT_ROOT'] . "/WebBuilder/WebApp.class.php";
$WebApp->pageHeading('TEST RESULTS');
$db = new DBMain();

$test_id = 1;

//show the test results
$test_res = $db->exec_query('tests', ['*'], '', '', '', '', "record_id = $test_id");
$tests = $test_res->fetch_assoc();

// echo "<br>Test id: " . $test_id;
// echo "<br>Test name: " . $_POST['test_name'];
// echo "<br>description: " . $_POST['test_description'];
// echo "<br>nqf: " . $_POST['nqf_level'];
// echo "<br>credits: " . $_POST['credits'];
// echo "<br>Passmark: " . $_POST['passmark'];
// echo "<br>sections total: " . $section_total = $_POST['sections_total'] . "<br><br>";
// echo "<br>question total: " . $section_total . "<br><br>";

$sections_res = $db->exec_query('test_sections', ['*'], '', '', '', '', "test_id = $test_id", "ORDER BY record_id ASC");

$index = 0;
$marks = 0;
$total_possible_answers = 0;
$total_correct_answers = 0;
// fetch sections fetch loop questions fetch loop answers

while ($section = $sections_res->fetch_assoc()) {
    "<br>Section: " . $section['section_name'] . "<br>";
    $question_res = $db->exec_query('test_questions', ['*'], '', '', '', '', "test_section_id = {$section['record_id']}", "ORDER BY record_id ASC");

    $q_index = 0;
    while ($question = $question_res->fetch_assoc()) {
        "<br>Question: " . $question['question'] . "<br>";
        $answer_res = $db->exec_query('test_answers', ['*'], '', '', '', '', "test_question_id = {$section['record_id']}", "ORDER BY record_id ASC");

        $a_index = 0;
        $total_possible_answers += $answer_res->num_rows;
        while ($answer = $answer_res->fetch_assoc()) {
            "<br>total answers for question:" . $answer_res->num_rows;
            "<br>Answer: " . $answer['answer'];
            "<br>Answer value: " . $answer['option'];
            "<br>Current value: " . $_POST[$index . "_change_value_$a_index"] . "<br>";
            if ($answer['option'] == 1) {
                $total_correct_answers++;
            }
            $current_a = $_POST[$index . "_change_value_$a_index"];

            if (($answer['option'] == 1) && ($current_a == 1)) {
                "<br>Correct";
                $marks++;
            } else {
                "<br>Incorrect";
            }
            $a_index++;
        }
        $q_index++;
    }
    $index++;
}



"<br>Total answers: " . $total_possible_answers . "<br>";
"<br>Total correct answers: " . $total_correct_answers . "<br>";

$count = 0;

"<br>Num_correct_a: " . $count . "<br>";

$test_score = ($marks / $total_correct_answers) * 100;

$new_test_score = round($test_score, 0);

"<br>Score: " . $test_score . "<br>";

?>

<style>
    .sticky_button {
        position: fixed;
        top: 2.5%;
        right: 5%;
        z-index: 10;
        background-color: #aa9104;
        border: none;
        color: white;
        padding: 32px 64px;
        text-decoration: none;
        cursor: pointer;
        border-radius: 15px;
        font-size: 16px;
        transition: background-color 0.2s ease;

    }

    .sticky_2 {
        position: fixed;
        top: 2.5%;
        left: 5%;
        z-index: 10;
        background-color: #aa9104;
        border: none;
        color: white;
        padding: 32px 64px;
        text-decoration: none;
        cursor: pointer;
        border-radius: 15px;
        font-size: 16px;
        transition: background-color 0.2s ease;

    }
</style>

<form>
    <div class='form_container' style='width:95%'>
        <input type='text' name='test_name' required class='input' style='width:100%;' placeholder="TEST NAME"
            value='<?php echo $test['test_name']; ?>' /><br>
        <input type='text' name='test_description' required class='input' style='width:100%;'
            placeholder="TEST DESCRIPTION" value='<?php echo $test['test_description']; ?>' /><br>
        <div style='display:flex; justify-content: center;'>
            <input type='text' name='nqf_level' required class='input' style='width:48%;' placeholder="NQF LEVEL"
                value='<?php echo $test['nqf_level']; ?>' />
            <input type='text' name='credits' required class='input' style='width:48%;' placeholder="Credits"
                value='<?php echo $test['credits']; ?>' /><br>
        </div>
        <br><br>
        <div style="display:flex; justify-content: center;">
            <input type="text" id="test_marks" name="test_marks" required class="input" style="width:30%;"
                placeholder="Test Marks" value="<?php echo $new_test_score; ?>">
            <select name='passmark' id='passmark' required class='input' style='width:30%;'>
                <option value='1'>100%</option>
                <option value='0.95'>95%</option>
                <option value='0.9'>90%</option>
                <option value='0.85'>85%</option>
                <option value='0.8'>80%</option>
                <option value='0.75'>75%</option>
            </select><br>
            <script>
                document.getElementById('passmark').value = '<?php echo $tests['passmark']; ?>';
            </script>
        </div>
        <br>
        <?php $WebApp->pageHeading('TEST QUESTIONS'); ?>
        <br>

        <?php
        $index = 0;

        $sections_res2 = $db->exec_query('test_sections', ['*'], '', '', '', '', "test_id = $test_id", "ORDER BY record_id ASC");
        while ($section = $sections_res2->fetch_assoc()) {
            $question_res = $db->exec_query('test_questions', ['*'], '', '', '', '', "test_section_id = {$section['record_id']}", "ORDER BY record_id ASC");

            $answer_res = $db->exec_query('test_answers', ['*'], '', '', '', '', "test_question_id = {$section['record_id']}", "ORDER BY record_id ASC");
            // if the answer in correct change the background color
            if (($answer['option'] == 1) && ($current_a == 1)) {
                echo "<div class='form_container' id='form_container_$index' style='width: 95%; margin-bottom: 5vw; background-color: green;'>";
                ?>
                <input type="number" id="row_count_<?php echo $index; ?>" hidden name="row_count_<?php echo $index; ?>"
                    value='<?php echo $question_res->num_rows; ?>'>
                <input type="text" name="section_<?php echo $index; ?>_name" required="" class="input"
                    placeholder="SECTION NAME" value='<?php echo $section['section_name']; ?>' style="width: 60%;">
                <input type="text" name="section_<?php echo $index; ?>_id" class="input"
                    value='<?php echo $section['record_id']; ?>' style="width: 60%;" hidden>
                <table id="table_<?php echo $index; ?>" style="width: 100%; text-align: center;">
                    <tr>
                        <td style="width: 90%;">QUESTION</td>
                    </tr>
                    <?php
                    $q_index = 0;
                    while ($question = $question_res->fetch_assoc()) { ?>
                        <tr>
                            <td style="width: 5%;" hidden>
                                <div onclick='remove_(<?php echo $q_index; ?>,<?php echo $index; ?>)' class='button'
                                    id='<?php echo $index; ?>_hide_<?php echo $q_index; ?>' style='background-color:green;'>X
                                </div>
                            </td>
                            <td style="width: 90%;">
                                <textarea name="<?php echo $index; ?>_question_<?php echo $q_index; ?>"
                                    id="<?php echo $index; ?>_question_<?php echo $q_index; ?>" class="input"
                                    style="width: 90%; margin-left: 2vw;"><?php echo $question['question']; ?></textarea>
                            </td>
                        </tr>
                        <?php
                        $q_index++;
                    } ?>
                    <tr>
                        <td style="width: 90%;">ANSWER</td>
                    </tr>
                    <?php $a_index = 0;
                    while ($answer = $answer_res->fetch_assoc()) { ?>
                        <tr>
                            <td style="width: 5%;" hidden>
                                <div onclick='remove_(<?php echo $a_index; ?>,<?php echo $index; ?>)' class='button'
                                    id='<?php echo $index; ?>_hide_<?php echo $a_index; ?>' style='background-color:green;'>X
                                </div>
                            </td>
                            <td style="width: 90%;">
                                <textarea name="<?php echo $index; ?>_answer_<?php echo $a_index; ?>"
                                    id="<?php echo $index; ?>_answer_<?php echo $a_index; ?>" class="input"
                                    style="width: 90%; margin-left: 2vw;"><?php echo $answer['answer']; ?></textarea>
                            </td>
                            <td style="width: 5%;">
                                <div hidden onclick='change_(<?php echo $a_index; ?>,<?php echo $index; ?>)' class='button'
                                    id='<?php echo $index; ?>_change_<?php echo $a_index; ?>'
                                    style="background-color:green; width: 40%; margin-right: 2vw;">X</div>
                                <input type="checkbox" id='<?php echo $index; ?>_change_<?php echo $a_index; ?>'
                                    name='<?php echo $index; ?>_change_<?php echo $a_index; ?>'
                                    onclick='change_(<?php echo $a_index; ?>,<?php echo $index; ?>)'
                                    style="width: 4vw;height: 4vw;">
                                <input type="text" id='<?php echo $index; ?>_change_value_<?php echo $a_index; ?>'
                                    name='<?php echo $index; ?>_change_value_<?php echo $a_index; ?>' value="0" class="button"
                                    hidden>
                            </td>
                            <td style="width: 5%;">
                                <div class='button' id='<?php echo $index; ?>_hide_<?php echo $a_index; ?>' hidden>X</div>
                            </td>
                        </tr>
                        <?php
                        $a_index++;
                    } ?>
                </table>
                <hr>
            </div>

            <?php
            } else {
                echo "<div class='form_container' id='form_container_$index' style='width: 95%; margin-bottom: 5vw; background-color: red;'>";

                ?>
            <input type="number" id="row_count_<?php echo $index; ?>" hidden name="row_count_<?php echo $index; ?>"
                value='<?php echo $question_res->num_rows; ?>'>
            <input type="text" name="section_<?php echo $index; ?>_name" required="" class="input" placeholder="SECTION NAME"
                value='<?php echo $section['section_name']; ?>' style="width: 60%;">
            <input type="text" name="section_<?php echo $index; ?>_id" class="input"
                value='<?php echo $section['record_id']; ?>' style="width: 60%;" hidden>
            <table id="table_<?php echo $index; ?>" style="width: 100%; text-align: center;">
                <tr>
                    <td style="width: 90%;">QUESTION</td>
                </tr>
                <?php
                $q_index = 0;
                while ($question = $question_res->fetch_assoc()) { ?>
                    <tr>
                        <td style="width: 5%;" hidden>
                            <div onclick='remove_(<?php echo $q_index; ?>,<?php echo $index; ?>)' class='button'
                                id='<?php echo $index; ?>_hide_<?php echo $q_index; ?>' style='background-color:red;'>X
                            </div>
                        </td>
                        <td style="width: 90%;">
                            <textarea name="<?php echo $index; ?>_question_<?php echo $q_index; ?>"
                                id="<?php echo $index; ?>_question_<?php echo $q_index; ?>" class="input"
                                style="width: 90%; margin-left: 2vw;"><?php echo $question['question']; ?></textarea>
                        </td>
                    </tr>
                    <?php
                    $q_index++;
                } ?>
                <tr>
                    <td style="width: 90%;">ANSWER</td>
                </tr>
                <?php $a_index = 0;
                while ($answer = $answer_res->fetch_assoc()) { ?>
                    <tr>
                        <td style="width: 5%;" hidden>
                            <div onclick='remove_(<?php echo $a_index; ?>,<?php echo $index; ?>)' class='button'
                                id='<?php echo $index; ?>_hide_<?php echo $a_index; ?>' style='background-color:red;'>X
                            </div>
                        </td>
                        <td style="width: 90%;">
                            <textarea name="<?php echo $index; ?>_answer_<?php echo $a_index; ?>"
                                id="<?php echo $index; ?>_answer_<?php echo $a_index; ?>" class="input"
                                style="width: 90%; margin-left: 2vw;"><?php echo $answer['answer']; ?></textarea>
                        </td>
                        <td style="width: 5%;">
                            <div hidden onclick='change_(<?php echo $a_index; ?>,<?php echo $index; ?>)' class='button'
                                id='<?php echo $index; ?>_change_<?php echo $a_index; ?>'
                                style="background-color:red; width: 40%; margin-right: 2vw;">X</div>
                            <input type="checkbox" id='<?php echo $index; ?>_change_<?php echo $a_index; ?>'
                                name='<?php echo $index; ?>_change_<?php echo $a_index; ?>'
                                onclick='change_(<?php echo $a_index; ?>,<?php echo $index; ?>)' style="width: 4vw;height: 4vw;">
                            <input type="text" id='<?php echo $index; ?>_change_value_<?php echo $a_index; ?>'
                                name='<?php echo $index; ?>_change_value_<?php echo $a_index; ?>' value="0" class="button" hidden>
                        </td>
                        <td style="width: 5%;">
                            <div class='button' id='<?php echo $index; ?>_hide_<?php echo $a_index; ?>' hidden>X</div>
                        </td>
                    </tr>
                    <?php
                    $a_index++;
                } ?>
            </table>
            <hr>
            </div>

            <?php
            }            ?>
        <?php $index++;
        } ?>