156 lines
4.3 KiB
PHP
156 lines
4.3 KiB
PHP
<div class="container mt-4">
|
|
<div class="card shadow-sm">
|
|
<div class="card-body">
|
|
<div class="row">
|
|
<div class="col-6 center-v">
|
|
<h5>Daftar User Management</h5>
|
|
</div>
|
|
<div class="col-6 text-end">
|
|
<button class="btn btn-primary btn-sm mb-3" id="btnAdd">Tambah User</button>
|
|
</div>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-bordered" id="userTable">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th class="mw-20">No</th>
|
|
<th>Nama</th>
|
|
<th>Username</th>
|
|
<th>Role</th>
|
|
<th class="mw-70">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody></tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal -->
|
|
<div class="modal fade" id="userModal" tabindex="-1" aria-labelledby="userModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content" id="modalContent">
|
|
<!-- konten add/edit akan dimuat via AJAX -->
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script>
|
|
$(document).ready(function() {
|
|
|
|
// 🔹 Load Data Awal
|
|
function loadData() {
|
|
$.ajax({
|
|
url: "<?= base_url('user_management/get_list'); ?>",
|
|
type: "GET",
|
|
dataType: "json",
|
|
success: function(res) {
|
|
let html = "";
|
|
if (res.length > 0) {
|
|
$.each(res, function(i, row) {
|
|
html += `
|
|
<tr>
|
|
<td class="center-all mw-20">${i+1}</td>
|
|
<td>${row.name}</td>
|
|
<td>${row.username}</td>
|
|
<td class="center-all">${row.role}</td>
|
|
<td class="center-all mw-70">
|
|
<button class="btn btn-sm btn-warning btnEdit" style="margin-right: 5px;" data-id="${row.id}">Edit</button>
|
|
<button class="btn btn-sm btn-danger btnDelete" data-id="${row.id}">Hapus</button>
|
|
</td>
|
|
</tr>`;
|
|
});
|
|
} else {
|
|
html = `<tr><td colspan="4" class="text-center">Belum ada data</td></tr>`;
|
|
}
|
|
$("#userTable tbody").html(html);
|
|
}
|
|
});
|
|
}
|
|
loadData();
|
|
|
|
// 🔹 Tombol Tambah
|
|
$("#btnAdd").on("click", function() {
|
|
$("#modalContent").load("<?= base_url('user_management/add'); ?>", function() {
|
|
$("#userModal").modal("show");
|
|
});
|
|
});
|
|
|
|
// 🔹 Tombol Edit
|
|
$(document).on("click", ".btnEdit", function() {
|
|
let id = $(this).data("id");
|
|
$("#modalContent").load("<?= base_url('user_management/edit/'); ?>" + id, function() {
|
|
$("#userModal").modal("show");
|
|
});
|
|
});
|
|
|
|
// 🔹 Submit Form Add/Edit
|
|
$(document).on("submit", "#formUser", function(e) {
|
|
e.preventDefault();
|
|
let form = $(this);
|
|
$.ajax({
|
|
url: form.attr("action"),
|
|
type: "POST",
|
|
data: form.serialize(),
|
|
dataType: "json",
|
|
success: function(res) {
|
|
if (res.status == "success") {
|
|
$("#userModal").modal("hide");
|
|
loadData();
|
|
Swal.fire("Berhasil!", res.message, "success");
|
|
} else {
|
|
Swal.fire("Gagal!", res.message, "error");
|
|
}
|
|
},
|
|
error: function() {
|
|
Swal.fire("Error!", "Terjadi kesalahan pada server", "error");
|
|
}
|
|
});
|
|
});
|
|
|
|
// 🔹 DELETE DENGAN SWEETALERT
|
|
$(document).on("click", ".btnDelete", function() {
|
|
const id = $(this).data("id");
|
|
|
|
Swal.fire({
|
|
title: "Yakin hapus user ini?",
|
|
text: "Data yang dihapus tidak dapat dikembalikan!",
|
|
icon: "warning",
|
|
showCancelButton: true,
|
|
confirmButtonColor: "#d33",
|
|
cancelButtonColor: "#3085d6",
|
|
confirmButtonText: "Ya, hapus!",
|
|
cancelButtonText: "Batal"
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
$.ajax({
|
|
url: "<?= site_url('user_management/delete/'); ?>" + id,
|
|
type: "POST",
|
|
dataType: "json",
|
|
success: function(res) {
|
|
if (res.status === "success") {
|
|
Swal.fire({
|
|
icon: "success",
|
|
title: "Berhasil!",
|
|
text: res.message,
|
|
timer: 1500,
|
|
showConfirmButton: false
|
|
});
|
|
loadData();
|
|
} else {
|
|
Swal.fire("Gagal!", res.message, "error");
|
|
}
|
|
},
|
|
error: function() {
|
|
Swal.fire("Error!", "Terjadi kesalahan di server.", "error");
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
});
|
|
</script>
|