/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-03-23 15:56:46
 * @LastEditors: WorkerChen z874512298@163.com
 * @LastEditTime: 2022-05-10 18:18:00
 */
const {
  controlNumber,
  constructorDishUuid
} = require("../../_utils/dish");
const {
  bus
} = getApp();
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true,
  },
  properties: {
    value: {
      type: Object,
      value: {},
    },
    direction: {
      type: String,
      value: "horizontal",
      observer(v) {
        this.setData({
          isHorizontal: v === "horizontal",
        });
      },
    },
    extraInfo: {
      type: Object,
      value: {},
    },
  },
  // 数据监听
  observers: {
    "value, extraInfo"(value, extraInfo) {
      if (value && extraInfo.storeOperatingStatus) {
        this.initData(value);
      }
    },
  },
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
    // 菜品的UUID
    dishKey: "",
    // 菜品数据
    dishData: {},
    // 购买信息
    buyInfo: {
      number: 0,
    },
    // 是否不允许点击
    disabled: false,
    // 是否横向展示
    isHorizontal: true,
    // 图片是否展示
    imgShow: false,
  },
  // 组件方法
  methods: {
    init() {
      bus.on("shoppingCartChannel", this.handleShoppingCartData.bind(this));
      // 清空购物车
      bus.on("onClearCart", this.handlerClearCart.bind(this));
    },
    // 计算菜品数据
    initData(v) {
      const {
        categoryId,
        foodType,
        discountPrice,
        minimumPrice,
        minimumRecPrice,
        attribute,
        storeFoodSkuDtoList,
        oversell,
      } = v;
      // 计算所有sku的总库存
      const allStock = storeFoodSkuDtoList
        .map((i) => i.stock || 0)
        .reduce((acc, cv) => acc + cv);
      const sellerOut = allStock < 1;
      const dishKey = constructorDishUuid(storeFoodSkuDtoList[0], attribute);

      // 计算展示价
      const currPrice = discountPrice ?
        discountPrice :
        foodType === 3 ?
        minimumRecPrice :
        minimumPrice;

      // 这个是用于展示被划掉的价格，通常来说是原价
      const crossOutPrice = foodType === 1 ? "" : minimumPrice;

      const dishData = Object.assign({}, v, {
        currPrice,
        crossOutPrice,
        allStock,
        sellerOut,
      });

      const {
        storeOperatingStatus
      } = this.data.extraInfo || {};

      this.setData({
        dishData,
        dishKey,
        disabled: storeOperatingStatus === 2 || sellerOut || oversell,
      });
    },
    // 点击菜品获取详情
    clickItem() {
      console.log()
      let dishData = this.data.dishData;
        dishData.number = this.data.buyInfo.number
      bus.emit("showDishDetail", {
        show: true,
        detail: dishData
      });
    },
    // 处理购物车信道的信息
    handleShoppingCartData(info) {
      const {
        key,
        data: {
          number
        },
      } = info;
      const {
        dishKey
      } = this.data;
      if (key === dishKey) {
        this.setData({
          "buyInfo.number": number,
        });
      }
    },
    // 数量加/减
    controlNumber({
      currentTarget: {
        dataset
      }
    }) {
      const {
        extraInfo,
        disabled
      } = this.data;
      if (extraInfo.storeOperatingStatus === 2 || disabled) return;

      const {
        isAdd = true
      } = dataset;
      let {
        dishData,
        dishKey,
        buyInfo: {
          number
        },
      } = this.data;

      const {
        moreSku,
        storeFoodSkuDtoList,
        miniImage,
        introduction
      } =
      dishData;

      if (!moreSku){
        dishData = storeFoodSkuDtoList[0];
        dishData.number = number
      }

      controlNumber({
        detail: Object.assign({}, dishData, {
          number,
          miniImage,
          introduction,
        }),
        key: dishKey,
      }, {
        isAdd,
      });
    },
    // 清空购物车
    handlerClearCart() {
      const {
        number
      } = this.data.buyInfo;
      if (number > 0)
        this.setData({
          "buyInfo.number": 0,
        });
    },
  },
  // 组件生命周期
  lifetimes: {
    attached() {
      this.init();
    },
  },
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const {
        isPageHidden
      } = this.data;

      // show事件发生前，页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true,
      });

      // 清除定时器等操作
    },
  },
};

Component(componentOptions);