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

    // Auto-load dynamic select options from known sources
    window.ReportsModule = {
        loadSelectOptions: function(selectElement, source) {
            var db = Database || null;
            var url = '';

            var sourceMap = {
                'membership_types': { url: '/api/members/search?q=&type=membership_types', labelField: 'name_en', valueField: 'id' },
                'payment_methods': { url: '/api/reports/param-options?source=payment_methods', labelField: 'name_en', valueField: 'id' },
                'complaint_categories': { url: '/api/reports/param-options?source=complaint_categories', labelField: 'name_en', valueField: 'id' }
            };

            if (!sourceMap[source]) return;

            $.get(sourceMap[source].url, function(res) {
                var data = res.data || res.results || res;
                if (!Array.isArray(data)) return;
                data.forEach(function(item) {
                    var val = item[sourceMap[source].valueField] || item.id;
                    var label = item[sourceMap[source].labelField] || item.text || item.name;
                    $(selectElement).append('<option value="' + val + '">' + label + '</option>');
                });
            }).fail(function() {
                // Silently fail — options just won't be populated
            });
        }
    };

})();