/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-03-29 16:07:48
 * @LastEditors: HJ
 * @LastEditTime: 2021-06-25 10:24:57
 */
const { computedActiveText } = require("../../_utils/actives");
const {
  fhGetStoreCoupon,
  fhGetStoreLike,
  fhControlStoreLike,
} = require("../../../api/store/index");
const { Tips, Loading, hLoading, bus } = getApp();
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true,
  },
  properties: {
    value: {
      type: Object,
      value: {},
    },
    overflowReduction: {
      type: Array,
      value: [],
    },
    // 所有活动
    actives: {
      type: Object,
      value: {},
      observer(v) {
        v && this.handlerActives(v);
      },
    },
  },
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
    // 优惠券
    coupons: [],
    // 是否关注门店
    like: false,
    // 优惠活动
    allActive: {},
    activeShowFlag: false,
  },
  // 组件方法
  methods: {
    async init() {
      const memberId = wx.getStorageSync("memberId");
      await this.getStoreCoupon(memberId);
      if (memberId) {
        this.memberId = memberId;
        await this.fhGetStoreLike();
      }
    },
    // 获取门店优惠券
    async getStoreCoupon(memberId) {
      try {
        const { results } = await fhGetStoreCoupon({ memberId });
        this.setData({
          coupons: results,
        });
        bus.emit("onCouponsData", { coupons: results });
      } catch (e) {
        console.error("debug axios", e);
      }
    },
    // 获取用户是否关注门店
    async fhGetStoreLike() {
      try {
        const { results } = await fhGetStoreLike();
        this.setData({ like: results });
      } catch (e) {
        console.error("debug axios", e);
      }
    },
    // 取消/关注门店
    async controlStoreLike() {
      const memberId = wx.getStorageSync("memberId");
      if (!memberId) {
        wx.navigateTo({
          url: "/pages/authorization/authorization",
        });
        return;
      }

      const { like } = this.data;

      Loading(`${like ? "取消" : ""}关注门店中...`);
      try {
        const storeId = wx.getStorageSync("storeid");

        await fhControlStoreLike(like ? "delete" : "post", {
          storeId,
          memberId,
        });

        Tips(`${like ? "取消" : ""}关注门店成功`);
        this.setData({
          like: !like,
        });
      } catch (e) {
        console.error("debug axios", e);
      }
      hLoading();
    },
    // 活动详情弹窗的显示
    showActivePopup() {
      bus.emit("onShowActivePopup", { show: true });
    },
    // 处理活动信息
    handlerActives(active = {}) {
      const allActive = computedActiveText(active);

      const activeShowFlag = Object.keys(allActive).length > 0;

      this.setData({
        allActive,
        activeShowFlag,
      });
    },
    // 跳转
    nav({ currentTarget: { dataset } }) {
      const { path } = dataset;
      wx.$routerTo(path);
    },
  },
  // 组件生命周期
  lifetimes: {
    attached() {
      this.init();
    },
  },
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data;

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

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

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

Component(componentOptions);
