<?php include $_SERVER['DOCUMENT_ROOT'] . "/WebApp/assesor/html.class.php";
$db = new DBMain();

$clients_res = $db->exec_query('clients', ['*'], '', '', '', '', "1", "ORDER BY clients_name ASC");
$clients_data = '<option></option>';
while ($clients = $clients_res->fetch_assoc()) {

    $clients_data = $clients_data . "<option value='{$clients['record_id']}'>{$clients['clients_name']}</option>";
}

$users_res = $db->exec_query('safesure_users', ['*'], '', '', '', '', "user_type_id = 2", "ORDER BY safesure_users_name ASC");
$users_data = '<option></option>';
while ($users = $users_res->fetch_assoc()) {

    $users_data = $users_data . "<option value='{$users['record_id']}'>{$users['safesure_users_name']}</option>";
}

$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>'";
}

$booking_res = $db->exec_query('bookings', ['*'], '', '', '', '', "record_id = '{$_GET['record_id']}'");

$WebApp->pageHeading('ATTENDENCE REGISTER');

?>

<datalist name='employees' id='employees'></datalist>

<div style='background-color:grey;position:absolute;z-index:999;left:5%;top:3%;width:90%;height:100%;cursor:move;touch-action:pan-y'
    id='pdf_div' hidden>
    <div style='position:absolute;z-index:9999;right:1%;top:1%;color:white; font-size:50px'
        onclick="document.getElementById('pdf_div').style.display = 'none';">X</div>
    <iframe src="" 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
    while ($booking = $booking_res->fetch_assoc()) {
        $clients_id = $booking['clients_id'];
        ?>
        <form action='complete_booking.php' method="POST">
            <input type='text' name="booking_id" hidden value='<?php echo $_GET['record_id']; ?>' />
            <input type='text' name="client_id" hidden value='<?php echo $clients_id; ?>' />
            <hr><br>
            <table id='content_table'>
                <tr>
                    <th>
                        NAME (SEARCHABLE)
                        </td>
                    <th>
                        SURNAME
                        </td>
                    <th>
                        OCUPATION
                        </td>
                    <th>
                        ASSESSMENT
                        </td>
                    <th>
                        TEST
                    </th>
                    <th>
                        ATTENDANCE
                        </td>

                </tr>
                <? $ems = explode(',', substr($booking['client_employees'], 1));
                $ass = explode(',', substr($booking['assessments'], 1));
                $tests = explode(',', substr($booking['tests'], 1));
                $index = 1;
                $index_ass_tests = 0;

                foreach ($ems as $emp) {
                    if (strlen($emp) > 0) {
                        $emp_res = $db->exec_query('client_employees', ['*'], '', '', '', '', "record_id = $emp");
                        $employee = $emp_res->fetch_assoc();

                        ?>
                        <tr>
                            <td>
                                <input type='text' name='is_emp_<? echo $index; ?>' hidden id='is_emp_<? echo $index; ?>'
                                    value='<? echo $employee['record_id']; ?>' />
                                <input type='text' name='emp_name_<? echo $index; ?>' id='emp_name_<? echo $index; ?>'
                                    onchange='check_value(<? echo $index; ?>)' class='input' list="employees"
                                    value='<? echo $employee['client_employees_name']; ?>' />
                            </td>
                            <td>
                                <input type='text' name='emp_sur_<? echo $index; ?>' id='emp_sur_<? echo $index; ?>'
                                    value='<? echo $employee['surname']; ?>' class='input' />
                            </td>
                            <td>
                                <input type='text' name='emp_occ_<? echo $index; ?>' id='emp_occ_<? echo $index; ?>'
                                    value='<? echo $employee['occupation']; ?>' class='input' />
                            </td>
                            <td>
                                <select id='assessment_<? echo $index; ?>' name='assessment_<? echo $index; ?>' disabled
                                    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 id='test_n<? echo $index; ?>' name='test_n<? echo $index; ?>' disabled
                                    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>
                                <?php
                                $results_index = 0;
                                $passed = 0;
                                // var_dump($ass);
                                // var_dump($tests);'
                                if (strlen($ass[$index_ass_tests]) >= 1) {
                                    $assesses = $db->exec_query('assesses', ['*'], '', '', '', '', "booking = {$_GET['record_id']} AND assessments_id = {$ass[$index_ass_tests]} AND client_employees_id = {$employee['record_id']}", "order by record_id ASC");

                                    $asses_res = $db->exec_query('assessments', ['*'], '', '', '', '', '1', "ORDER BY assessments_name ASC");

                                    if ($assesses->num_rows != 0) {
                                        while ($assessment = $assesses->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 ($assessment['results'] == 'C') {
                                                $passed = 1;
                                                ?>
                                                <br>
                                                <div class='button' id='start_assessment_<? echo $index; ?>'
                                                    onclick="open_assessment_pdf(<?php echo $assessment['record_id']; ?>)"
                                                    style='background-color:GREEN;'>PASSED</div>

                                            <?php } else { ?>

                                                <div class='button' id='start_assessment_<? echo $index; ?>'
                                                    onclick="open_assessment_pdf(<?php echo $assessment['record_id']; ?>)"
                                                    style='background-color:RED;'>FAILED</div>

                                            <?php } ?>

                                            <?php
                                            $results_index++;
                                        }

                                        if ($results_index == 1 && $passed == 0) {
                                            ?>

                                            <br>
                                            <div class='button' onclick='start_assessment(<? echo $index; ?>,1)'
                                                id='start_assessment_<? echo $index; ?>' style='background-color:orange;'>2nd ATTEMPT</div>

                                            <?php
                                        }

                                        if ($results_index == 2 && $passed == 0) {
                                            ?>
                                            <br>
                                            <div class='button' onclick='start_assessment(<? echo $index; ?>,1)'
                                                id='start_assessment_<? echo $index; ?>' style='background-color:orange;'>3rd ATTEMPT</div>

                                        <?php }
                                    } else {

                                        ?>

                                        <br>
                                        <input type='text' name='attendince_<? echo $index; ?>' id='attendince_<? echo $index; ?>' hidden
                                            value='NO' class='input' />
                                        <div class='button' onclick='mark_attendance(<? echo $index; ?>)' id='attendance_<? echo $index; ?>'
                                            style='background-color:grey;'>NO</div>


                                        <div class='button' onclick='start_assessment(<? echo $index; ?>)'
                                            id='start_assessment_<? echo $index; ?>' hidden style='background-color:orange;'>ASSESSMENT
                                        </div>

                                        <?php
                                    }
                                } else {
                                    //   echo "found test";
                    
                                    $test_sec = $db->exec_query('tests_sec', ['*'], '', '', '', '', "booking = {$_GET['record_id']} AND test_id = {$tests[$index_ass_tests]} AND client_employees_id = {$employee['record_id']}", "order by record_id ASC");

                                    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>
                                        <input type='text' name='attendince_<? echo $index; ?>' id='attendince_<? echo $index; ?>' hidden
                                            value='NO' class='input' />
                                        <div class='button' onclick='mark_test_attendance(<? echo $index; ?>)'
                                            id='attendance_<? echo $index; ?>' style='background-color:grey;'>NO</div>


                                        <div class='button' onclick='start_test(<? echo $index; ?>)' id='start_test_<? echo $index; ?>'
                                            hidden style='background-color:orange;'>TEST
                                        </div>

                                        <?php
                                    }
                                }
                                ?>
                        </tr>


                    <?php }
                    $index_ass_tests++;
                    $index++;
                }
                ?>
                <input type='number' id='row_count' hidden name='row_count' value='<?php echo $index; ?>' readonly />
                <input type='number' id='clients_id' hidden name='clients_id' value='<?php echo $clients_id ?>' readonly />
                <input type='number' id='safesure_users_id' hidden name='safesure_users_id'
                    value='<?php echo $booking['safesure_users_id']; ?>' readonly />
                <input type='text' id='status' hidden name='status' value='<?php echo $booking['status']; ?>' readonly />
                <?php
    }
    ?>

        </table>

        <hr>
        <?php ?>

        <div class='form_container'
            style='width:90%; margin-bottom:5vw; display: flex; flex-direction: row; justify-content: space-around;'>
            <div>
                <h2>ATTENDANCE REGISTAR NOTES</h2>
                <textarea id='notes' name='notes' class='input' required placeholder='NOTES'></textarea>
            </div>
        </div>

        <button class='button' name='save'>MARK AS COMPLETE</button>
    </form>

    <script>

        function open_assessment_pdf(id) {
            document.getElementById('pdf_div').style.display = 'block';
            document.getElementById('pdf_frame').src = "https://safesure.elegantwork.co.za/WebApp/assesor/html_pdf.php?id=" + id;
        }

        function mark_attendance(ind) {
            if (document.getElementById('attendance_' + ind).style.backgroundColor == 'grey') {
                document.getElementById('attendance_' + ind).style.backgroundColor = 'green';
                document.getElementById('attendince_' + ind).value = 'YES';
                document.getElementById('attendance_' + ind).innerHTML = 'YES';
                document.getElementById('start_assessment_' + ind).hidden = false;
            } else {
                document.getElementById('attendance_' + ind).style.backgroundColor = 'grey';
                document.getElementById('attendince_' + ind).value = 'NO';
                document.getElementById('attendance_' + ind).innerHTML = 'NO';
                document.getElementById('start_assessment_' + ind).hidden = true;
            }
        }

        function start_assessment(index, attempt = 0) {
            window.location.href = "assesment.php?booking_id=<? echo $_GET['record_id']; ?>&clients_id=<?php echo $clients_id; ?>&client_employees_id=" + document.getElementById('is_emp_' + index).value + "&assessments_id=" + document.getElementById('assessment_' + index).value + "&attempt=" + attempt;

            console.log("'assesment.php?booking_id='<? echo $_GET['record_id']; ?>'&clients_id='<?php echo $clients_id; ?>'&client_employees_id='" + document.getElementById('is_emp_' + index).value + '&assessments_id=' + document.getElementById('assessment_' + index).value + '&attempt=" + attempt');

        }

        var client_id = document.getElementById('clients_id').value;

        function start_test(index, attempt = 0) {
            window.location.href = "test.php?booking_id=<? echo $_GET['record_id']; ?>&clients_id=<?php echo $clients_id; ?>&client_employees_id=" + document.getElementById('is_emp_' + index).value + "&test_id=" + document.getElementById('test_n' + index).value + "&attempt=" + attempt;
        }


        function mark_test_attendance(ind) {
            if (document.getElementById('attendance_' + ind).style.backgroundColor == 'grey') {
                document.getElementById('attendance_' + ind).style.backgroundColor = 'green';
                document.getElementById('attendince_' + ind).value = 'YES';
                document.getElementById('attendance_' + ind).innerHTML = 'YES';
                document.getElementById('start_test_' + ind).hidden = false;
            } else {
                document.getElementById('attendance_' + ind).style.backgroundColor = 'grey';
                document.getElementById('attendince_' + ind).value = 'NO';
                document.getElementById('attendance_' + ind).innerHTML = 'NO';
                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;
        }

        document.getElementById('clients').value;
        document.getElementById('safesure_users_id').value;
        document.getElementById('status').value;
        get_emps();

        function get_emps() {
            clients_id = document.getElementById('clients_id').value;
            document.getElementById('employees').innerHTML = '';
            const xhttp = new XMLHttpRequest();
            // Define a callback function
            xhttp.onload = function () {
                // Here you can use the Data
                if (this.responseText != 1) {
                    array_emps = this.responseText.split('||');
                    var list = document.getElementById('employees');
                    array_emps.forEach(function (item) {
                        var option = document.createElement('option');
                        option.value = item;
                        list.appendChild(option);
                    });
                }
            }
            xhttp.open("GET", '/WebBuilder/get_datalist_ajax.php?table_name=<? echo $table_name; ?>&clients_id=' + clients_id);
            xhttp.send();
        }

        function check_value(ind) {
            const inputValue = document.getElementById('emp_name_' + ind).value.trim();
            if (!inputValue) return;

            const matchedOption = Array.from(document.querySelectorAll('#employees option')).find(option => option.value === inputValue);
            if (matchedOption) {
                // Extract specific option values
                // alert(matchedOption.value);
                const optionValues = matchedOption.value.split('|');
                // Assign values to input fields
                document.getElementById('emp_name_' + ind).value = optionValues[1];
                document.getElementById('emp_sur_' + ind).value = optionValues[2];
                document.getElementById('emp_id_' + ind).value = optionValues[3];
                document.getElementById('emp_occ_' + ind).value = optionValues[4];
                document.getElementById('is_emp_' + ind).value = optionValues[0];
            }
        }
    </script>
</div>

<!-- ? include $_SERVER['DOCUMENT_ROOT'] . "/WebApp/assesor/html.class.php";
$db = new DBMain();


$clients_res = $db->exec_query('clients', ['*'], '', '', '', '', "1", "ORDER BY clients_name ASC");
$clients_data = '<option></option>';
while ($clients = $clients_res->fetch_assoc()) {


    $clients_data = $clients_data . "<option value='{$clients['record_id']}'>{$clients['clients_name']}</option>";
}

$users_res = $db->exec_query('safesure_users', ['*'], '', '', '', '', "user_type_id = 2", "ORDER BY safesure_users_name ASC");
$users_data = '<option></option>';
while ($users = $users_res->fetch_assoc()) {

    $users_data = $users_data . "<option value='{$users['record_id']}'>{$users['safesure_users_name']}</option>";
}

$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>'";
}

