<?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 status ='ATTEMPT'");

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 = $test_res->fetch_assoc();

        $table_data = $table_data . "
            <tr>
                <td> {$bookings['booking_no']} </td>
                <td> {$bookings['date_booked']} </td>
                <td> {$bookings['assigned_to']} </td>
                <td> {$test['name']} </td>
                <td> {$bookings['status']} </td>
                <td> 
                <button 
                class='submit_btn' 
                id='submit_btn'
                onclick='attempt_tests()'
                booking_no='{$bookings['booking_no']}'>
                ATTEMPT
                </button> 
                </td>
            </tr>
        ";
    }

} else {
    $table_data = $table_data . "
        <tr>
            <td> </td>
            <td> </td>
            <td> No Bookings Found </td>
            <td> </td>
            <td> </td>
        </tr>
    ";
}

?>

<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;
    }
</style>

<div class="form_down">
    <h1>CURRENT BOOKINGS</h1>
    <table style='width:90%; text-align:center;'>
        <tr>
            <td style='font-size:1.5vw;'>
                BOOKING NO:
            </td>
            <td style='font-size:1.5vw;'>
                DATE BOOKED:
            </td>
            <td style='font-size:1.5vw;'>
                ASSIGNED TO:
            </td>
            <td style='font-size:1.5vw;'>
                TEST:
            </td>
            <td>
                STATUS:
            </td>
            <td>

            </td>
        </tr>

        <?php
        echo $table_data;
        ?>
    </table>
    <br>
    <br>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function () {
        document.querySelectorAll(".submit_btn").forEach(function (btn) {
            btn.addEventListener("click", function () {
                let booking_no = this.getAttribute("booking_no");
                window.location.href = '../training/attempt_test.php?booking_no=' + booking_no;
            });
        });
    });
</script>