"use strict";
const common_vendor = require("../../common/vendor.js");
const common_http = require("../../common/http.js");
const _sfc_main = {
  data() {
    return {
      id: null,
      form: { name: "", type: "cash", bankName: "", bankAccount: "", openingBalance: "" },
      showType: false,
      types: [
        { key: "cash", name: "现金" },
        { key: "bank", name: "银行存款" },
        { key: "wechat", name: "微信" },
        { key: "alipay", name: "支付宝" },
        { key: "other", name: "其他" }
      ]
    };
  },
  onLoad(q) {
    this.id = q && q.id ? Number(q.id) : null;
    if (this.id)
      this.load();
  },
  methods: {
    typeLabel(t) {
      const m = { cash: "现金", bank: "银行存款", wechat: "微信", alipay: "支付宝", other: "其他" };
      return m[t] || t;
    },
    async load() {
      try {
        const list = await common_http.get("/api/accounts");
        const a = (Array.isArray(list) ? list : (list == null ? void 0 : list.list) || []).find((x) => x.id == this.id);
        if (a) {
          this.form = { name: a.name, type: a.type, bankName: a.bank_name || a.bankName || "", bankAccount: a.bank_account || a.bankAccount || "", openingBalance: "" };
        }
      } catch (e) {
      }
    },
    async save() {
      if (!this.form.name) {
        common_vendor.index.showToast({ title: "请输入名称", icon: "none" });
        return;
      }
      try {
        const body = { ...this.form, openingBalance: Number(this.form.openingBalance || 0) };
        if (this.id)
          await common_http.put(`/api/accounts/${this.id}`, body);
        else
          await common_http.post("/api/accounts", { ...body, status: 1 });
        common_vendor.index.showToast({ title: "已保存", icon: "success" });
        setTimeout(() => common_vendor.index.navigateBack(), 300);
      } catch (e) {
        common_vendor.index.showToast({ title: "保存失败", icon: "none" });
      }
    }
  }
};
if (!Array) {
  const _component_uni_popup = common_vendor.resolveComponent("uni-popup");
  _component_uni_popup();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: $data.form.name,
    b: common_vendor.o(($event) => $data.form.name = $event.detail.value),
    c: common_vendor.t($options.typeLabel($data.form.type)),
    d: common_vendor.o(($event) => $data.showType = true),
    e: $data.form.type === "bank"
  }, $data.form.type === "bank" ? {
    f: $data.form.bankName,
    g: common_vendor.o(($event) => $data.form.bankName = $event.detail.value)
  } : {}, {
    h: $data.form.type === "bank"
  }, $data.form.type === "bank" ? {
    i: $data.form.bankAccount,
    j: common_vendor.o(($event) => $data.form.bankAccount = $event.detail.value)
  } : {}, {
    k: $data.form.openingBalance,
    l: common_vendor.o(($event) => $data.form.openingBalance = $event.detail.value),
    m: common_vendor.o((...args) => $options.save && $options.save(...args)),
    n: common_vendor.f($data.types, (t, k0, i0) => {
      return {
        a: common_vendor.t(t.name),
        b: t.key,
        c: common_vendor.o(($event) => {
          $data.form.type = t.key;
          $data.showType = false;
        }, t.key)
      };
    }),
    o: common_vendor.o(($event) => $data.showType = false),
    p: common_vendor.sr("popup", "42e126a9-0"),
    q: common_vendor.o(($event) => $data.showType = $event),
    r: common_vendor.p({
      type: "bottom",
      modelValue: $data.showType
    })
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/account/form.js.map
