155 lines
8.1 KiB
JavaScript
155 lines
8.1 KiB
JavaScript
/* ============================================================
|
|
js/bantuan.js — Histori bantuan v2.1
|
|
============================================================ */
|
|
|
|
window.labelJenisBantuan = {
|
|
sembako:'Sembako', uang:'Uang/Dana', pendidikan:'Pendidikan',
|
|
kesehatan:'Kesehatan', pakaian:'Pakaian', pelatihan:'Pelatihan', lainnya:'Lainnya'
|
|
};
|
|
window.labelStatusBantuan = {
|
|
tersalur:'Tersalur', belum:'Belum Tersalur', batal:'Dibatalkan'
|
|
};
|
|
|
|
/* Render tab bantuan */
|
|
window.renderTabBantuan = async function (item) {
|
|
var container = document.getElementById('detail-tab-bantuan');
|
|
if (!container) return;
|
|
container.innerHTML = '<div class="loading">Memuat histori bantuan...</div>';
|
|
|
|
var data = await window.fetchJson('php/read_bantuan.php?kemiskinan_id=' + item.id, []);
|
|
container.innerHTML = '';
|
|
|
|
/* Alert jatuh tempo */
|
|
var jt = data.filter(function(d){ return parseInt(d.jatuh_tempo) === 1; });
|
|
if (jt.length) {
|
|
var alert = document.createElement('div');
|
|
alert.className = 'bantuan-alert';
|
|
alert.innerHTML = '<i class="fa-solid fa-triangle-exclamation"></i> ' + jt.length + ' bantuan sudah jatuh tempo dan belum disalurkan!';
|
|
container.appendChild(alert);
|
|
}
|
|
|
|
if (window.appAuth.role === 'pengurus_masjid') {
|
|
var btnAdd = document.createElement('button');
|
|
btnAdd.className = 'btn btn-add';
|
|
btnAdd.innerHTML = '+ Catat Bantuan Baru';
|
|
btnAdd.onclick = function () { window.openTambahBantuanModal(item.id, item.nama_kepala_keluarga); };
|
|
container.appendChild(btnAdd);
|
|
}
|
|
|
|
if (!data.length) {
|
|
var empty = document.createElement('div');
|
|
empty.className = 'empty'; empty.textContent = 'Belum ada histori bantuan.';
|
|
container.appendChild(empty); return;
|
|
}
|
|
|
|
data.forEach(function (b) {
|
|
var jmlTxt = b.jumlah_bantuan ? b.jumlah_bantuan + ' ' + (b.satuan || '') : '—';
|
|
var isJt = parseInt(b.jatuh_tempo) === 1;
|
|
|
|
var card = document.createElement('div');
|
|
card.className = 'b-card ' + b.status_penyaluran + (isJt ? ' jatuh' : '');
|
|
|
|
var chipColors = { tersalur:'chip-green', belum:'chip-amber', batal:'chip-gray' };
|
|
|
|
card.innerHTML =
|
|
'<div class="b-head">' +
|
|
'<span class="b-jenis">' + (window.labelJenisBantuan[b.jenis_bantuan] || b.jenis_bantuan) + '</span>' +
|
|
'<span class="chip ' + (chipColors[b.status_penyaluran]||'chip-gray') + '">' + (window.labelStatusBantuan[b.status_penyaluran]||b.status_penyaluran) + '</span>' +
|
|
'</div>' +
|
|
'<div class="b-desc">' + window.escapeHtml(b.deskripsi_bantuan) + '</div>' +
|
|
'<div class="b-meta">📅 ' + (b.tanggal_fmt||'—') + ' · ' + window.escapeHtml(jmlTxt) + (b.nama_masjid ? ' · ' + window.escapeHtml(b.nama_masjid) : '') + '</div>' +
|
|
(b.periode_fmt ? '<div class="b-periode' + (isJt ? ' late' : '') + '">Berikutnya: ' + b.periode_fmt + (isJt ? ' — Jatuh Tempo!' : '') + '</div>' : '') +
|
|
(b.dicatat_oleh ? '<div class="b-meta">Dicatat: ' + window.escapeHtml(b.dicatat_oleh) + '</div>' : '');
|
|
|
|
if (window.appAuth.role === 'pengurus_masjid') {
|
|
var acts = document.createElement('div');
|
|
acts.className = 'b-acts';
|
|
|
|
if (b.status_penyaluran === 'belum') {
|
|
var btnS = document.createElement('button');
|
|
btnS.className = 'btn btn-salur'; btnS.textContent = '✓ Tandai Tersalur';
|
|
btnS.onclick = (function(bid){ return async function(){
|
|
try {
|
|
await window.postForm('php/update_bantuan_status.php', { id: bid, status_penyaluran: 'tersalur' });
|
|
window.showToast('Bantuan ditandai tersalur.', 'success');
|
|
window.renderTabBantuan(item);
|
|
} catch(e){ window.showToast('Gagal: ' + e.message, 'error'); }
|
|
}; })(b.id);
|
|
acts.appendChild(btnS);
|
|
|
|
var btnBt = document.createElement('button');
|
|
btnBt.className = 'btn btn-delete'; btnBt.textContent = 'Batalkan';
|
|
btnBt.onclick = (function(bid){ return async function(){
|
|
if (!confirm('Batalkan bantuan ini?')) return;
|
|
try {
|
|
await window.postForm('php/update_bantuan_status.php', { id: bid, status_penyaluran: 'batal' });
|
|
window.showToast('Bantuan dibatalkan.', 'info');
|
|
window.renderTabBantuan(item);
|
|
} catch(e){ window.showToast('Gagal: ' + e.message, 'error'); }
|
|
}; })(b.id);
|
|
acts.appendChild(btnBt);
|
|
}
|
|
|
|
var btnH = document.createElement('button');
|
|
btnH.className = 'btn btn-delete'; btnH.innerHTML = '<i class="fa-solid fa-trash"></i>';
|
|
btnH.onclick = (function(bid){ return async function(){
|
|
if (!confirm('Hapus catatan bantuan ini?')) return;
|
|
try {
|
|
await window.postForm('php/delete_bantuan.php', { id: bid });
|
|
window.showToast('Catatan dihapus.', 'success');
|
|
window.renderTabBantuan(item);
|
|
} catch(e){ window.showToast('Gagal: ' + e.message, 'error'); }
|
|
}; })(b.id);
|
|
acts.appendChild(btnH);
|
|
card.appendChild(acts);
|
|
}
|
|
container.appendChild(card);
|
|
});
|
|
};
|
|
|
|
/* Modal tambah bantuan */
|
|
window.openTambahBantuanModal = function (kemiskinanId, namaKK) {
|
|
document.getElementById('bantuan-kem-id').value = kemiskinanId;
|
|
document.getElementById('bantuan-jenis').value = 'sembako';
|
|
document.getElementById('bantuan-deskripsi').value = '';
|
|
document.getElementById('bantuan-jumlah').value = '';
|
|
document.getElementById('bantuan-satuan').value = '';
|
|
document.getElementById('bantuan-tanggal').value = new Date().toISOString().split('T')[0];
|
|
document.getElementById('bantuan-periode').value = '';
|
|
document.getElementById('bantuan-status').value = 'belum';
|
|
document.getElementById('bantuan-dicatat').value = window.appAuth.nama || '';
|
|
document.getElementById('modal-bantuan-title').textContent = 'Catat Bantuan — ' + namaKK;
|
|
|
|
var modal = document.getElementById('modal-bantuan');
|
|
modal.style.display = 'flex';
|
|
document.getElementById('bantuan-deskripsi').focus();
|
|
modal.onclick = function (e) { if (e.target === this) this.style.display = 'none'; };
|
|
|
|
document.getElementById('btn-submit-bantuan').onclick = async function () {
|
|
var btn = this;
|
|
btn.disabled = true; btn.textContent = 'Menyimpan...';
|
|
var desc = document.getElementById('bantuan-deskripsi').value.trim();
|
|
if (!desc) { window.showToast('Deskripsi wajib diisi.', 'error'); btn.disabled=false; btn.textContent='Simpan Bantuan'; return; }
|
|
var payload = {
|
|
kemiskinan_id: document.getElementById('bantuan-kem-id').value,
|
|
jenis_bantuan: document.getElementById('bantuan-jenis').value,
|
|
deskripsi_bantuan: desc,
|
|
jumlah_bantuan: document.getElementById('bantuan-jumlah').value,
|
|
satuan: document.getElementById('bantuan-satuan').value.trim(),
|
|
tanggal_bantuan: document.getElementById('bantuan-tanggal').value,
|
|
periode_berikutnya:document.getElementById('bantuan-periode').value,
|
|
status_penyaluran: document.getElementById('bantuan-status').value,
|
|
dicatat_oleh: document.getElementById('bantuan-dicatat').value.trim()
|
|
};
|
|
try {
|
|
await window.postForm('php/create_bantuan.php', payload);
|
|
modal.style.display = 'none';
|
|
window.showToast('Bantuan berhasil dicatat.', 'success');
|
|
if (window._currentDetailItem && window._currentDetailItem.id === parseInt(payload.kemiskinan_id)) {
|
|
window.renderTabBantuan(window._currentDetailItem);
|
|
}
|
|
} catch (err) { window.showToast('Gagal: ' + err.message, 'error'); }
|
|
finally { btn.disabled=false; btn.textContent='Simpan Bantuan'; }
|
|
};
|
|
};
|