// Global variables let ajaxUrl, storeUrl, table; $.ajaxSetup({ headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"), }, }); $(document).ready(function () { // Get URLs from hidden inputs ajaxUrl = $('input[name="ajax_url"]').val(); storeUrl = $('input[name="store_url"]').val(); // Initialize DataTable table = $("#kt_table").DataTable({ processing: true, serverSide: true, ajax: { url: ajaxUrl, }, columns: [ { data: "category_name", name: "c.name" }, { data: "name", name: "w.name" }, { data: "shortname", name: "w.shortname" }, { data: "desc", name: "w.desc" }, { data: "action", name: "action", orderable: false, searchable: false, className: "text-center", width: "auto", render: function (data, type, row) { return data; }, }, ], responsive: true, autoWidth: false, columnDefs: [ { targets: -1, // Action column className: "text-center", width: "auto", orderable: false, }, ], }); // Initialize Select2 $("#category_id").select2({ placeholder: "-- Pilih Kategori --", allowClear: true, width: "100%", dropdownParent: $("#workModal"), language: { noResults: function () { return "Tidak ada hasil yang ditemukan"; }, searching: function () { return "Mencari..."; }, }, }); // Modal close handlers $(document).on("click", '[data-dismiss="modal"]', function () { $(this).closest(".modal").modal("hide"); }); $(document).on("click", ".modal .close", function () { $(this).closest(".modal").modal("hide"); }); $(document).on("click", ".modal", function (e) { if (e.target === this) { $(this).modal("hide"); } }); $(document).on("keydown", function (e) { if (e.keyCode === 27) { $(".modal.show").modal("hide"); } }); // Bootstrap 5 fallback $(document).on("click", '[data-bs-dismiss="modal"]', function () { $(this).closest(".modal").modal("hide"); }); // Alternative close button selectors $(document).on("click", ".btn-secondary", function () { if ($(this).closest(".modal").length) { $(this).closest(".modal").modal("hide"); } }); // Force close function window.closeModal = function (modalId) { $("#" + modalId).modal("hide"); }; // Modal hidden event $("#workModal").on("hidden.bs.modal", function () { $("#workForm").trigger("reset"); $("#category_id").val("").trigger("change"); $('#workForm input[name="_method"]').remove(); }); // Add Work $("#addWork").click(function () { $("#workModal").modal("show"); let form_action = storeUrl; $("#workForm").attr("action", form_action); $("#workForm input[name='_method']").remove(); $("#workForm").attr("data-form", "store"); $("#workForm").trigger("reset"); $("#workForm textarea[name='desc']").val(""); // Reset Select2 $("#category_id").val("").trigger("change"); }); // Submit Form $("#workForm").submit(function (e) { e.preventDefault(); let dataForm = $("#workForm").attr("data-form"); if (dataForm == "store") { $.ajax({ url: $("#workForm").attr("action"), type: "POST", data: $("#workForm").serialize(), success: function (res) { $("#workModal").modal("hide"); $("#workForm").trigger("reset"); $("#category_id").val("").trigger("change"); table.ajax.reload(); Swal.fire({ icon: "success", title: "Berhasil!", text: "Data pekerjaan berhasil disimpan.", timer: 2000, showConfirmButton: false, }); }, }); } else if (dataForm == "update") { $.ajax({ url: $("#workForm").attr("action"), type: "POST", data: $("#workForm").serialize(), success: function (res) { $("#workModal").modal("hide"); $("#workForm").trigger("reset"); $("#category_id").val("").trigger("change"); table.ajax.reload(); Swal.fire({ icon: "success", title: "Berhasil!", text: "Data pekerjaan berhasil diupdate.", timer: 2000, showConfirmButton: false, }); }, }); } }); }); // Global functions for edit and delete function destroyWork(id) { let action = $("#destroyWork" + id).attr("data-action"); Swal.fire({ title: "Hapus Pekerjaan?", text: "Anda tidak akan bisa mengembalikannya!", showCancelButton: true, confirmButtonColor: "#d33", cancelButtonColor: "#dedede", confirmButtonText: "Hapus", }).then((result) => { if (result.value) { $.ajax({ url: action, type: "POST", data: { _token: $('meta[name="csrf-token"]').attr("content"), _method: "DELETE", }, success: function (res) { Swal.fire( "Berhasil!", "Pekerjaan berhasil dihapus.", "success" ); if (table) { table.ajax.reload(); } }, }); } }); } function editWork(id) { let form_action = $("#editWork" + id).attr("data-action"); let edit_url = $("#editWork" + id).attr("data-url"); $("#workModal").modal("show"); $("#workForm").append(''); $("#workForm").attr("action", form_action); $("#workForm").attr("data-form", "update"); $.get(edit_url, function (res) { $("#workForm input[name='name']").val(res.data.name); $("#workForm input[name='shortname']").val(res.data.shortname); $("#workForm textarea[name='desc']").html(res.data.desc); $("#workForm select[name='category_id']") .val(res.data.category_id) .trigger("change"); }); }