/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-03-31 17:07:04
 * @LastEditors: HJ
 * @LastEditTime: 2021-04-20 17:48:52
 */
const { fhGetAllCoupon } = require("../../../api/coupon/index");
const { computedActiveText } = require("../../_utils/actives");
const { bus } = getApp();

const _PLATFORM_COUPOU_TYPE = 2;

const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true,
  },
  properties: {
    actives: {
      type: Object,
      value: {},
      observer(v) {
        v && this.handlerActives(v);
      },
    },
  },
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
    // 是否展示活动弹窗
    show: false,
    // 所有活动
    allActivesInfo: {},
    // 平台券
    platformCouponText: "",
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
    init() {
      bus.on("onCouponsData", this.handlerCoupons.bind(this));
      bus.on("onShowActivePopup", this.handlerShow.bind(this));
      this.getAllCoupon();
    },
    // 关闭弹窗
    onClose() {
      this.setData({
        show: false,
      });
    },
    // 处理活动信息
    handlerActives(active = {}) {
      let { allActivesInfo } = this.data;

      allActivesInfo = {
        ...allActivesInfo,
        ...computedActiveText(active),
      };

      this.setData({
        allActivesInfo,
      });
    },
    // 处理优惠券信息
    handlerCoupons({ coupons }) {
      if (coupons && coupons.length > 0) {
        this.setData({
          "allActivesInfo.领券": coupons,
        });
      }
    },
    // 显示弹窗
    handlerShow({ show }) {
      this.setData({
        show,
      });
    },
    // 获取所有优惠券
    async getAllCoupon() {
      try {
        const memberId = wx.getStorageSync("memberId");
        if (!memberId) return;
        const storeId = wx.getStorageSync("storeid");
        const merchantId = wx.getStorageSync("merchantid");
        const { results } = await fhGetAllCoupon(memberId, {
          storeId,
          merchantId,
          orderFee: 99999,
          foodTemplateSkuIdList: [1],
        });
        this.handlerPlatformCoupon(results);
      } catch (e) {
  console.error("debug axios", e);
      }
    },
    // 处理获取到的优惠券，并提取出平台优惠券
    handlerPlatformCoupon(data) {
      let { canUse } = data;
      let text = [];
      if (canUse) {
        canUse.forEach((i) => {
          const { couponType, threshold, value } = i;
          if (couponType === _PLATFORM_COUPOU_TYPE) {
            text.push(threshold ? `满${threshold}减${value}` : `${value}元平台券`)
          }
        });
      }
      this.setData({
        platformCouponText: text.join("；"),
      });
    },
  },
  // 组件生命周期
  lifetimes: {
    attached() {
      this.init();
    },
  },
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data;

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

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

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

Component(componentOptions);
