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

$bookings_res = $db->query("bookings", "SELECT * FROM bookings WHERE booking_no='{$_GET['booking_no']}'");

$submit_btn = new input();
$submit_btn->class("submit_btn");
$submit_btn->value("Submit");
$submit_btn->onclick("complete_test()");

?>
<style>
    body {
        background-image: url('');
        /* background-color: white; */
        background-repeat: repeat-y;
    }

    table {
        width: 95%;
        border-collapse: collapse;
    }

    th,
    td {
        padding-left: 10px;
        padding-right: 10px;
        border: 3px solid black;
        text-align: left;
        background-color: white;
    }

    .change_btn {
        font-size: 1em;
        margin: 1vw;
        cursor: pointer;
        border: 3px solid green;
        background-color: green;
        color: white;
        border-radius: 15px;
        padding: 1.2vw;
        /* height: 6vw; */
        width: 70%;
        /* margin-bottom: 4vw; */
        text-align: center;
        max-width: 250px;
    }

    .change_btn:hover {
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        transform: scale(1.05);
        background-color: #008000b5;
        border: 3px solid #008000b5;
        color: white;
    }


    .change_btn1 {
        font-size: 1em;
        margin: 1vw;
        cursor: pointer;
        border: 3px solid red;
        background-color: red;
        color: white;
        border-radius: 15px;
        padding: 1.2vw;
        /* height: 6vw; */
        width: 70%;
        /* margin-bottom: 4vw; */
        text-align: center;
        max-width: 250px;
    }

    .change_btn1:hover {
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        transform: scale(1.05);
        background-color: #ff00006b;
        border: 3px solid #ff00006b;
        color: white;
    }

    .change_btn2 {
        font-size: 1em;
        margin: 0.5vw;
        cursor: pointer;
        border: 3px solid grey;
        background-color: grey;
        color: white;
        border-radius: 15px;
        padding: 1.2vw;
        /* height: 6vw; */
        width: 70%;
        /* margin-bottom: 4vw; */
        text-align: center;
        max-width: 250px;
    }

    .change_btn2:hover {
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        transform: scale(1.05);
        background-color: #8080809e;
        /* border: 3px solid #DB8E63; */
        color: white;
    }
</style>

