bank-sampah/Views/Master/KategoriSampah/Index.cshtml

225 lines
8.0 KiB
Plaintext

@{
ViewData["Title"] = "Kategori Sampah";
}
<div class="flex flex-col gap-2 md:flex-row md:justify-between md:gap-0">
<div class="prose">
<span class="text-xl font-semibold text-black">
Kategori Sampah
</span>
</div>
<div class="flex flex-col gap-2 md:flex-row">
<button class="btn btn-sm bg-green-800 max-w-full rounded-full text-white hover:bg-green-900" onclick="modal_tambah.showModal()">
<i class="ph ph-plus"></i>
Tambah Kategori Sampah
</button>
</div>
</div>
<!-- Modal Tambah/Edit Data -->
<dialog id="modal_tambah" class="modal modal-bottom sm:modal-middle">
<div class="modal-box w-full max-w-2xl p-6 bg-white rounded-2xl">
<h3 id="modal_title" class="text-gray-900 text-xl font-semibold font-['Plus_Jakarta_Sans'] leading-8 mb-8">Tambah Kategori Sampah</h3>
<form id="formTambah" onsubmit="submitForm(event)">
<input type="hidden" id="edit_mode" value="false">
<div class="flex flex-col gap-6">
<!-- Field Nama -->
<div class="flex flex-col">
<fieldset class="fieldset">
<legend class="fieldset-legend">
Nama Kategori Sampah<span class="text-red-500">*</span>
</legend>
<input type="text" id="nama" class="input w-full" placeholder="Nama Kategori Sampah" required />
</fieldset>
</div>
<div class="flex flex-col">
<fieldset class="fieldset">
<legend class="fieldset-legend">
Jenis Sampah
<span class="text-red-500">*</span>
</legend>
<select id="jenis_sampah" class="select w-full" required>
<option value="" disabled selected>Pilih Jenis Sampah</option>
</select>
</fieldset>
</div>
<!-- Buttons -->
<div class="flex flex-col sm:flex-row gap-3 justify-end">
<button type="button" class="px-8 py-2.5 bg-white rounded-full outline outline-1 -outline-offset-1 outline-gray-300 text-slate-800 text-base font-semibold font-['Plus_Jakarta_Sans'] leading-6 hover:bg-gray-50 w-full sm:w-auto" onclick="closeModal()">
Batal
</button>
<button type="submit" class="px-8 py-2.5 bg-green-800 rounded-full text-white text-base font-semibold font-['Plus_Jakarta_Sans'] leading-6 hover:bg-green-900 w-full sm:w-auto">
Simpan
</button>
</div>
</div>
</form>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<!-- /modal tambah/edit -->
<div class="h-6"></div>
<div class="card bg-white">
<div class="card-body p-2">
<div class="w-full overflow-x-auto">
<table class="table-zebra table" id="example">
<!-- head -->
<thead>
<tr>
<th class="w-[5%]">No</th>
<th class="w-[60%]">Nama Kategori Sampah</th>
<th class="w-[25%]">Jenis Sampah</th>
<th class="w-[10%]">Aksi</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
@section Scripts {
<script type="text/javascript">
var table;
$(document).ready(function () {
table = new DataTable('#example', {
ajax: '/Master/KategoriSampah/Table',
scrollX: true,
autoWidth: false,
initComplete: function () {
$('div.dt-scroll-body thead').css('visibility', 'collapse');
},
columns: [
{ data: null, render: (d, t, r, m) => m.row + 1, orderable: false, searchable: false },
{ data: 'nama' },
{ data: 'jenis_sampah' },
{ data: 'aksi' },
]
});
// Delete button handler
$('#example').on('click', '.btn-circle.btn-error', function (e) {
e.preventDefault();
Swal.fire({
title: 'Apakah anda yakin?',
text: "Data yang dihapus tidak dapat dikembalikan lagi",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Ya, Hapus',
cancelButtonText: 'Batal',
buttonsStyling: false,
customClass: {
confirmButton: 'btn bg-red-500 text-white hover:bg-red-600 px-4 py-2 rounded-full mr-2',
cancelButton: 'btn bg-white text-gray-500 hover:bg-gray-50 px-4 py-2 rounded-full border border-gray-300',
},
}).then((result) => {
if (result.isConfirmed) {
// TODO: Implement actual delete API call
Swal.fire({
title: 'Terhapus!',
text: 'Data berhasil dihapus.',
icon: 'success',
confirmButtonText: 'OK',
buttonsStyling: false,
customClass: {
confirmButton: 'btn bg-green-800 text-white hover:bg-green-900 px-4 py-2 rounded-full',
},
}).then(() => {
// Reload table
table.ajax.reload();
});
}
});
});
// Edit button handler
$('#example').on('click', '.btn-circle.btn-warning', function (e) {
e.preventDefault();
// Get row data
var row = table.row($(this).parents('tr')).data();
// Set edit mode
$('#edit_mode').val('true');
$('#modal_title').text('Edit Kategori Sampah');
// Populate form with row data
$('#nama').val(row.nama);
// Open modal
modal_tambah.showModal();
});
// Get data for jenis sampah select
$.ajax({
url: '/Master/JenisSampah/table', // Ganti sesuai endpoint kamu
method: 'GET',
dataType: 'json',
success: function (response) {
var dropdown = $('#jenis_sampah');
// Hapus placeholder lama
dropdown.empty();
dropdown.append('<option value="" disabled selected>Pilih Jenis Sampah</option>');
// Tambahkan item dari API
$.each(response, function (index, item) {
dropdown.append(
$('<option>', {
value: item.nama,
text: item.nama
})
);
});
},
error: function () {
console.error('Gagal memuat data jenis sampah.');
}
});
});
function closeModal() {
// Reset form
$('#formTambah')[0].reset();
$('#edit_mode').val('false');
$('#modal_title').text('Tambah Kategori Sampah');
// Close modal
modal_tambah.close();
}
function submitForm(e) {
e.preventDefault();
var isEditMode = $('#edit_mode').val() === 'true';
// TODO: Implement form submission logic
Swal.fire({
title: 'Berhasil!',
text: isEditMode ? 'Data berhasil diperbarui' : 'Data berhasil ditambahkan',
icon: 'success',
confirmButtonText: 'OK',
buttonsStyling: false,
customClass: {
confirmButton: 'btn bg-green-800 text-white hover:bg-green-900 px-4 py-2 rounded-full',
},
}).then(() => {
closeModal();
// Reload table
table.ajax.reload();
});
}
</script>
}