62 lines
2.7 KiB
JavaScript
62 lines
2.7 KiB
JavaScript
(() => {
|
|
"use strict";
|
|
const body = document.body;
|
|
const form = document.getElementById("branchform");
|
|
const list = document.getElementById("branchListBody");
|
|
const message = document.getElementById("branchMessage");
|
|
|
|
const showMessage = (text, type = "success") => {
|
|
const parts = String(text || "").split(":");
|
|
message.textContent = parts.length > 2 ? parts.slice(2).join(":") : text;
|
|
message.className = `alert alert-${type} py-2 px-3 mb-2`;
|
|
};
|
|
|
|
const openBranch = async (branchId) => {
|
|
const params = new URLSearchParams({ pid: form.portfolio.value, bid: branchId, suuid: document.getElementById("suuid").value });
|
|
const result = await window.MatrixModal.openFrame({
|
|
title: branchId === "0" ? "New Branch" : "Edit Branch",
|
|
url: `${body.dataset.branchDetailUrl}?${params}`,
|
|
completionMessage: "matrix:branch-saved"
|
|
});
|
|
if (result.status === "saved") form.submit();
|
|
};
|
|
|
|
form.portfolio?.addEventListener("change", () => form.submit());
|
|
document.getElementById("addBranch")?.addEventListener("click", () => openBranch("0"));
|
|
|
|
list?.addEventListener("click", async (event) => {
|
|
const row = event.target.closest("tr[data-branch-id]");
|
|
if (!row) return;
|
|
if (event.target.closest(".edit-branch")) {
|
|
openBranch(row.dataset.branchId);
|
|
return;
|
|
}
|
|
const button = event.target.closest(".branch-status");
|
|
if (!button) return;
|
|
button.disabled = true;
|
|
try {
|
|
const response = await fetch(body.dataset.branchStatusUrl, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded;charset=ISO-8859-1" },
|
|
body: new URLSearchParams({ branch: row.dataset.branchId, oprby: document.getElementById("usid").value })
|
|
});
|
|
if (!response.ok) throw new Error(await response.text());
|
|
const result = JSON.parse(await response.text());
|
|
if (result.status === "failed") {
|
|
showMessage(result.message, "danger");
|
|
return;
|
|
}
|
|
const active = button.dataset.active !== "true";
|
|
button.dataset.active = String(active);
|
|
button.textContent = active ? "Active" : "Inactive";
|
|
button.className = `btn btn-sm branch-status ${active ? "btn-success" : "btn-outline-secondary"}`;
|
|
row.querySelector(".edit-branch").disabled = !active;
|
|
showMessage("Status changed successfully");
|
|
} catch (error) {
|
|
showMessage(error.message || "Could not change branch status", "danger");
|
|
} finally {
|
|
button.disabled = false;
|
|
}
|
|
});
|
|
})();
|