let colCount = 1; $(document).ready(function() { $('.unit_kerja').select2({ placeholder: '--- Pilih Unit Kerja ---', allowClear: true, width: '100%', ajax: { url : '/select-unit-kerja', dataType: 'json', delay: 250, data: function(params){ let q = ''; if(allAkses){ q = params.term; }else{ q = authUnitKerja?.name ?? ''; } return { q }; }, processResults: function(data){ let results = data?.data.map(item => ({ id: item.id, text: item.name })); return { results }; }, cache: true, }, minimumInputLength: 0, }); $('.sub_unit_kerja').select2({ placeholder: '-- Pilih Sub Unit Kerja --', allowClear: true, width: '100%', }); // --- isi default unit kerja --- // if(authUnitKerja){ // let option = new Option(authUnitKerja.name, authUnitKerja.id, true, true); // $('.unit_kerja').append(option).trigger('change'); // } let initialUnit = $('.unit_kerja').val(); if(initialUnit){ loadSubUnitKerja(initialUnit); } let prefillSubIds = []; let prefillSubMeta = {}; // jalankan setiap kali unit_kerja berubah $('.unit_kerja').on('change', function(){ let idUnit = $(this).val(); if(idUnit){ loadSubUnitKerja(idUnit, prefillSubIds, prefillSubMeta); prefillSubIds = []; prefillSubMeta = {}; } }); $('.kategori_dok').select2(); if(allAkses){ selectOptionUnitKerjaV1(0); }else{ selectOptionUnitKerjaV2(0); } if (window.__folderPrefillApplied) return; const urlParams = new URLSearchParams(window.location.search); const prefill = window.__prefillFromSession || null; const unitId = prefill?.unitId || urlParams.get('unitKerja'); const subParam = prefill?.subId || urlParams.get('subUnit'); const katParam = prefill?.kategoriId || urlParams.get('kategori'); const unitName = prefill?.unitName || urlParams.get('unitName'); const subName = prefill?.subName || urlParams.get('subName'); const kategoriName = prefill?.kategoriName || urlParams.get('kategoriName'); let prefillSubIdsFinal = []; let prefillKatIdsFinal = []; if (unitId) { window.__folderPrefillApplied = true; window.__prefillFromSession = null; $('.unit_kerja').val(null).trigger('change'); $('.sub_unit_kerja').val(null).trigger('change'); $('.kategori_dok').val(null).trigger('change'); const unitLabel = unitName || unitId; const unitOption = new Option(unitLabel, unitId, true, true); const selectedSubIds = subParam ? Array.from(new Set(subParam.split(',').filter(Boolean))) : []; const subNames = subName ? subName.split(',').map(s => s.trim()).filter(Boolean) : []; prefillSubIds = selectedSubIds; prefillSubMeta = selectedSubIds.reduce((acc, id, idx) => { let label = subNames[idx] || subNames[0] || id; label = label.replace(/^\d+\s*\/\s*/, ''); acc[id] = label; return acc; }, {}); $('.unit_kerja').append(unitOption).trigger('change'); if (selectedSubIds.length) { $('.sub_unit_kerja').val(selectedSubIds).trigger('change'); } prefillSubIdsFinal = selectedSubIds; } if (katParam) { const katIds = Array.from(new Set(katParam.split(',').filter(Boolean))); if (katIds.length) { $('.kategori_dok').val(katIds).trigger('change'); prefillKatIdsFinal = katIds; } else if (kategoriName) { const katOption = new Option(kategoriName, katParam, true, true); $('.kategori_dok').append(katOption).trigger('change'); prefillKatIdsFinal = katParam ? [katParam] : []; } } if ((unitId || subParam || katParam) && typeof index === 'function') { index(prefillKatIdsFinal, unitId, prefillSubIdsFinal, ''); } }); function loadSubUnitKerja(unitId, selectedSubIds = [], selectedSubMeta = {}){ $('.sub_unit_kerja').empty().append(''); $.ajax({ url: `/select-sub-unit-kerja/${unitId}`, method: 'GET', success: function(response) { if (response?.data) { response.data.forEach(unit => { let selected = false; if (selectedSubIds.length) { selected = selectedSubIds.includes(String(unit.id)); } else if (authSubUnitKerja) { selected = unit.id === authSubUnitKerja.objectsubunitkerjapegawaifk; } const option = new Option(unit.name, unit.id, false, selected); $('.sub_unit_kerja').append(option); }); if (selectedSubIds.length) { selectedSubIds.forEach(id => { if ($(`.sub_unit_kerja option[value="${id}"]`).length === 0) { const label = selectedSubMeta[id] || id; const opt = new Option(label, id, false, true); $('.sub_unit_kerja').append(opt); } }); } $('.sub_unit_kerja').trigger('change'); } } }); } function addForm(){ let col = $("#col_add_file") let html = ''; html += `

#
Format yang didukung: PDF Maksimal 10mb.
` col.append(html) let newInput = document.querySelector(`#fileUpload_${colCount}`); bindFileUpload(newInput); if(allAkses){ selectOptionUnitKerjaV1(colCount) }else{ selectOptionUnitKerjaV2(colCount) } colCount++; } function removeCol(count){ $(`#col-${count}`).remove() } function selectOptionUnitKerjaV1(colCount) { let selectUnit = $(`#select_id_unit_kerja_${colCount}`); let selectSubUnit = $(`#select_id_sub_unit_kerja_${colCount}`); // inisialisasi select2 untuk Unit Kerja selectUnit.select2({ placeholder: '-- Pilih Unit Kerja --', allowClear:true, width: '100%', dropdownParent: selectUnit.parent(), ajax:{ url : '/select-unit-kerja', dataType: 'json', delay: 250, data: function(params){ return { q: params.term } }, processResults: function(data){ return { results : data?.data.map(item => ({ id: item.id+'/'+item.name, text: item.name, sub_units: item.sub_unit_kerja // kirim ke front })) } }, cache: true, }, minimumInputLength: 1, }); selectSubUnit.select2({ placeholder: '-- Pilih Sub Unit Kerja --', allowClear: true, width: '100%', dropdownParent: selectSubUnit.parent() }); // event ketika unit kerja dipilih selectUnit.on('select2:select', function (e) { let data = e.params.data; // data unit kerja terpilih selectSubUnit.empty().append(''); if (data.sub_units && data.sub_units.length > 0) { data.sub_units.forEach(sub => { selectSubUnit.append(``); }); } // aktifkan select2 untuk sub unit }); } function selectOptionUnitKerjaV2(colCount) { let selectUnit = $(`#select_id_unit_kerja_${colCount}`); let selectSubUnit = $(`#select_id_sub_unit_kerja_${colCount}`); // Kosongkan dulu selectUnit.empty().append(''); selectSubUnit.empty().append(''); // Load semua unit kerja tanpa ajax $.ajax({ url: '/select-unit-kerja', // endpoint tetap sama method: 'GET', success: function(response) { if (response?.data) { response.data.forEach(unit => { const option = new Option(unit.name, unit.id +'/'+unit.name, false, false); $(option).data('sub_units', unit.sub_unit_kerja); // simpan sub unit di data attribute selectUnit.append(option); }); } } }); // Inisialisasi select2 tanpa ajax selectUnit.select2({ placeholder: '-- Pilih Unit Kerja --', allowClear: true, width: '100%', dropdownParent: selectUnit.parent() }); selectSubUnit.select2({ placeholder: '-- Pilih Sub Unit Kerja --', allowClear: true, width: '100%', dropdownParent: selectSubUnit.parent() }); // Event ketika unit kerja dipilih selectUnit.on('change', function () { const selectedOption = $(this).find(':selected'); const subUnits = selectedOption.data('sub_units') || []; selectSubUnit.empty().append(''); if (subUnits.length > 0) { subUnits.forEach(sub => { selectSubUnit.append(``); }); } }); }