190 lines
6.1 KiB
JavaScript
190 lines
6.1 KiB
JavaScript
|
|
|
|
|
|
function editData(value){
|
|
const uid = $(value).data('uid')
|
|
const name = $(value).data('name')
|
|
const tarif = $(value).data('tarif')
|
|
$("#edit_modal").modal('show')
|
|
$("#edit_name").val(name)
|
|
$("#edit_tarif").val(tarif)
|
|
formEdit.attr('action', `/pelayanan/${uid}`)
|
|
|
|
formEdit.off("submit").on("submit", async function(e){
|
|
e.preventDefault()
|
|
const formDataEdit = new FormData(formEdit[0])
|
|
try {
|
|
const responseEdit = await fetch(formEdit.attr("action"), {
|
|
method:"POST",
|
|
body:formDataEdit,
|
|
})
|
|
|
|
const resultEdit = await responseEdit.json()
|
|
if(responseEdit.ok && resultEdit.status === "success"){
|
|
Swal.fire({
|
|
title:"Success",
|
|
text:"Data Berhasil diupdate",
|
|
icon:"success",
|
|
showConfirmButton: false,
|
|
timer:1200
|
|
});
|
|
|
|
formEdit[0].reset()
|
|
$("#edit_modal").modal('hide')
|
|
dataTable.bootstrapTable("refresh")
|
|
}
|
|
} catch (error) {
|
|
Swal.fire({
|
|
title:"Error",
|
|
text:"Message:" + error,
|
|
icon:"error",
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
function deleteData(uid, name){
|
|
Swal.fire({
|
|
title: "Apakah kamu yakin ingin menghapus data ini?",
|
|
text : name,
|
|
icon: "warning",
|
|
showCancelButton: true
|
|
}).then((result) => {
|
|
if(result.isConfirmed){
|
|
fetch(`/pelayanan/${uid}`, {
|
|
method:'DELETE',
|
|
headers: {
|
|
"X-CSRF-TOKEN": document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
|
|
"Content-Type": "application/json"
|
|
}
|
|
}).then((response) => {
|
|
if(!response.ok){
|
|
throw new Error("Network response was not ok");
|
|
}
|
|
return response.json();
|
|
})
|
|
.then((data) => {
|
|
if(data.status === "success"){
|
|
Swal.fire({
|
|
title: "Success",
|
|
text: "Data berhasil dihapus",
|
|
icon:"success",
|
|
showConfirmButton: false,
|
|
timer: 1000
|
|
}).then(() => {
|
|
dataTable.bootstrapTable("refresh")
|
|
})
|
|
}else{
|
|
Swal.fire({
|
|
title: "Error!",
|
|
text: data.message || "Failed to delete Schedule.",
|
|
icon: "error"
|
|
});
|
|
}
|
|
})
|
|
.catch(error => {
|
|
Swal.fire({
|
|
title: "Error!",
|
|
text: "Something went wrong. Please try again later.",
|
|
icon: "error"
|
|
});
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
let index = 1;
|
|
function addField(){
|
|
let container = document.getElementById('service-wrapper')
|
|
let field = `
|
|
<div class="service-item">
|
|
<div class="form-group">
|
|
<label>Nama Pelayanan</label>
|
|
<input type="text" class="form-control" name="data[${index}][name]" placeholder="Enter name">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Tarif</label>
|
|
<input type="text" class="form-control tarif-input" name="data[${index}][tarif]" placeholder="Enter tarif">
|
|
</div>
|
|
<button type="button" class="btn btn-danger btn-sm mb-2 remove-field">Hapus</button>
|
|
</div>
|
|
`;
|
|
container.insertAdjacentHTML('beforeend', field)
|
|
index++;
|
|
}
|
|
document.addEventListener('click', function(e) {
|
|
if (e.target && e.target.classList.contains('remove-field')) {
|
|
e.target.closest('.service-item').remove();
|
|
}
|
|
});
|
|
|
|
document.addEventListener('input', function(e) {
|
|
if (e.target && e.target.classList.contains('tarif-input')) {
|
|
let value = e.target.value.replace(/\D/g, ''); // hapus semua non-digit
|
|
value = new Intl.NumberFormat('id-ID').format(value); // format ke rupiah
|
|
e.target.value = value;
|
|
}
|
|
});
|
|
|
|
|
|
formCreate.on("submit", async function(e){
|
|
e.preventDefault()
|
|
formCreate.find('.tarif-input').each(function() {
|
|
let value = $(this).val().replace(/\./g, '');
|
|
$(this).val(value);
|
|
});
|
|
|
|
const formData = new FormData(formCreate[0])
|
|
|
|
try {
|
|
const response = await fetch(formCreate.attr("action"), {
|
|
method:"POST",
|
|
body:formData,
|
|
headers: {
|
|
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content
|
|
}
|
|
|
|
})
|
|
const result = await response.json()
|
|
if(response.ok && result.status === "success"){
|
|
Swal.fire({
|
|
title:"Success",
|
|
text:"Data Berhasil ditambahkan",
|
|
icon:"success",
|
|
showConfirmButton: false,
|
|
timer:1200
|
|
});
|
|
|
|
formCreate[0].reset()
|
|
$("#service-wrapper").html(''); // Kosongkan tambahan
|
|
|
|
// Reset field awal biar ada 1 input lagi
|
|
$("#service-container").html(`
|
|
<div class="service-item">
|
|
<div class="form-group">
|
|
<label>Nama Pelayanan</label>
|
|
<input type="text" class="form-control" name="data[0][name]" placeholder="Enter name">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Tarif</label>
|
|
<input type="text" class="form-control tarif-input" name="data[0][tarif]" placeholder="Enter tarif">
|
|
</div>
|
|
</div>
|
|
`);
|
|
|
|
// Fokuskan ke input pertama
|
|
formCreate.find('input[name="data[0][name]"]').first().focus();
|
|
|
|
$("#create_modal_service").modal('hide')
|
|
dataTable.bootstrapTable("refresh")
|
|
}
|
|
} catch (error) {
|
|
|
|
Swal.fire({
|
|
title:"Error",
|
|
text:"Message:" + error,
|
|
icon:"error",
|
|
})
|
|
}
|
|
})
|