<?php include $_SERVER['DOCUMENT_ROOT'] . "/WebApp/assesor/html.class.php";
$db = new DBMain();

$booking_res = $db->exec_query('bookings', ['*'], '', '', '', '', "record_id = '{$_GET['booking_id']}'");
// echo "SELECT * FROM bookings WHERE record_id = '{$_GET['booking_id']}'";
$booking = $booking_res->fetch_assoc();

$asses_res = $db->exec_query('assessments', ['*'], '', '', '', '', '1', "ORDER BY assessments_name ASC");
$assessments_data = "<option></option>";
while ($ass = $asses_res->fetch_assoc()) {
    $assessments_data = $assessments_data . "<option value='{$ass['record_id']}'>{$ass['assessments_name']}</option>'";
}

$test_res = $db->exec_query('tests', ['*'], '', '', '', '', '1', "ORDER BY test_name ASC");
$tests_data = "<option></option>";
while ($test = $test_res->fetch_assoc()) {
    $tests_data = $tests_data . "<option value='{$test['record_id']}'>{$test['test_name']}</option>'";
}

$WebApp->pageHeading('ASSESOR - LICENSE');
?>

<iframe id="preload_frame" style="display:none;"></iframe>

<div id="pdf_div" style="display:none;background-color:grey;position:absolute;z-index:999;
    left:5%;top:3%;width:90%;height:100%;cursor:move;touch-action:pan-y;
    display:none;">

    <div style='position:absolute;z-index:9999;right:1%;top:1%;color:white;font-size:50px;cursor:pointer;'
        onclick="close_pdf_div()">X</div>

    <iframe id="pdf_frame" style="width:90%;height:90%;position:absolute;z-index:99;
        left:5%;top:3%;background-color:white;"></iframe>
</div>

<div class='form_container'>
    <?php
    $clients_id = $booking['clients_id'];
    ?>
    <form action='complete_booking.php' method="POST">
        <input type='text' name="booking_id" hidden value='<?php echo $_GET['booking_id']; ?>' />

        <hr><br>
        <table id='content_table'>
            <tr>
                <th style="font-size: 1vw;">
                    NAME & SURNAME
                </th>
                <th style="font-size: 1vw;">
                    INDUCTION DATE
                </th>
                <th style="font-size: 1vw;">
                    ASSESSMENT
                </th>
                <th style="font-size: 1vw;">
                    TEST
                </th>
            </tr>

            <?php
            $ems = explode(',', substr($booking['client_employees'], 1));
            $ass = explode(',', substr($booking['assessments'], 1));
            $tests = explode(',', substr($booking['tests'], 1));
            // echo "Employees no: " . $ems[0] . "<br>";
            // echo "Assessments no: " . $ass[0] . "<br>";
            // echo "Test no: " . $tests[1] . "<br>";
            
            $index = 1;
            $index_ass_tests = 0;

            foreach ($ems as $emp) {
                if (strlen($emp) > 0) {
                    $emp_res = $db->exec_query('client_employees', ['*'], '', '', '', '', "record_id = $emp");
                    // echo "SELECT * FROM client_employees WHERE record_id = $emp";
                    $employee = $emp_res->fetch_assoc();
                    ?>
                    <tr>
                        <td>
                            <input type="text" readonly name="employee_name_surname_<?php echo $index; ?> "
                                id="employee_name_surname_<?php echo $index; ?> "
                                value="<?php echo $employee['client_employees_name']; ?>  <?php echo $employee['surname']; ?>"
                                class="inputs" style="font-size: 1vw;">
                        </td>
                        <td>
                            <input type="text" readonly name="induction_date_<?php echo $index; ?> "
                                id="induction_date_<?php echo $index; ?> " class="inputs"
                                value="<?php echo $booking['induction_date'] ?>" style="font-size: 1vw;">
                        </td>
                        <!-- <td>
                                <input type="text" readonly name="medical_date_<?php echo $index; ?> "
                                    id="medical_date_<?php echo $index; ?> " class="inputs"
                                    value="<?php echo $booking['medical_date'] ?>" style="font-size: 1vw;">
                            </td>
                            <td>
                                <input type="text" readonly name="industry_<?php echo $index; ?> "
                                    id="industry_<?php echo $index; ?> " class="inputs"
                                    value="<?php echo $booking['industry']; ?>" style="font-size: 1vw;">
                            </td> -->
                        <td>
                            <select disabled id='assessment_<? echo $index; ?>' name='assessment_<? echo $index; ?>' readonly
                                class='select'><? echo $assessments_data; ?>
                            </select>

                            <script>
                                document.getElementById('assessment_<? echo $index; ?>').value = <?php echo (strlen($ass[$index_ass_tests]) == 0 ? 0 : $ass[$index_ass_tests]); ?>;
                            </script>
                        </td>
                        <td>
                            <select disabled id='test_n<? echo $index; ?>' name='test_n<? echo $index; ?>' readonly
                                class='select'><? echo $tests_data; ?>
                            </select>
                            <script>
                                document.getElementById('test_n<? echo $index; ?>').value = <?php echo (strlen($tests[$index_ass_tests]) == 0 ? 0 : $tests[$index_ass_tests]); ?>;
                            </script>
                        </td>
                        <td>

                            <div class='button' id='license_html_<?php echo $index; ?>'
                                onclick="open_license_html(<?php echo $_GET['booking_id']; ?>)">
                                VIEW LICENSE</div>

                        </td>
                        <td>
                            <div class="button" id="license_pdf_<?php echo $index; ?> " onclick="next()">PDF
                            </div>
                        </td>
                    </tr>
                    <?php

                    $index_ass_tests++;
                    $index++;
                }
            }
            ?>
        </table>
        <?php

        ?>
        <br>
        <button class='button' name='save'>MARK AS COMPLETE</button>

        <script>
            function open_license_html(id) {
                const preload = document.getElementById("preload_frame");
                const popup = document.getElementById("pdf_div");
                const view = document.getElementById("pdf_frame");

                const url = "license.html.php?booking_id=" + id;

                preload.src = url;

                preload.onload = function () {
                    popup.style.display = "block";
                    view.src = url;
                };
            }

            function next() {
                window.location.href = "license_pdf.php?booking_id=<?php echo $_GET['booking_id']; ?>";
            }

            function close_pdf_div() {
                document.getElementById('pdf_div').style.display = 'none';
                document.getElementById('pdf_frame').src = "";
            }
        </script>
    </form>
</div>

<!-- 2992.50 -->