/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2020-09-21 14:52:12
 * @LastEditors: HJ
 * @LastEditTime: 2021-04-20 14:01:49
 */
const { watch } = getApp();
Component({
  externalClasses: ["nearby-list-body"],
  properties: {
    value: {
      type: Object,
      value: {},
    },
    canJump: {
      type: Boolean,
      value: true,
    },
  },
  data: {
    rawValue: {},
    discountExpand: false,
    isShowArr: false,
    // 所有平台优惠券匹配信息
    couponSummary: {},
    // 平台优惠券的标签展示文案
    platformCouponText: "",
  },
  observers: {
    "rawValue.summary": function (v) {
      if (v) {
        const str = v.reduce((acc, curr) => {
          acc += "" + curr;
          return acc;
        });
        this.setData({
          isShowArr: str.length > 24,
        });
      }
    },
    value: function (v) {
      if (v) {
        let { value } = this.data;
        let { summary, distance } = value;
        typeof summary === "string" && (summary = summary.split(","));
        distance = parseFloat(distance) || 0;
        distance =
          distance < 1
            ? parseInt(distance * 1000) + "m"
            : distance.toFixed(2) + "km";
        Object.assign(value, { summary, distance });
        this.setData({
          rawValue: value,
        });
      }
    },
    "rawValue.sellerId, couponSummary ": function (merchantId, couponSummary = {}) {
      couponSummary = couponSummary || {}
      const text = couponSummary[merchantId] || couponSummary["ALL"];
      if (text) {
        this.setData({
          platformCouponText: text,
        });
      }
    },
  },
  methods: {
    jumpSaleroom: function ({ currentTarget: { dataset: { storetype, storeid, merchantid }}}) {
      if (!this.data.canJump) return;
      wx.setStorageSync("storeid", storeid);
      wx.setStorageSync("merchantid", merchantid);
      wx.setStorageSync("noid", "disagree");
      const url = storetype ? '/store/pages/week/index' : '/store/pages/lunch/index';
      wx.navigateTo({
        url
      });
    },
    discountExpandFn() {
      this.setData({
        discountExpand: !this.data.discountExpand,
      });
    },
    handlerCouponInfo(value) {
      const { couponSummary } = value;
      this.setData({
        couponSummary,
      });
    },
  },
  // 组件生命周期
  lifetimes: {
    ready() {
      watch("platformCouponInfo", this.handlerCouponInfo.bind(this));
    },
  },
});
