"use strict"; const common_http = require("../../common/http.js"); const common_vendor = require("../../common/vendor.js"); function formatDate(d) { const y = d.getFullYear(); const m = String(d.getMonth() + 1).padStart(2, "0"); const day = String(d.getDate()).padStart(2, "0"); return `${y}-${m}-${day}`; } const _sfc_main = { data() { const now = /* @__PURE__ */ new Date(); const start = new Date(now.getFullYear(), now.getMonth(), 1); return { startDate: formatDate(start), endDate: formatDate(now), dim: "customer", rows: [], summary: { salesAmount: 0, costAmount: 0, profit: 0, profitRate: 0, itemCount: 0 }, loading: false, error: "" }; }, onLoad(query) { try { const d = query && query.dim; if (d === "product" || d === "customer") this.dim = d; } catch (e) { } this.refresh(); }, computed: { profitRateText() { var _a; const rate = Number(((_a = this.summary) == null ? void 0 : _a.profitRate) || 0); return rate.toFixed(2) + "%"; }, summaryItems() { if (!this.rows.length) return []; return [ { label: "销售额", value: `¥ ${this.fmt(this.summary.salesAmount)}` }, { label: "成本", value: `¥ ${this.fmt(this.summary.costAmount)}` }, { label: "利润", value: `¥ ${this.fmt(this.summary.profit)}` }, { label: "利润率", value: this.profitRateText } ]; } }, methods: { fmt(n) { return Number(n || 0).toFixed(2); }, showProductSpec(row) { return this.dim === "product" && row && row.spec; }, rowMetrics(row) { if (!row) return []; return [ { label: "销售额", value: `¥ ${this.fmt(row.salesAmount)}` }, { label: "成本", value: `¥ ${this.fmt(row.costAmount)}` }, { label: "利润", value: `¥ ${this.fmt(row.profit)}` }, { label: "利润率", value: `${Number(row.profitRate || 0).toFixed(2)}%` } ]; }, setDimension(d) { if (d !== "customer" && d !== "product") return; if (this.dim === d) return; this.dim = d; this.refresh(); }, onStartChange(e) { this.startDate = e.detail.value; this.refresh(); }, onEndChange(e) { this.endDate = e.detail.value; this.refresh(); }, async refresh() { var _a, _b, _c, _d, _e; this.loading = true; this.error = ""; try { const resp = await common_http.get("/api/report/sales", { dimension: this.dim, startDate: this.startDate, endDate: this.endDate }); const items = Array.isArray(resp == null ? void 0 : resp.items) ? resp.items : []; this.rows = items.map((it) => ({ name: (it == null ? void 0 : it.name) || (this.dim === "product" ? "未命名商品" : "未指定客户"), spec: (it == null ? void 0 : it.spec) || "", salesAmount: Number((it == null ? void 0 : it.salesAmount) || 0), costAmount: Number((it == null ? void 0 : it.costAmount) || 0), profit: Number((it == null ? void 0 : it.profit) || 0), profitRate: Number((it == null ? void 0 : it.profitRate) || 0) })); this.summary = { salesAmount: Number(((_a = resp == null ? void 0 : resp.summary) == null ? void 0 : _a.salesAmount) || 0), costAmount: Number(((_b = resp == null ? void 0 : resp.summary) == null ? void 0 : _b.costAmount) || 0), profit: Number(((_c = resp == null ? void 0 : resp.summary) == null ? void 0 : _c.profit) || 0), profitRate: Number(((_d = resp == null ? void 0 : resp.summary) == null ? void 0 : _d.profitRate) || 0), itemCount: Number(((_e = resp == null ? void 0 : resp.summary) == null ? void 0 : _e.itemCount) || this.rows.length) }; } catch (e) { this.error = e && e.message || "报表加载失败"; } finally { this.loading = false; } } } }; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return common_vendor.e({ a: common_vendor.t($data.startDate), b: $data.startDate, c: common_vendor.o((...args) => $options.onStartChange && $options.onStartChange(...args)), d: common_vendor.t($data.endDate), e: $data.endDate, f: common_vendor.o((...args) => $options.onEndChange && $options.onEndChange(...args)), g: $data.dim === "customer" ? 1 : "", h: common_vendor.o(($event) => $options.setDimension("customer")), i: $data.dim === "product" ? 1 : "", j: common_vendor.o(($event) => $options.setDimension("product")), k: $options.summaryItems.length }, $options.summaryItems.length ? { l: common_vendor.f($options.summaryItems, (item, ix, i0) => { return { a: common_vendor.t(item.label), b: common_vendor.t(item.value), c: ix }; }) } : {}, { m: $data.loading }, $data.loading ? {} : $data.error ? { o: common_vendor.t($data.error) } : !$data.rows.length ? {} : { q: common_vendor.f($data.rows, (row, idx, i0) => { return common_vendor.e({ a: common_vendor.t(row.name), b: $options.showProductSpec(row) }, $options.showProductSpec(row) ? { c: common_vendor.t(row.spec) } : {}, { d: common_vendor.f($options.rowMetrics(row), (metric, mIdx, i1) => { return { a: common_vendor.t(metric.label), b: common_vendor.t(metric.value), c: mIdx }; }), e: idx }); }) }, { n: $data.error, p: !$data.rows.length }); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]); wx.createPage(MiniProgramPage); //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/report/index.js.map