function doImport(url) { const file = document.getElementById('importFile').files[0]; if (!file) { Swal.fire({ icon: 'warning', title: 'Perhatian', text: 'Pilih file Excel terlebih dahulu.', timer: 2000, showConfirmButton: false }); return; } const btn = document.getElementById('btnImport'); btn.disabled = true; btn.innerHTML = 'Memproses...'; const fd = new FormData(); fd.append('file', file); fd.append('_token', CSRF); fetch(url, { method: 'POST', body: fd }) .then(r => r.json()) .then(res => { btn.disabled = false; btn.innerHTML = ' Import'; const resultDiv = document.getElementById('importResult'); const msgDiv = document.getElementById('importMsg'); const errorsDiv = document.getElementById('importErrors'); resultDiv.style.display = 'block'; msgDiv.className = 'alert ' + (res.errors && res.errors.length ? 'alert-warning' : 'alert-success'); msgDiv.textContent = res.message; if (res.errors && res.errors.length) { errorsDiv.innerHTML = res.errors.map(e => `
⚠ ${e}
`).join(''); } else { errorsDiv.innerHTML = ''; } if (typeof tbl !== 'undefined') tbl.ajax.reload(null, false); if (typeof table !== 'undefined') table.ajax.reload(null, false); if (typeof dt !== 'undefined') dt.ajax.reload(null, false); }) .catch(() => { btn.disabled = false; btn.innerHTML = ' Import'; Swal.fire({ icon: 'error', title: 'Error', text: 'Gagal menghubungi server.' }); }); } document.getElementById('modalImport').addEventListener('hidden.bs.modal', function () { document.getElementById('importFile').value = ''; document.getElementById('importResult').style.display = 'none'; document.getElementById('importErrors').innerHTML = ''; });