"use strict";
const common_vendor = require("../../common/vendor.js");
const common_http = require("../../common/http.js");
const _sfc_main = {
  data() {
    return { id: null, d: {}, editing: false, form: { name: "", contactName: "", mobile: "", phone: "", address: "", level: "", priceLevel: "零售价", arOpening: 0, remark: "" }, priceLevels: ["零售价", "批发价", "大单报价"], priceLabels: ["零售价", "批发价", "大单报价"], priceIdx: 0 };
  },
  onLoad(q) {
    if (q && q.id) {
      this.id = Number(q.id);
      this.fetch();
    }
  },
  methods: {
    async fetch() {
      try {
        this.d = await common_http.get(`/api/customers/${this.id}`);
        this.form = {
          name: this.d.name || "",
          contactName: this.d.contactName || "",
          mobile: this.d.mobile || "",
          phone: this.d.phone || "",
          address: this.d.address || "",
          level: this.d.level || "",
          priceLevel: this.d.priceLevel || "retail",
          arOpening: Number(this.d.arOpening || 0),
          remark: this.d.remark || ""
        };
        const idx = this.priceLevels.indexOf(this.form.priceLevel);
        this.priceIdx = idx >= 0 ? idx : 0;
      } catch (e) {
        common_vendor.index.showToast({ title: "加载失败", icon: "none" });
      }
    },
    toggleEdit() {
      this.editing = !this.editing;
    },
    onPriceChange(e) {
      this.priceIdx = Number(e.detail.value);
      this.form.priceLevel = this.priceLevels[this.priceIdx];
    },
    choose() {
      const pages = getCurrentPages();
      let targetIdx = -1;
      for (let i = pages.length - 2; i >= 0; i--) {
        const vm = pages[i] && pages[i].$vm ? pages[i].$vm : null;
        if (vm && vm.order) {
          vm.order.customerId = this.d.id;
          vm.customerName = this.d.name;
          targetIdx = i;
          break;
        }
      }
      if (targetIdx >= 0) {
        const delta = pages.length - 1 - targetIdx;
        common_vendor.index.navigateBack({ delta });
      } else {
        common_vendor.index.navigateBack();
      }
    },
    async save() {
      if (!this.form.name)
        return common_vendor.index.showToast({ title: "请填写客户名称", icon: "none" });
      try {
        await common_http.put(`/api/customers/${this.id}`, this.form);
        common_vendor.index.showToast({ title: "已保存", icon: "success" });
        this.editing = false;
        await this.fetch();
      } catch (e) {
        common_vendor.index.showToast({ title: (e == null ? void 0 : e.message) || "保存失败", icon: "none" });
      }
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: !$data.editing
  }, !$data.editing ? {
    b: common_vendor.t($data.d.name)
  } : {
    c: $data.form.name,
    d: common_vendor.o(($event) => $data.form.name = $event.detail.value)
  }, {
    e: !$data.editing
  }, !$data.editing ? {
    f: common_vendor.t($data.d.contactName || "—")
  } : {
    g: $data.form.contactName,
    h: common_vendor.o(($event) => $data.form.contactName = $event.detail.value)
  }, {
    i: !$data.editing
  }, !$data.editing ? {
    j: common_vendor.t($data.d.mobile || "—")
  } : {
    k: $data.form.mobile,
    l: common_vendor.o(($event) => $data.form.mobile = $event.detail.value)
  }, {
    m: !$data.editing
  }, !$data.editing ? {
    n: common_vendor.t($data.d.phone || "—")
  } : {
    o: $data.form.phone,
    p: common_vendor.o(($event) => $data.form.phone = $event.detail.value)
  }, {
    q: !$data.editing
  }, !$data.editing ? {
    r: common_vendor.t($data.d.address || "—")
  } : {
    s: $data.form.address,
    t: common_vendor.o(($event) => $data.form.address = $event.detail.value)
  }, {
    v: !$data.editing
  }, !$data.editing ? {
    w: common_vendor.t($data.d.level || "—")
  } : {
    x: $data.form.level,
    y: common_vendor.o(($event) => $data.form.level = $event.detail.value)
  }, {
    z: !$data.editing
  }, !$data.editing ? {
    A: common_vendor.t($data.d.priceLevel)
  } : {
    B: common_vendor.t($data.priceLabels[$data.priceIdx]),
    C: $data.priceLabels,
    D: $data.priceIdx,
    E: common_vendor.o((...args) => $options.onPriceChange && $options.onPriceChange(...args))
  }, {
    F: !$data.editing
  }, !$data.editing ? {
    G: common_vendor.t(Number($data.d.arOpening || 0).toFixed(2))
  } : {
    H: $data.form.arOpening,
    I: common_vendor.o(common_vendor.m(($event) => $data.form.arOpening = $event.detail.value, {
      number: true
    }))
  }, {
    J: common_vendor.t(Number($data.d.receivable || 0).toFixed(2)),
    K: !$data.editing
  }, !$data.editing ? {
    L: common_vendor.t($data.d.remark || "—")
  } : {
    M: $data.form.remark,
    N: common_vendor.o(($event) => $data.form.remark = $event.detail.value)
  }, {
    O: common_vendor.t($data.editing ? "取消" : "编辑"),
    P: common_vendor.o((...args) => $options.toggleEdit && $options.toggleEdit(...args)),
    Q: $data.editing
  }, $data.editing ? {
    R: common_vendor.o((...args) => $options.save && $options.save(...args))
  } : {
    S: common_vendor.o((...args) => $options.choose && $options.choose(...args))
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/customer/detail.js.map
