/**
 * Cards Module — Global JS
 */
(function() {
    'use strict';

    // Auto-refresh access log every 30 seconds if on that page
    if (document.getElementById('accessLogTable')) {
        setInterval(function() {
            const dt = $.fn.dataTable.tables({ api: true });
            if (dt && dt.ajax) {
                dt.ajax.reload(null, false);
            }
        }, 30000);
    }

    // Member search Select2 initialization for forms that need it
    document.querySelectorAll('.member-search-select2').forEach(function(el) {
        $(el).select2({
            theme: 'bootstrap-5',
            width: '100%',
            ajax: {
                url: '/api/members/search',
                dataType: 'json',
                delay: 300,
                data: function(params) {
                    return { q: params.term };
                },
                processResults: function(data) {
                    return {
                        results: data.map(function(m) {
                            return {
                                id: m.id,
                                text: m.membership_number + ' - ' + (m.text || m.full_name_en || '')
                            };
                        })
                    };
                }
            },
            minimumInputLength: 2,
            placeholder: 'Search member...'
        });
    });
})();