diff --git a/Views/Transaksi/SedekahSampah/Index.cshtml b/Views/Transaksi/SedekahSampah/Index.cshtml index eb18dbf..7857cd6 100644 --- a/Views/Transaksi/SedekahSampah/Index.cshtml +++ b/Views/Transaksi/SedekahSampah/Index.cshtml @@ -58,36 +58,40 @@
-
-
-
- - Jenis Sampah - * - - -
-
+
-
-
- - Berat Sampah (Kg) - * - - -
-
+
- @@ -202,19 +206,23 @@ $('#edit_mode').val('true'); $('#modal_title').text('Edit Sedekah Sampah'); - // Populate form with row data - $('#nama').val(row.nama); + // Populate form with row data + $('#nama').val(row.nama); + setSampahRows(row && row.detail_sampah ? row.detail_sampah : []); - // Open modal - modal_tambah.showModal(); - }); + // Open modal + modal_tambah.showModal(); + }); + + // initialize dynamic sampah fields + setSampahRows(); }); function closeModal() { - // Reset form $('#formTambah')[0].reset(); $('#edit_mode').val('false'); - $('#modal_title').text('Tambah Fasilitas'); + $('#modal_title').text('Tambah Sedekah Sampah'); + setSampahRows(); // Close modal modal_tambah.close(); @@ -242,5 +250,65 @@ table.ajax.reload(); }); } + + function setSampahRows(items) { + var container = document.getElementById('sampahFields'); + if (!container) return; + container.innerHTML = ''; + + if (items && items.length) { + items.forEach(function (item) { addSampahRow(item); }); + } else { + addSampahRow(); + } + } + + function addSampahRow(data) { + var template = document.getElementById('jenisSampahRowTemplate'); + var container = document.getElementById('sampahFields'); + if (!template || !container) return; + + var clone = template.content.firstElementChild.cloneNode(true); + var jenisSelect = clone.querySelector('select[name="jenis_sampah[]"]'); + var beratInput = clone.querySelector('input[name="berat_sampah[]"]'); + + if (data && jenisSelect) { + var jenisValue = data.jenis_sampah_id || data.jenis_sampah; + if (jenisValue) jenisSelect.value = jenisValue; + } + + if (data && beratInput && (data.berat !== undefined || data.total_berat !== undefined || data.jumlah_berat !== undefined)) { + beratInput.value = data.berat || data.total_berat || data.jumlah_berat; + } + + container.appendChild(clone); + updateSampahRemoveButtons(); + } + + function removeSampahRow(button) { + var container = document.getElementById('sampahFields'); + if (!container) return; + + var row = button.closest('.sampah-row'); + if (row) { + row.remove(); + updateSampahRemoveButtons(); + } + } + + function updateSampahRemoveButtons() { + var container = document.getElementById('sampahFields'); + if (!container) return; + + var rows = container.querySelectorAll('.sampah-row'); + var disableRemove = rows.length <= 1; + + rows.forEach(function (row) { + var btn = row.querySelector('.btn-remove-sampah'); + if (!btn) return; + btn.disabled = disableRemove; + btn.classList.toggle('btn-disabled', disableRemove); + }); + } }