"use strict";
const common_vendor = require("../../common/vendor.js");
const API_api = require("../../API/api.js");
if (!Array) {
  const _easycom_uni_icons2 = common_vendor.resolveComponent("uni-icons");
  _easycom_uni_icons2();
}
const _easycom_uni_icons = () => "../../uni_modules/uni-icons/components/uni-icons/uni-icons.js";
if (!Math) {
  _easycom_uni_icons();
}
const _sfc_main = {
  __name: "sort",
  props: {
    merchantId: {
      type: String,
      required: true
    }
  },
  emits: ["update-cart"],
  setup(__props, { emit }) {
    const props = __props;
    const resultArray = common_vendor.ref([]);
    common_vendor.ref([
      {
        orderState: 0,
        url: "/pages/sort/sort",
        title: "点餐"
      },
      {
        orderState: 1,
        url: "/pages/evaluate/evaluate",
        title: "评价"
      },
      {
        orderState: 2,
        url: "/pages/details/details",
        title: "商家"
      }
    ]);
    const currentIndex = common_vendor.ref(0);
    common_vendor.ref([]);
    const businessId = common_vendor.ref("");
    common_vendor.ref(props.merchantId);
    common_vendor.watch(() => props.merchantId, async (newVal) => {
      console.log("传过来的商家id", newVal);
      businessId.value = newVal;
      await leftGroup();
      await rightList();
    });
    common_vendor.onMounted(() => {
    });
    const leftGroup = async () => {
      try {
        console.log(businessId.value);
        const res = await common_vendor.index.request({
          url: API_api.apiImageUrl + "/api/dishesGroup/list/dishesGroup",
          method: "POST",
          data: {
            businessId: businessId.value
          }
        });
        if (res.data.code === 0) {
          console.log(res.data.data);
          const length = res.data.data.length;
          for (let i = 0; i < length; i++) {
            const index = i + 1;
            const groupName = res.data.data[i].groupName;
            resultArray.value.push(groupName);
          }
          console.log(resultArray.value);
        }
      } catch (error) {
        console.log("error", error);
      }
    };
    const rightList = async () => {
      try {
        const res = await common_vendor.index.request({
          url: API_api.apiImageUrl + "/api/dishes/list/dishes",
          method: "POST",
          data: {
            businessId: businessId.value
          }
        });
        if (res.data.code === 0) {
          const filteredData = res.data.data.filter((item) => item.status !== "1");
          historyList.value = filteredData;
          console.log("更新后的菜品列表:", historyList.value);
        } else {
          console.error("请求失败:", res.data.message);
        }
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    };
    common_vendor.ref(null);
    const switchTab = async (index) => {
      currentIndex.value = index;
      const updatedDishesQueryRequest = {
        businessId: businessId.value,
        dishesGroupId: index + 1
      };
      try {
        const res = await common_vendor.index.request({
          url: API_api.apiImageUrl + "/api/dishes/list/dishesVO",
          method: "POST",
          data: JSON.stringify(updatedDishesQueryRequest)
        });
        if (res.data.code === 0) {
          const filteredData = res.data.data.filter((item) => item.status !== "1");
          historyList.value = filteredData;
        } else {
          console.error("请求失败:", res.data.message);
        }
      } catch (error) {
        console.error("Error fetching data:", error);
      }
      common_vendor.index.scrollIntoView({
        id: "contentArea",
        duration: 300
      });
    };
    const historyList = common_vendor.ref();
    const cartItems = common_vendor.ref([]);
    const addToCart = (item) => {
      cartItems.value.push({
        ...item,
        quantity: 1
      });
      common_vendor.index.setStorageSync("cartItems", cartItems.value);
      cartItems.value.forEach((item2) => {
        console.log(item2.dishesName);
      });
      emit("update-cart", cartItems.value);
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.f(resultArray.value, (tab, index, i0) => {
          return {
            a: common_vendor.t(tab),
            b: index,
            c: currentIndex.value === index ? 1 : "",
            d: common_vendor.o(($event) => switchTab(index))
          };
        }),
        b: currentIndex.value === 0
      }, currentIndex.value === 0 ? {} : {}, {
        c: common_vendor.f(historyList.value, (item, index, i0) => {
          return {
            a: item.dishesImage,
            b: common_vendor.t(item.dishesName),
            c: common_vendor.t(item.dishesPrice),
            d: "257302e4-0-" + i0,
            e: common_vendor.o(() => addToCart(item)),
            f: index
          };
        }),
        d: common_vendor.p({
          type: "plus-filled",
          size: "30",
          color: "#4095e5"
        })
      });
    };
  }
};
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/111111000000/uniapp04/pages/merchant/sort.vue"]]);
my.createComponent(Component);
