<?php /* Auto Complete */ ?>
<script language="javascript" type="text/javascript" src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css">

<div class="row mb-4">
    <div class="col-12">
        <div class="card shadow-sm">
            <div class="card-header bg-primary text-white text-end">
                <h5 class="card-title mb-0 urdu-regular">طالب علم تلاش کریں</h5>
            </div>
            <div class="card-body">
                <form id="searchForm" dir="rtl">
                    <div class="row g-3 align-items-end justify-content-end">
                        <div class="col-md-4 text-end">
                            <label for="stdName" class="form-label urdu-regular">نام طالب علم</label>
                            <input type="text" id="stdName" name="stdName" class="form-control text-end urdu-regular"
                                placeholder="نام درج کریں">
                        </div>
                        <div class="col-md-4 text-end">
                            <label for="registrationNumber" class="form-label urdu-regular">رجسٹریشن نمبر</label>
                            <input type="text" id="registrationNumber" name="registrationNumber"
                                class="form-control text-start" dir="ltr" placeholder="رجسٹریشن نمبر">
                        </div>
                        <div class="col-md-2 text-end mt-3 mt-md-0">
                            <button type="submit" class="btn btn-success w-100 urdu-regular shadow-sm"
                                id="btnSearch">تلاش کیجئے <i class="fa fa-search"></i></button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12">
        <div id="msgContainer"></div>
        <div id="searchBody">
            <!-- Search results will be rendered here by JavaScript -->
        </div>
    </div>
</div>

<script type="text/javascript">
    // Polyfill for $.browser to fix jquery.autocomplete.js compatibility with jQuery 3+
    if (typeof jQuery !== 'undefined') {
        jQuery.browser = jQuery.browser || {};
        (function () {
            jQuery.browser.msie = jQuery.browser.msie !== undefined ? jQuery.browser.msie : (/(msie|trident)/i.test(navigator.userAgent));
            jQuery.browser.version = jQuery.browser.version || 0;
            if (jQuery.browser.msie) {
                jQuery.browser.version = /(msie |rv:)(\d+(\.\d+)?)/i.exec(navigator.userAgent)?.[2] || 0;
            }
        })();
    }

    // Preserve the original autocomplete data logic
    $(document).ready(function () {
        $("#registrationNumber").autocompleteArray(
            [<?php echo ($crud->autoFill("SELECT registrationNo FROM regnumbers ORDER BY sno", "registrationNo")); ?>],
            {
                delay: 10,
                minChars: 1,
                matchSubset: 1,
                onItemSelect: function (li) { if (li) { /* Custom logic can go here */ } },
                autoFill: true,
                maxItemsToShow: 10
            }
        );

        $("#stdName").autocompleteArray(
            [<?php echo ($crud->autoFill("SELECT DISTINCT stdName FROM registrationinfo", "stdName")); ?>],
            {
                delay: 10,
                minChars: 1,
                matchSubset: 1,
                onItemSelect: function (li) { if (li) { /* Custom logic can go here */ } },
                autoFill: true,
                maxItemsToShow: 10
            }
        );
    });
</script>

<script src="assets/js/search.js"></script>