<div class="form_down">
    <h1>ATTEMPT TEST</h1>
    <table style='width:90%; text-align:center;'>
        <tr>
            <td style='font-size:1.5vw;'>
                TEST NAME:
            </td>
            <td style='font-size:1.5vw;'>
                DESCRIPTION:
            </td>
            <td style='font-size:1.5vw;'>
                PASSMARK:
            </td>
            <td style='font-size:1.5vw;'>
                ASSIGNED TO:
            </td>
            <td style='font-size:1.5vw;'>
                DATE BOOKED:
            </td>
            <td>

            </td>
        </tr>

        <?php
        $test_index = 0;
        $index = 1;
        if ($bookings_res->num_rows > 0) {
            while ($bookings = $bookings_res->fetch_assoc()) {

                $test_res = $db->query("tests", "SELECT * FROM tests WHERE record_id='{$bookings['test_id']}'");
                $test_data = $test_res->fetch_assoc();
                $results_index = 0;
                $passed = 0;

                ?>
                <tr>
                    <td>
                        <?php echo $test_data['name'] ?>
                    </td>
                    <td>
                        <?php echo $test_data['test_description'] ?>
                    </td>
                    <td>
                        <?php echo $test_data['passmark'] ?>
                    </td>
                    <td>
                        <?php echo $bookings['assigned_to'] ?>
                    </td>
                    <td>
                        <?php echo $bookings['date_booked'] ?>
                    </td>
                    <?php
                    $test_sec = $db->query("test_attempts", "SELECT * FROM test_attempts WHERE booking_no='{$_GET['booking_no']}'");

                    if ($test_sec->num_rows != 0) {
                        while ($test_a = $test_sec->fetch_assoc()) {
                            ?>
                            <input type='text' name='attendince_<? echo $index; ?>' id='attendince_<? echo $index; ?>' hidden
                                value='YES' class='input' />
                            <div class='button' id='attendance_<? echo $index; ?>' hidden style='background-color:green;'>YES
                            </div>
                            <br>
                            <?php if ($test_a['results'] == 'C') {
                                $passed = 1;
                                ?>
                                <br>
                                <div class='button' id='start_test_<? echo $index; ?>'
                                    onclick="open_test_pdf(<?php echo $test_a['record_id']; ?>)" style='background-color:GREEN;'>PASSED
                                </div>

                            <?php } else { ?>

                                <div class='button' id='start_test_<? echo $index; ?>'
                                    onclick="open_test_pdf(<?php echo $test_a['record_id']; ?>)" style='background-color:RED;'>FAILED</div>

                            <?php } ?>

                            <?php
                            $results_index++;
                        }

                        if ($results_index == 1 && $passed == 0) {
                            ?>

                            <br>
                            <div class='button' onclick='start_test(<? echo $index; ?>,1)' id='start_test_<? echo $index; ?>'
                                style='background-color:orange;'>2nd ATTEMPT</div>

                            <?php
                        }

                        if ($results_index == 2 && $passed == 0) {
                            ?>
                            <br>
                            <div class='button' onclick='start_test(<? echo $index; ?>,1)' id='start_test_<? echo $index; ?>'
                                style='background-color:orange;'>3rd ATTEMPT</div>

                        <?php }
                    } else {

                        ?>
                        <!-- <br> -->
                        <td>
                            <input type='text' name='attendince_<? echo $index; ?>' id='attendince_<? echo $index; ?>' hidden
                                value='NO' class='input' />
                            <div class='change_btn2' onclick='mark_test_attendance(<? echo $index; ?>)'
                                id='attendance_<? echo $index; ?>'>ATTEMPT</div>
                            <div class='submit_btn' onclick='start_test(<? echo $index; ?>)' id='start_test_<? echo $index; ?>'
                                hidden>START TEST
                            </div>
                        </td>
                        <?php
                    }

                    ?>
                </tr>


                <!-- <tr>
                        <td> </td>
                        <td> </td>
                        <td> No Bookings Found </td>
                        <td> </td>
                        <td> </td>
                    </tr> -->
                <?php
            }
            $test_index++;
        }
        ?>
    </table>
    <br>
    <?php
    $bookings_res = $db->query("bookings", "SELECT * FROM bookings WHERE booking_no='{$_GET['booking_no']}'");
    $booking = $bookings_res->fetch_assoc();

    $tests_data_res = $db->query("tests", "SELECT * FROM tests WHERE record_id='{$booking['test_id']}'");
    $tests_data = $tests_data_res->fetch_assoc();

    $test_id = $tests_data['record_id'];

    $record_id = new input();
    $record_id->name("record_id");
    $record_id->id("record_id");
    $record_id->value($test_id);
    $record_id->type("hidden");

    $record_id->add();
    $submit_btn->add();
    ?>
    <br>
</div>

<script>

    function start_test(index, attempt = 0) {

        document.getElementById('record_id').value = index;

        window.location.href = "test.php?booking_id=<? echo $_GET['booking_no']; ?>" + "&attempt=" + attempt + "&record_id=" + index + "&passmark=" + <?php echo $tests_data['passmark'] ?> + "&test_name=" + "<?php echo $tests_data['name'] ?>" + "&test_desc=" + "<?php echo $tests_data['test_description'] ?>";
    }

    function mark_test_attendance(ind) {
        if (document.getElementById('attendance_' + ind).className == 'change_btn2') {
            document.getElementById('attendance_' + ind).className = 'change_btn';
            document.getElementById('attendince_' + ind).value = 'YES';
            document.getElementById('attendance_' + ind).innerHTML = 'YES';
            document.getElementById('start_test_' + ind).hidden = false;
        } else {
            document.getElementById('attendance_' + ind).className = 'change_btn2';
            document.getElementById('attendince_' + ind).value = 'ATTEMPT';
            document.getElementById('attendance_' + ind).innerHTML = 'ATTEMPT';
            document.getElementById('start_test_' + ind).hidden = true;
        }
    }

    function open_test_pdf(id) {
        document.getElementById('pdf_div').style.display = 'block';
        document.getElementById('pdf_frame').src = "test.html.php?id=" + id;
    }
</script>