"use strict";
const common_vendor = require("../../common/vendor.js");
const common_http = require("../../common/http.js");
const _sfc_main = {
  data() {
    return {
      items: [],
      query: { kw: "", page: 1, size: 20, categoryId: "" },
      finished: false,
      loading: false,
      tab: "all",
      categories: []
    };
  },
  onLoad() {
    this.fetchCategories();
    this.reload();
  },
  onShow() {
    this.reload();
  },
  computed: {
    categoryNames() {
      return this.categories.map((c) => c.name);
    },
    categoryLabel() {
      const c = this.categories.find((x) => String(x.id) === String(this.query.categoryId));
      return c ? "类别：" + c.name : "选择类别";
    }
  },
  methods: {
    switchTab(t) {
      this.tab = t;
      this.query.categoryId = "";
      this.reload();
    },
    onPickCategory(e) {
      const idx = Number(e.detail.value);
      const c = this.categories[idx];
      this.query.categoryId = c ? c.id : "";
      this.reload();
    },
    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 (_) {
      }
    },
    reload() {
      this.items = [];
      this.query.page = 1;
      this.finished = false;
      this.loadMore();
    },
    async loadMore() {
      if (this.loading || this.finished)
        return;
      this.loading = true;
      try {
        const params = { kw: this.query.kw, page: this.query.page, size: this.query.size };
        if (this.tab === "category" && this.query.categoryId)
          params.categoryId = this.query.categoryId;
        const res = await common_http.get("/api/products", params);
        const list = Array.isArray(res == null ? void 0 : res.list) ? res.list : Array.isArray(res) ? res : [];
        this.items = this.items.concat(list);
        if (list.length < this.query.size)
          this.finished = true;
        this.query.page += 1;
      } catch (e) {
        common_vendor.index.showToast({ title: "加载失败", icon: "none" });
      } finally {
        this.loading = false;
      }
    },
    openForm(id) {
      const url = "/pages/product/form" + (id ? "?id=" + id : "");
      common_vendor.index.navigateTo({ url });
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: $data.tab === "all" ? 1 : "",
    b: common_vendor.o(($event) => $options.switchTab("all")),
    c: $data.tab === "category" ? 1 : "",
    d: common_vendor.o(($event) => $options.switchTab("category")),
    e: common_vendor.o((...args) => $options.reload && $options.reload(...args)),
    f: $data.query.kw,
    g: common_vendor.o(common_vendor.m(($event) => $data.query.kw = $event.detail.value, {
      trim: true
    })),
    h: $data.tab === "category"
  }, $data.tab === "category" ? {
    i: common_vendor.t($options.categoryLabel),
    j: $options.categoryNames,
    k: common_vendor.o((...args) => $options.onPickCategory && $options.onPickCategory(...args))
  } : {}, {
    l: common_vendor.o((...args) => $options.reload && $options.reload(...args)),
    m: $data.items.length
  }, $data.items.length ? {
    n: common_vendor.f($data.items, (it, k0, i0) => {
      return common_vendor.e({
        a: it.cover
      }, it.cover ? {
        b: it.cover
      } : {}, {
        c: common_vendor.t(it.name),
        d: common_vendor.t(it.brand || "-"),
        e: common_vendor.t(it.model || ""),
        f: common_vendor.t(it.spec || ""),
        g: common_vendor.t(it.stock ?? 0),
        h: common_vendor.t((it.retailPrice ?? it.price ?? 0).toFixed(2)),
        i: it.id,
        j: common_vendor.o(($event) => $options.openForm(it.id), it.id)
      });
    })
  } : {}, {
    o: common_vendor.o((...args) => $options.loadMore && $options.loadMore(...args)),
    p: common_vendor.o(($event) => $options.openForm())
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/product/list.js.map