$booking_res = $db->exec_query('bookings', ['*'], '', '', '', '', "record_id = '{$_GET['record_id']}'");
$booking = $booking_res->fetch_assoc();
$WebApp->pageHeading('ATTENDENCE REGISTER');

?>


<datalist name='employees' id='employees'></datalist>


<div style='background-color:grey;position:absolute;z-index:999;left:5%;top:3%;width:90%;height:100%;cursor:move;touch-action:pan-y' id='pdf_div' hidden>
    <div style='position:absolute;z-index:9999;right:1%;top:1%;color:white; font-size:50px' onclick="document.getElementById('pdf_div').style.display = 'none';">X</div>
    <iframe src="" 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 if (strpos($booking['client_employees'], 'B') !== false) {
    } else {
    ?>
        <form action='complete_booking.php' method="POST">
            <input type='text' name="booking_id" hidden value='<php echo $_GET['record_id']; ?>' />
            <hr><br>
            <table id='content_table'>
                <tr>
                    <th>
                        NAME (SEARCHABLE)
                        </td>
                    <th>
                        SURNAME
                        </td>
                    <th>
                        OCUPATION
                        </td>
                    <th>
                        ASSESMENT
                        </td>
                    <th>
                        ATTENDANCE
                        </td>

                </tr>
                < $ems = explode(',',  substr($booking['client_employees'], 1)); ?>
                < $ass = explode(',', substr($booking['assessments'], 1));
                $index = 1;

                foreach ($ems as $emp) {
                    if (strlen($emp) > 0) {
                        $emp_res = $db->exec_query('client_employees', ['*'], '', '', '', '', "record_id = $emp");
                        $employee = $emp_res->fetch_assoc();

                ?>
                        <tr>
                            <td>

                                <input type='text' name='is_emp_< echo $index; ?>' hidden id='is_emp_< echo $index; ?>' value='< echo $employee['record_id']; ?>' />
                                <input type='text' name='emp_name_< echo $index; ?>' id='emp_name_< echo $index; ?>' onchange='check_value(< echo $index; ?>)' class='input' list="employees" value='< echo $employee['client_employees_name']; ?>' />

                                </td>
                            <td>

                                <input type='text' name='emp_sur_< echo $index; ?>' id='emp_sur_< echo $index; ?>' value='< echo $employee['surname']; ?>' class='input' />

                                </td>
                            <td>

                                <input type='text' name='emp_occ_< echo $index; ?>' id='emp_occ_< echo $index; ?>' value='< echo $employee['occupation']; ?>' class='input' />

                                </td>
                            <td>
                                <select id='assesment_< echo $index; ?>' name='assesment_< echo $index; ?>' disabled  class='select'>< echo $assessments_data; ?></select>
                                <script>
                                    document.getElementById('assesment_< echo $index; ?>').value = < echo $ass[$index - 1]; ?>;
                                </script>
                            </td>
                            <td>
                                <php $assesses = $db->exec_query('assesses', ['*'], '', '', '', '', "booking = {$_GET['record_id']} AND assessments_id = {$ass[$index - 1]} AND client_employees_id = {$employee['record_id']}", "order by record_id ASC");

                                $results_index = 0;
                                $passed = 0;
                                if ($assesses->num_rows != 0) {
                                    while ($assesment = $assesses->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 ($assesment['results'] == 'C') {
                                            $passed = 1;
                                ?>
                                    <br>
                                    <div class='button' id='start_assesment_< echo $index; ?>' onclick="open_assesment_pdf(<php echo $assesment['record_id']; ?>)" style='background-color:GREEN;'>PASSED</div>

                                    <php } else { ?>

                                        <div class='button' id='start_assesment_< echo $index; ?>' onclick="open_assesment_pdf(<php echo $assesment['record_id']; ?>)" style='background-color:RED;'>FAILED</div>

                                    <php } ?>




                                <php
                                        $results_index++;
                                    }

                                    if ($results_index == 1 && $passed == 0) {
                                ?>



                                    <br>
                                    <div class='button' onclick='start_assesment(< echo $index; ?>,1)' id='start_assesment_< echo $index; ?>' style='background-color:orange;'>2nd ATTEMPT</div>

                                    <php
                                    }

                                    if ($results_index == 2 && $passed == 0) {
                                    ?>



                                        <br>
                                        <div class='button' onclick='start_assesment(< echo $index; ?>,1)' id='start_assesment_< echo $index; ?>' style='background-color:orange;'>3rd ATTEMPT</div>

                                        <php }
                                } else {

                                        ?>

                                        <br>
                                        <input type='text' name='attendince_< echo $index; ?>' id='attendince_< echo $index; ?>' hidden value='NO' class='input' />
                                        <div class='button' onclick='mark_attendance(< echo $index; ?>)' id='attendance_< echo $index; ?>' style='background-color:grey;'>NO</div>


                                            <div class='button' onclick='start_assesment(< echo $index; ?>)' id='start_assesment_< echo $index; ?>' hidden style='background-color:orange;'>ASSESMENT</div>
                            </td>
                        <php } ?>
                        </tr>
                <
                        $index++;
                    }
                }
                ?>
            </table>
            <input type='number' hidden id='row_count' name='row_count' value='< echo $index; ?>' readonly />

            <hr>
            <php  ?>

            <div class='form_container' style='width:90%; margin-bottom:5vw;'>
                <h2>ATTENDANCE REGISTAR NOTES</h2>
                <textarea id='notes' name='notes' class='input' required placeholder='NOTES'></textarea>
            </div>
            <button class='button' name='save'>MARK AS COMPLETE</button>
        </form>
    <php } ?>
    <script>
        function open_assesment_pdf(id) {
            document.getElementById('pdf_div').style.display = 'block';
            document.getElementById('pdf_frame').src = "https://safesure.elegantwork.co.za/WebApp/assesor/html_pdf.php?id=" + id;
        }

        function mark_attendance(ind) {
            if (document.getElementById('attendance_' + ind).style.backgroundColor == 'grey') {
                document.getElementById('attendance_' + ind).style.backgroundColor = 'green';
                document.getElementById('attendince_' + ind).value = 'YES';
                document.getElementById('attendance_' + ind).innerHTML = 'YES';
                document.getElementById('start_assesment_' + ind).hidden = false;
            } else {
                document.getElementById('attendance_' + ind).style.backgroundColor = 'grey';
                document.getElementById('attendince_' + ind).value = 'NO';
                document.getElementById('attendance_' + ind).innerHTML = 'NO';
                document.getElementById('start_assesment_' + ind).hidden = true;
            }
        }

        function start_assesment(index, attempt = 0) {
            window.location.href = "assesment.php?booking_id=< echo $_GET['record_id']; ?>&client_id=<php echo $booking['clients_id']; ?>&client_employees_id=" + document.getElementById('is_emp_' + index).value + "&assessments_id=" + document.getElementById('assesment_' + index).value + "&attempt=" + attempt;
        }

        document.getElementById('clients').value = value = '< echo $booking['clients_id']; ?>';
        document.getElementById('users').value = value = '<echo $booking['safesure_users_id']; ?>';
        document.getElementById('status').value = value = '< echo $booking['status']; ?>';
        get_emps();

        function get_emps() {
            clients_id = document.getElementById('clients').value;
            document.getElementById('employees').innerHTML = '';
            const xhttp = new XMLHttpRequest();
            // Define a callback function
            xhttp.onload = function() {
                // Here you can use the Data
                if (this.responseText != 1) {
                    array_emps = this.responseText.split('||');
                    var list = document.getElementById('employees');
                    array_emps.forEach(function(item) {
                        var option = document.createElement('option');
                        option.value = item;
                        list.appendChild(option);
                    });
                }
            }
            xhttp.open("GET", '/WebBuilder/get_datalist_ajax.php?table_name=< echo $table_name; ?>&clients_id=' + clients_id);
            xhttp.send();
        }

        function check_value(ind) {
            const inputValue = document.getElementById('emp_name_' + ind).value.trim();
            if (!inputValue) return;

            const matchedOption = Array.from(document.querySelectorAll('#employees option')).find(option => option.value === inputValue);
            if (matchedOption) {
                // Extract specific option values
                // alert(matchedOption.value);
                const optionValues = matchedOption.value.split('|');
                // Assign values to input fields
                document.getElementById('emp_name_' + ind).value = optionValues[1];
                document.getElementById('emp_sur_' + ind).value = optionValues[2];
                document.getElementById('emp_id_' + ind).value = optionValues[3];
                document.getElementById('emp_occ_' + ind).value = optionValues[4];
                document.getElementById('is_emp_' + ind).value = optionValues[0];
            }
        }
    </script>


</div> -->