141 lines
4.5 KiB
JavaScript
141 lines
4.5 KiB
JavaScript
formCreate.on("submit", async function(e){
|
|
e.preventDefault()
|
|
const formData = new FormData(formCreate[0])
|
|
try {
|
|
const response = await fetch(formCreate.attr("action"), {
|
|
method:"POST",
|
|
body:formData,
|
|
})
|
|
|
|
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()
|
|
$("#create_modal").modal('hide')
|
|
dataTable.bootstrapTable("refresh")
|
|
}
|
|
} catch (error) {
|
|
Swal.fire({
|
|
title:"Error",
|
|
text:"Message:" + error,
|
|
icon:"error",
|
|
})
|
|
}
|
|
})
|
|
|
|
document.getElementById('togglePassword').addEventListener('click', function () {
|
|
const passwordInput = document.getElementById('password');
|
|
const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
|
|
passwordInput.setAttribute('type', type);
|
|
this.textContent = type === 'password' ? '👁️' : '🙈';
|
|
});
|
|
|
|
|
|
document.getElementById('togglePassword_edit').addEventListener('click', function () {
|
|
const passwordInput = document.getElementById('password_edit');
|
|
const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
|
|
passwordInput.setAttribute('type', type);
|
|
this.textContent = type === 'password' ? '👁️' : '🙈';
|
|
});
|
|
|
|
function editData(value){
|
|
const uid = $(value).data('uid')
|
|
const el = $(value)
|
|
$("#name_edit").val(el.data('name'))
|
|
$("#email_edit").val(el.data('email'))
|
|
$("#edit_modal").modal("show")
|
|
formEdit.attr('action', `/karyawan/${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(id, name){
|
|
Swal.fire({
|
|
title: "Apakah kamu yakin ingin menghapus data ini?",
|
|
text : 'Akses ' + name,
|
|
icon: "warning",
|
|
showCancelButton: true
|
|
}).then((result) => {
|
|
if(result.isConfirmed){
|
|
fetch(`/karyawan/${id}`, {
|
|
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"
|
|
});
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
|