263 lines
7.4 KiB
JavaScript
263 lines
7.4 KiB
JavaScript
const csrfToken = document.querySelector('input[name="_token"]')?.value ?? '';
|
|
const defaultSuccessMessage = 'Berhasil melakukan aksi!';
|
|
|
|
let isCreating = false;
|
|
let isEditing = false;
|
|
const deletingIds = new Set();
|
|
|
|
function showError(message) {
|
|
if (!message) return;
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Gagal',
|
|
text: message,
|
|
});
|
|
}
|
|
|
|
function handleModalSuccess(modalElement, message, afterAlert = () => {}) {
|
|
const handler = function () {
|
|
Swal.fire({
|
|
icon: 'success',
|
|
title: 'Berhasil',
|
|
text: message || defaultSuccessMessage,
|
|
timer: 2000,
|
|
showConfirmButton: false,
|
|
backdrop: true,
|
|
});
|
|
afterAlert();
|
|
modalElement.removeEventListener('hidden.bs.modal', handler);
|
|
};
|
|
|
|
modalElement.addEventListener('hidden.bs.modal', handler);
|
|
bootstrap.Modal.getInstance(modalElement)?.hide();
|
|
}
|
|
|
|
formCreateMasterPersetujuan.on('submit', async function (e) {
|
|
e.preventDefault();
|
|
|
|
if (isCreating) return; // cegah submit berulang
|
|
isCreating = true;
|
|
|
|
try {
|
|
const res = await fetch('/master-persetujuan', {
|
|
method: 'POST',
|
|
headers: {
|
|
'X-CSRF-TOKEN': csrfToken,
|
|
},
|
|
body: new FormData(this),
|
|
});
|
|
|
|
const responseData = await res.json();
|
|
if (!responseData.status) {
|
|
throw new Error(responseData.message || 'Terjadi kesalahan saat menyimpan data.');
|
|
}
|
|
|
|
handleModalSuccess(modalCreate, responseData.message, () => {
|
|
$('#col_add_master_persetujuan').html('');
|
|
colCount = 1;
|
|
formCreateMasterPersetujuan.find('select').val(null).trigger('change');
|
|
datatableMasterPersetujuan.bootstrapTable('refresh');
|
|
});
|
|
} catch (err) {
|
|
showError(err.message);
|
|
} finally {
|
|
isCreating = false;
|
|
}
|
|
});
|
|
|
|
async function deleteMasterPersetujuan(button) {
|
|
const { id, name } = $(button).data();
|
|
|
|
if (deletingIds.has(id)) return; // cegah klik berulang id sama
|
|
|
|
const result = await Swal.fire({
|
|
title: 'Apakah kamu yakin ingin menghapus data master persetujuan?',
|
|
text: name,
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
backdrop: true,
|
|
});
|
|
|
|
if (!result.isConfirmed) return;
|
|
|
|
try {
|
|
deletingIds.add(id);
|
|
|
|
const response = await fetch(`/master-persetujuan/${id}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'X-CSRF-TOKEN': csrfToken,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
const data = await response.json();
|
|
if (!data.status) {
|
|
throw new Error(data.message || 'Failed to delete Item.');
|
|
}
|
|
|
|
Swal.fire({
|
|
title: 'Success',
|
|
text: 'Data berhasil dihapus',
|
|
icon: 'success',
|
|
showConfirmButton: false,
|
|
timer: 1000,
|
|
}).then(() => datatableMasterPersetujuan.bootstrapTable('refresh'));
|
|
} catch (error) {
|
|
showError(error.message || 'Something went wrong. Please try again later.');
|
|
} finally {
|
|
deletingIds.delete(id);
|
|
}
|
|
}
|
|
|
|
async function editMasterPersetujuan(button) {
|
|
const data = $(button).data();
|
|
new bootstrap.Modal(modalEdit).show();
|
|
formEditMasterPersetujuan.attr('action', `/master-persetujuan/${data.id}`);
|
|
|
|
selectOptionPegawaiEdit();
|
|
selectOptionUnitKerjaEdit();
|
|
|
|
resetEditSelections();
|
|
|
|
if (data.pegawai_id) {
|
|
setOldSelect2Value('#pegawai_id_edit', data.pegawai_id, data.pegawai_nama);
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`/master-persetujuan/${data.id}`);
|
|
const responseData = await res.json();
|
|
|
|
if (!responseData.status) {
|
|
throw new Error(responseData.message || 'Gagal memuat data akses.');
|
|
}
|
|
|
|
const detailUnits = responseData.data?.detail_units;
|
|
if (Array.isArray(detailUnits)) {
|
|
setOldSelect2Values('#unit_akses_edit', detailUnits);
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
showError(err.message);
|
|
}
|
|
}
|
|
|
|
function resetEditSelections() {
|
|
formEditMasterPersetujuan[0]?.reset();
|
|
$('#pegawai_id_edit').val(null).trigger('change');
|
|
$('#unit_akses_edit').empty().trigger('change');
|
|
}
|
|
|
|
function selectOptionPegawaiEdit() {
|
|
const selectPegawai = $('#pegawai_id_edit');
|
|
if (selectPegawai.data('select2')) return;
|
|
|
|
selectPegawai.select2({
|
|
placeholder: '-- Pilih Pegawai --',
|
|
allowClear: true,
|
|
width: '100%',
|
|
dropdownParent: selectPegawai.parent(),
|
|
ajax: {
|
|
url: '/select-pegawai',
|
|
dataType: 'json',
|
|
delay: 250,
|
|
data: function (params) {
|
|
return { q: params.term };
|
|
},
|
|
processResults: function (data) {
|
|
return {
|
|
results: data?.data.map((item) => ({
|
|
id: item.id,
|
|
text: item.nama,
|
|
})),
|
|
};
|
|
},
|
|
cache: true,
|
|
},
|
|
minimumInputLength: 1,
|
|
});
|
|
}
|
|
|
|
function selectOptionUnitKerjaEdit() {
|
|
const selectUnit = $('#unit_akses_edit');
|
|
if (selectUnit.data('select2')) return;
|
|
|
|
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,
|
|
text: item.name,
|
|
})),
|
|
};
|
|
},
|
|
cache: true,
|
|
},
|
|
minimumInputLength: 1,
|
|
});
|
|
}
|
|
|
|
function setOldSelect2Value(selector, id, text) {
|
|
if (!id || !text) return;
|
|
const option = new Option(text, id, true, true);
|
|
$(selector).append(option).trigger('change');
|
|
}
|
|
|
|
function setOldSelect2Values(selector, items) {
|
|
if (!Array.isArray(items) || items.length === 0) return;
|
|
|
|
items.forEach((item) => {
|
|
if (!item?.id || !item?.text) return;
|
|
const option = new Option(item.text, item.id, true, true);
|
|
$(selector).append(option);
|
|
});
|
|
|
|
$(selector).trigger('change');
|
|
}
|
|
|
|
formEditMasterPersetujuan.on('submit', async function (e) {
|
|
e.preventDefault();
|
|
|
|
if (isEditing) return; // cegah submit ganda
|
|
isEditing = true;
|
|
|
|
try {
|
|
const actionUrl = formEditMasterPersetujuan.attr('action');
|
|
const formData = new FormData(this);
|
|
formData.append('_method', 'PUT');
|
|
|
|
const res = await fetch(actionUrl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'X-CSRF-TOKEN': csrfToken,
|
|
},
|
|
body: formData,
|
|
});
|
|
|
|
const responseData = await res.json();
|
|
if (!responseData.status) {
|
|
throw new Error(responseData.message || 'Terjadi kesalahan saat menyimpan data.');
|
|
}
|
|
|
|
handleModalSuccess(modalEdit, responseData.message, () => {
|
|
formCreateMasterPersetujuan.find('select').val(null).trigger('change');
|
|
datatableMasterPersetujuan.bootstrapTable('refresh');
|
|
});
|
|
} catch (err) {
|
|
showError(err.message);
|
|
} finally {
|
|
isEditing = false;
|
|
}
|
|
});
|