class SalePage { static InitForm() { const $form = $("#sale-form"); $form.on("submit", event => { event.preventDefault(); SalePage.SaveForm(); }) } static InitSaleItemSection() { const $table = $("#sale-items-section table"); const $checkboxes = $table.find("tbody input[type=checkbox]"); const $allCheckbox = $("#sale-items-all-checkbox"); const $batchActions = $(".batch-action"); $allCheckbox.on("change", () => { const checked = $allCheckbox.prop("checked"); $checkboxes.prop("checked", checked).trigger("change"); }); $checkboxes.on("change", () => { let batchActionsEnabled = $checkboxes.filter(":checked").length > 0; $batchActions.prop("disabled", !batchActionsEnabled); }); $table.find(".btn-delete-sale-item").on("click", event => { const saleItemId = $(event.currentTarget).data("sale-item-id"); swal.fire({ icon: "question", title: "Deleting Item", text: "Are you sure you want to delete this lead item?", confirmButtonText: "Delete", showCancelButton: true }).then(result => { if(result.isConfirmed) { $.post(window.location + "/delete-item", { sale_item_id: saleItemId }).then(response => { toastr.success("Lead item deleted successfully."); $("#sale-items-section").html(response.html); SalePage.InitSaleItemSection(); }); } }); }); } static SaveForm() { const $form = $("#sale-form"); const action = $form.get(0).action; const data = $form.serialize(); const $submitElements = $form.find("[type=submit]"); $submitElements.prop("disabled", true); return $.post(action, data).always(() => { $submitElements.prop("disabled", false); }).then(() => { toastr.success("Lead saved successfully."); }); } } $(() => { SalePage.InitSaleItemSection(); SalePage.InitForm(); });