import { applyLang, avatarError, bindHeader, connectToApp, createMask, get, historyMoreUrl, isCompletedStatus, money, statusClass, statusText, t, toast } from "../bd-static/shared.js";
const config = {
title: "agency_list_link",
billType: "BD",
historyTitle: "revenue_details",
listSummary: (data) => `${t("team_recharge")}: ${data.totalRecharge || 0} ${t("team_salary")}: $${data.totalSalary || 0}`,
itemSide: (item) => [`${t("salary")}: $${item.teamSalaryAmount || 0}`, `${t("recharge")}: $${item.teamRechargeAmount || 0}`, `${t("host")}: ${item.teamMemberCount || 0}`],
historyEndpoint: "/team/bd/history",
moreEndpoint: (billBelong) => historyMoreUrl("/team/bd/history/more", billBelong),
countLabel: "agency_number"
};
init(config);
async function init(settings) {
bindHeader(settings.title);
applyLang();
document.getElementById("historyButton").addEventListener("click", () => showHistory(settings));
try {
await connectToApp();
const response = await get(`/team/bd/member-bill/list?type=${settings.billType}`);
renderList(response.body || {}, settings);
} catch (error) {
toast(error.response?.errorMsg || error.message);
}
}
function renderList(data, settings) {
document.getElementById("billTitle").textContent = data.billTitle || "";
document.getElementById("summaryLine").textContent = settings.listSummary(data);
const list = document.getElementById("memberList");
list.innerHTML = "";
(data.memberBillList || []).forEach((item) => {
const card = document.createElement("article");
card.className = "member-card";
card.innerHTML = `
${item.agentName || "-"}
${t("user_id_prefix")}${item.agentAccount || "-"}
${settings.itemSide(item).map((line) => `
${line}
`).join("")}
`;
avatarError(card.querySelector("img"));
list.appendChild(card);
});
}
async function showHistory(settings) {
try {
const historyResponse = await get(settings.historyEndpoint);
const listResponse = await get(`/team/bd/member-bill/list?type=${settings.billType}`);
const history = historyResponse.body || {};
const bill = listResponse.body || {};
if (bill.bdHistoryCO) {
history.incomeDetails = history.incomeDetails || [];
history.incomeDetails.unshift(bill.bdHistoryCO);
}
const sheet = document.createElement("section");
sheet.className = "sheet";
sheet.innerHTML = `
${t(settings.historyTitle)}
${t("total_income")} $${money(history.totalIncome || 0)}
${t("previous_period_income")}: $${money(history.previousPeriodIncome || 0)}
${t("income_details_label")}
`;
createMask(sheet);
const holder = sheet.querySelector("#historyItems");
(history.incomeDetails || []).forEach((item) => holder.appendChild(historyItem(item, settings)));
} catch (error) {
toast(error.response?.errorMsg || error.message);
}
}
function historyItem(item, settings) {
const card = document.createElement("article");
card.className = `salary-item ${statusClass(item.statusText)}`;
const details = item.statusText === "In Progress"
? [
`${item.billTitle || ""}
`,
`${t("team_salary")}: $${item.teamSalaryAmount || 0}${t("bd_income_salary")}: $${item.bdIncomeSalary || 0} (${item.bdRatioSalary || 0}%)
`,
`${t("team_top_up")}: $${item.teamRechargeAmount || 0}${t("bd_income_top_up")}: $${item.bdIncomeRecharge || 0} (${item.bdRatioRecharge || 0}%)
`,
`${t(settings.countLabel)}: ${item.agencyNumber || item.bdNumber || 0}
`
]
: [
`${item.billTitle || ""}
`,
item.hitPolicyType === "TEAM_SALARY" ? `${t("team_salary")}: $${item.teamSalaryAmount || 0}
${t("bd_income_salary")}: $${item.settlementSalary || 0} (${item.commissionRate || 0}%)
` : "",
item.hitPolicyType === "TEAM_RECHARGE" ? `${t("team_top_up")}: $${item.teamRechargeAmount || 0}
${t("bd_income_top_up")}: $${item.settlementSalary || 0} (${item.commissionRate || 0}%)
` : "",
`${t("num_teams") || t(settings.countLabel)} ${item.agencyNumber || item.bdNumber || 0}
`
];
card.innerHTML = `${statusText(item.statusText)}
${details.join("")}`;
if (isCompletedStatus(item.statusText)) {
const more = document.createElement("button");
more.className = "more-btn";
more.type = "button";
more.textContent = t("more");
more.addEventListener("click", () => showMore(item.billBelong, settings));
card.appendChild(more);
}
return card;
}
async function showMore(billBelong, settings) {
try {
const endpoint = settings.moreEndpoint(billBelong);
if (!endpoint) return;
const response = await get(endpoint);
const data = response.body || {};
const sheet = document.createElement("section");
sheet.className = "sheet";
sheet.innerHTML = `
${data.period || ""}
${t("team_top_up")}: $${data.teamTotalRecharge || data.teamRecharge || 0}
${t("total_team_salary")}: $${data.teamTotalSalary || data.teamSalary || 0}
`;
createMask(sheet);
const list = sheet.querySelector("#moreMembers");
(data.memberDetails || []).forEach((item) => {
const card = document.createElement("article");
card.className = "member-card";
card.innerHTML = `
${item.userName || item.bdUserName || "-"}
${t("user_id_prefix")}${item.account || "-"}
${t("salary")}: $${item.salary || 0}
${t("recharge")}: $${item.recharge || 0}
${t("host")}: ${item.hostCount || item.agencyCount || 0}
`;
avatarError(card.querySelector("img"));
list.appendChild(card);
});
} catch (error) {
toast(error.response?.errorMsg || error.message);
}
}