"use strict";
const common_vendor = require("../../common/vendor.js");
const common_http = require("../../common/http.js");
const ImageUploader = () => "../../components/ImageUploader.js";
const _sfc_main = {
  components: { ImageUploader },
  data() {
    return {
      id: "",
      form: {
        name: "",
        barcode: "",
        brand: "",
        model: "",
        spec: "",
        origin: "",
        categoryId: "",
        unitId: "",
        stock: null,
        safeMin: null,
        safeMax: null,
        purchasePrice: null,
        retailPrice: null,
        wholesalePrice: null,
        bigClientPrice: null,
        images: [],
        remark: ""
      },
      units: [],
      categories: []
    };
  },
  onLoad(query) {
    this.id = (query == null ? void 0 : query.id) || "";
    this.bootstrap();
  },
  computed: {
    unitNames() {
      return this.units.map((u) => u.name);
    },
    categoryNames() {
      return this.categories.map((c) => c.name);
    },
    unitLabel() {
      const u = this.units.find((x) => String(x.id) === String(this.form.unitId));
      return u ? u.name : "选择单位";
    },
    categoryLabel() {
      const c = this.categories.find((x) => String(x.id) === String(this.form.categoryId));
      return c ? c.name : "选择类别";
    }
  },
  methods: {
    async bootstrap() {
      await Promise.all([this.fetchUnits(), this.fetchCategories()]);
      if (this.id)
        this.loadDetail();
    },
    async fetchUnits() {
      try {
        const res = await common_http.get("/api/product-units");
        this.units = Array.isArray(res == null ? void 0 : res.list) ? res.list : Array.isArray(res) ? res : [];
      } catch (_) {
      }
    },
    async fetchCategories() {
      try {
        const res = await common_http.get("/api/product-categories");
        this.categories = Array.isArray(res == null ? void 0 : res.list) ? res.list : Array.isArray(res) ? res : [];
      } catch (_) {
      }
    },
    onPickUnit(e) {
      const idx = Number(e.detail.value);
      const u = this.units[idx];
      this.form.unitId = u ? u.id : "";
    },
    onPickCategory(e) {
      const idx = Number(e.detail.value);
      const c = this.categories[idx];
      this.form.categoryId = c ? c.id : "";
    },
    scan() {
      common_vendor.index.scanCode({ onlyFromCamera: false, success: (res) => {
        this.form.barcode = res.result;
      } });
    },
    async loadDetail() {
      try {
        const data = await common_http.get("/api/products/" + this.id);
        Object.assign(this.form, {
          name: data.name,
          barcode: data.barcode,
          brand: data.brand,
          model: data.model,
          spec: data.spec,
          origin: data.origin,
          categoryId: data.categoryId,
          unitId: data.unitId,
          stock: data.stock,
          safeMin: data.safeMin,
          safeMax: data.safeMax,
          purchasePrice: data.purchasePrice,
          retailPrice: data.retailPrice,
          wholesalePrice: data.wholesalePrice,
          bigClientPrice: data.bigClientPrice,
          images: (data.images || []).map((i) => i.url || i)
        });
      } catch (_) {
      }
    },
    validate() {
      if (!this.form.name) {
        common_vendor.index.showToast({ title: "请填写名称", icon: "none" });
        return false;
      }
      if (this.form.safeMin != null && this.form.safeMax != null && Number(this.form.safeMin) > Number(this.form.safeMax)) {
        common_vendor.index.showToast({ title: "安全库存区间不合法", icon: "none" });
        return false;
      }
      return true;
    },
    buildPayload() {
      const f = this.form;
      return {
        name: f.name,
        barcode: f.barcode,
        brand: f.brand,
        model: f.model,
        spec: f.spec,
        origin: f.origin,
        categoryId: f.categoryId || null,
        unitId: f.unitId,
        safeMin: f.safeMin,
        safeMax: f.safeMax,
        prices: {
          purchasePrice: f.purchasePrice,
          retailPrice: f.retailPrice,
          wholesalePrice: f.wholesalePrice,
          bigClientPrice: f.bigClientPrice
        },
        stock: f.stock,
        images: f.images,
        remark: f.remark
      };
    },
    async save(goOn) {
      if (!this.validate())
        return;
      const payload = this.buildPayload();
      try {
        if (this.id)
          await common_http.put("/api/products/" + this.id, payload);
        else
          await common_http.post("/api/products", payload);
        common_vendor.index.showToast({ title: "保存成功", icon: "success" });
        if (goOn && !this.id) {
          this.form = { name: "", barcode: "", brand: "", model: "", spec: "", origin: "", categoryId: "", unitId: "", stock: null, safeMin: null, safeMax: null, purchasePrice: null, retailPrice: null, wholesalePrice: null, bigClientPrice: null, images: [], remark: "" };
        } else {
          setTimeout(() => common_vendor.index.navigateBack(), 400);
        }
      } catch (e) {
        common_vendor.index.showToast({ title: "保存失败", icon: "none" });
      }
    }
  }
};
if (!Array) {
  const _component_ImageUploader = common_vendor.resolveComponent("ImageUploader");
  _component_ImageUploader();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return {
    a: $data.form.name,
    b: common_vendor.o(common_vendor.m(($event) => $data.form.name = $event.detail.value, {
      trim: true
    })),
    c: $data.form.barcode,
    d: common_vendor.o(common_vendor.m(($event) => $data.form.barcode = $event.detail.value, {
      trim: true
    })),
    e: $data.form.brand,
    f: common_vendor.o(common_vendor.m(($event) => $data.form.brand = $event.detail.value, {
      trim: true
    })),
    g: $data.form.model,
    h: common_vendor.o(common_vendor.m(($event) => $data.form.model = $event.detail.value, {
      trim: true
    })),
    i: $data.form.spec,
    j: common_vendor.o(common_vendor.m(($event) => $data.form.spec = $event.detail.value, {
      trim: true
    })),
    k: $data.form.origin,
    l: common_vendor.o(common_vendor.m(($event) => $data.form.origin = $event.detail.value, {
      trim: true
    })),
    m: common_vendor.t($options.unitLabel),
    n: $options.unitNames,
    o: common_vendor.o((...args) => $options.onPickUnit && $options.onPickUnit(...args)),
    p: common_vendor.t($options.categoryLabel),
    q: $options.categoryNames,
    r: common_vendor.o((...args) => $options.onPickCategory && $options.onPickCategory(...args)),
    s: $data.form.stock,
    t: common_vendor.o(common_vendor.m(($event) => $data.form.stock = $event.detail.value, {
      number: true
    })),
    v: $data.form.safeMin,
    w: common_vendor.o(common_vendor.m(($event) => $data.form.safeMin = $event.detail.value, {
      number: true
    })),
    x: $data.form.safeMax,
    y: common_vendor.o(common_vendor.m(($event) => $data.form.safeMax = $event.detail.value, {
      number: true
    })),
    z: $data.form.purchasePrice,
    A: common_vendor.o(common_vendor.m(($event) => $data.form.purchasePrice = $event.detail.value, {
      number: true
    })),
    B: $data.form.retailPrice,
    C: common_vendor.o(common_vendor.m(($event) => $data.form.retailPrice = $event.detail.value, {
      number: true
    })),
    D: $data.form.wholesalePrice,
    E: common_vendor.o(common_vendor.m(($event) => $data.form.wholesalePrice = $event.detail.value, {
      number: true
    })),
    F: $data.form.bigClientPrice,
    G: common_vendor.o(common_vendor.m(($event) => $data.form.bigClientPrice = $event.detail.value, {
      number: true
    })),
    H: common_vendor.o(($event) => $data.form.images = $event),
    I: common_vendor.p({
      formData: {
        ownerType: "product"
      },
      modelValue: $data.form.images
    }),
    J: $data.form.remark,
    K: common_vendor.o(common_vendor.m(($event) => $data.form.remark = $event.detail.value, {
      trim: true
    })),
    L: common_vendor.o(($event) => $options.save(false)),
    M: common_vendor.o(($event) => $options.save(true))
  };
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/product/form.js.map
