bps-rw/Views/Faq/Index.cshtml

314 lines
12 KiB
Plaintext

@{
ViewData["Title"] = "FAQ";
}
<!-- Toast Notification -->
<div id="successToast" class="fixed top-20 right-6 z-9999 hidden">
<div class="alert bg-green-50 border border-green-200 shadow-lg flex items-start gap-3 pr-12 min-w-[320px]">
<div class="shrink-0">
<div class="w-5 h-5 rounded-full bg-green-500 flex items-center justify-center">
<span class="icon text-white text-xs">check</span>
</div>
</div>
<div class="flex-1">
<h3 id="toastTitle" class="font-semibold text-gray-900">FAQ berhasil ditambah!</h3>
<p id="toastMessage" class="text-sm text-gray-600">Berhasil! FAQ baru telah berhasil ditambahkan.</p>
</div>
<button onclick="closeToast()" class="absolute top-3 right-3 text-gray-400 hover:text-gray-600">
<span class="icon text-lg">close</span>
</button>
</div>
</div>
<div class="breadcrumbs text-sm mb-4">
<ul>
<li class="text-gray-500"><a>CMS</a></li>
<li id="breadcrumb-active">FAQ</li>
</ul>
</div>
<!-- Header -->
<div class="flex items-center justify-between mb-6">
<h2 class="text-xl font-semibold text-gray-800">FAQ</h2>
<div class="flex items-center">
<button onclick="modal_tambah.showModal()" class="btn btn-primary rounded-full">
<span class="icon icon-fill">add</span>
Tambah
</button>
</div>
</div>
<!-- Table -->
<div class="card bg-white shadow-sm">
<div class="card-body p-0">
<table class="table-zebra table w-full" id="faq-table">
<thead>
<tr>
<th class="w-[5%]">No</th>
<th class="w-[35%]">Pertanyaan</th>
<th class="w-[45%]">Answer</th>
<th class="w-[15%]">Aksi</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<!-- Modal Tambah FAQ -->
<dialog id="modal_tambah" class="modal modal-bottom sm:modal-middle">
<div class="modal-box w-full sm:max-w-lg">
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_tambah.close()">✕</button>
<h3 class="text-lg font-bold mb-4">Tambah FAQ</h3>
<form id="form_tambah">
<div class="space-y-4">
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Pertanyaan <span class="text-red-500">*</span></legend>
<textarea name="Pertanyaan" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan pertanyaan" required></textarea>
</fieldset>
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Jawaban <span class="text-red-500">*</span></legend>
<textarea name="Jawaban" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan jawaban" required></textarea>
</fieldset>
</div>
<div class="modal-action mt-6">
<button type="button" class="btn rounded-full" onclick="resetFormTambah()">
<span class="icon">cancel</span>
Tutup
</button>
<button type="submit" class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
<span class="icon">save</span>
Simpan
</button>
</div>
</form>
</div>
<form method="dialog" class="modal-backdrop">
<button onclick="resetFormTambah()">close</button>
</form>
</dialog>
<!-- Modal Edit FAQ -->
<dialog id="modal_edit" class="modal modal-bottom sm:modal-middle">
<div class="modal-box w-full sm:max-w-lg">
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_edit.close()">✕</button>
<h3 class="text-lg font-bold mb-4">Tambah FAQ</h3>
<form id="form_edit">
<input type="hidden" id="edit_id" name="Id">
<div class="space-y-4">
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Pertanyaan <span class="text-red-500">*</span></legend>
<textarea id="edit_pertanyaan" name="Pertanyaan" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan pertanyaan" required></textarea>
</fieldset>
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Jawaban <span class="text-red-500">*</span></legend>
<textarea id="edit_jawaban" name="Jawaban" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan jawaban" required></textarea>
</fieldset>
</div>
<div class="modal-action mt-6">
<button type="button" class="btn rounded-full" onclick="resetFormEdit()">
<span class="icon">cancel</span>
Tutup
</button>
<button type="submit" class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
<span class="icon">save</span>
Simpan
</button>
</div>
</form>
</div>
<form method="dialog" class="modal-backdrop">
<button onclick="resetFormEdit()">close</button>
</form>
</dialog>
<!-- Modal Detail FAQ -->
<dialog id="modal_detail" class="modal modal-bottom sm:modal-middle">
<div class="modal-box w-full max-w-2xl">
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_detail.close()">✕</button>
<h3 class="text-lg font-bold mb-4">Detail FAQ</h3>
<div class="space-y-4">
<div>
<p class="text-sm text-gray-500">Pertanyaan</p>
<p class="font-semibold text-lg" id="detail_pertanyaan">-</p>
</div>
<div>
<p class="text-sm text-gray-500">Jawaban</p>
<p id="detail_jawaban">-</p>
</div>
</div>
<div class="modal-action mt-6">
<button type="button" class="btn rounded-full" onclick="modal_detail.close()">
<span class="icon">cancel</span>
Tutup
</button>
<button type="button" id="btn_edit_from_detail"
class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
<span class="icon">edit</span>
Edit
</button>
</div>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://cdn.datatables.net/2.3.4/js/dataTables.js"></script>
<script src="/plugins/datatables/dataTables.tailwindcss.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type="text/javascript">
var table;
function showToast(title, message) {
$('#toastTitle').text(title);
$('#toastMessage').text(message);
$('#successToast').removeClass('hidden');
setTimeout(function () {
closeToast();
}, 3000);
}
function closeToast() {
$('#successToast').addClass('hidden');
}
function resetFormTambah() {
$('#form_tambah')[0].reset();
modal_tambah.close();
}
function resetFormEdit() {
$('#form_edit')[0].reset();
modal_edit.close();
}
$(document).ready(function () {
table = new DataTable('#faq-table', {
ajax: '@Url.Action("Table", "Faq")',
scrollX: true,
autoWidth: false,
initComplete: function () {
$('div.dt-scroll-body thead').css('visibility', 'collapse');
},
columnDefs: [
{ width: '5%', targets: 0 },
{ width: '35%', targets: 1 },
{ width: '45%', targets: 2 },
{ width: '15%', targets: 3 }
],
columns: [
{ data: 'no', orderable: false, searchable: false },
{
data: 'pertanyaan',
render: function (data) {
return `<div class="break-words whitespace-normal">${data}</div>`;
}
},
{
data: 'answer',
render: function (data) {
return `<div class="break-words whitespace-normal">${data}</div>`;
}
},
{ data: 'aksi', orderable: false, searchable: false },
]
});
// Form Tambah
$('#form_tambah').on('submit', function (e) {
e.preventDefault();
let formData = $(this).serialize();
$.ajax({
url: '@Url.Action("Create", "Faq")',
type: 'POST',
data: formData,
success: function (response) {
if (response.success) {
showToast('FAQ berhasil ditambah!', 'Berhasil! FAQ baru telah berhasil ditambahkan.');
modal_tambah.close();
$('#form_tambah')[0].reset();
table.ajax.reload();
}
},
error: function () {
showToast('Gagal!', 'Terjadi kesalahan saat menyimpan data.');
}
});
});
// Form Edit
$('#form_edit').on('submit', function (e) {
e.preventDefault();
let formData = $(this).serialize();
$.ajax({
url: '@Url.Action("Update", "Faq")',
type: 'POST',
data: formData,
success: function (response) {
if (response.success) {
showToast('FAQ berhasil diubah!', 'Berhasil! FAQ telah berhasil diubah.');
modal_edit.close();
table.ajax.reload();
}
},
error: function () {
showToast('Gagal!', 'Terjadi kesalahan saat mengupdate data.');
}
});
});
// Button Edit
table.on("click", ".btn-edit-faq", function (e) {
e.preventDefault();
let rowData = table.row($(this).closest('tr')).data();
// default data
$('#edit_id').val(rowData.id);
$('#edit_pertanyaan').val(rowData.pertanyaan);
$('#edit_jawaban').val(rowData.answer);
modal_edit.showModal();
});
// Button Detail
table.on("click", ".btn-detail-faq", function (e) {
e.preventDefault();
let rowData = table.row($(this).closest('tr')).data();
$('#detail_pertanyaan').text(rowData.pertanyaan);
$('#detail_jawaban').text(rowData.answer);
// Simpan data
$('#btn_edit_from_detail').data('rowData', rowData);
modal_detail.showModal();
});
// Edit from
$('#btn_edit_from_detail').on('click', function () {
let rowData = $(this).data('rowData');
modal_detail.close();
$('#edit_id').val(rowData.id);
$('#edit_pertanyaan').val(rowData.pertanyaan);
$('#edit_jawaban').val(rowData.answer);
modal_edit.showModal();
});
});
</script>