(function ($) {
'use strict';
window.pmsInitPcFormsSchemeFilters = function () {
if (window.pmsPcFormsSchemeFiltersInited) {
return;
}
var $form = $('#pcFormsSchemeFilterForm');
if (!$form.length) {
return;
}
if (typeof $.fn.select2 !== 'function') {
console.error('Select2 is required for PC Forms listing filters.');
return;
}
window.pmsPcFormsSchemeFiltersInited = true;
if (typeof window.pmsBindCleanFilterFormSubmit === 'function') {
window.pmsBindCleanFilterFormSubmit('#pcFormsSchemeFilterForm');
}
var select2Opts = { theme: 'bootstrap4', width: '100%', allowClear: true };
var $syncSelects = $form.find('select.scheme-project-sync');
var $allFilterSelects = $form.find('select.scheme-filter-select');
$allFilterSelects.each(function () {
var $sel = $(this);
if ($sel.hasClass('select2-hidden-accessible')) {
return;
}
var placeholder = $.trim($sel.find('option[value=""]').first().text() || '');
var opts = $.extend({}, select2Opts);
if (placeholder !== '') {
opts.placeholder = placeholder;
}
$sel.select2(opts);
});
var $richStatusSelect = $form.find('select[data-rich-status-filter]');
if ($richStatusSelect.length) {
$form.on('submit.pcFormsRichStatus', function () {
var $selected = $richStatusSelect.find('option:selected');
var filterType = $selected.data('filter-type') || '';
var filterValue = $selected.data('filter-value') || '';
$form.find('.pc-forms-rich-status-hidden').remove();
$form.find('select[name="state"]').attr('name', filterType ? '_state_disabled' : 'state');
if (filterType && filterValue) {
if (filterType === 'state') {
$form.append('');
} else if (filterType === 'workflow_stage') {
$form.append('');
} else if (filterType === 'meeting_type') {
$form.append('');
} else if (filterType === 'meeting_type_cleared') {
$form.append('');
$form.append('');
} else if (filterType === 'forum_label') {
$form.append('');
} else if (filterType === 'declined') {
$form.append('');
$form.append('');
}
}
});
}
var syncing = false;
$syncSelects.off('.pcFormsTripleSync');
$syncSelects.on('change.pcFormsTripleSync select2:select.pcFormsTripleSync select2:clear.pcFormsTripleSync', function () {
if (syncing) {
return;
}
var $this = $(this);
var pid = $this.val() || '';
var itemData = null;
if (pid !== '') {
var $selectedOption = $this.find('option:selected');
if ($selectedOption.length && $selectedOption.data('select2Data')) {
itemData = $selectedOption.data('select2Data');
} else {
// Fallback to select2 data API
var s2data = $this.select2('data');
if (s2data && s2data.length > 0) {
itemData = s2data[0];
}
}
}
syncing = true;
try {
$syncSelects.not(this).each(function () {
var $other = $(this);
if (($other.val() || '') !== pid) {
if (pid !== '' && itemData) {
// Create option if it doesn't exist
if (!$other.find('option[value="' + pid + '"]').length) {
var lookupType = $other.data('lookup-type') || '';
var label = pid;
if (lookupType === 'adp') label = itemData.adp_number || '—';
else if (lookupType === 'uid') label = itemData.code || itemData.uid || '—';
else label = itemData.name || itemData.text || '—';
var $newOpt = new Option(label, pid, true, true);
$($newOpt).data('select2Data', itemData);
$other.append($newOpt);
}
}
$other.val(pid).trigger('change.select2');
}
});
} finally {
syncing = false;
}
});
};
}(jQuery));