// packageE/lottery/lottery.js
// var app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",
    countTime: 0,
    timeData: {},

    activity: {},
    activity_id: '', // 活动id
    goods_id: '', // 活动商品id
    options_id: '', // 绑定商品规格的id
    activity_start: '', //活动开始时间
    activity_end: '', // 活动结束
    currentTime: parseInt(new Date().getTime() / 1000),
    activity_order: [],
    ladderList: [], // 梯度数组
    steps: 0,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      this.getActivity();
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });
  },
  methods: {
    toActivity() {
      wx.redirectTo({
        url: "/packageH/deposit_group/activity_index/activity_index?id=" + this.data.datas.activity.id
      });
    },
    onChange(e) {
      this.setData({
        timeData: e.detail,
      });
    },
    getActivity() {
      if (!this.data.datas.activity || !this.data.datas.activity.id) {
        return;
      }
      let countTime;
      if (this.data.datas.activity.activity_start > parseInt(Date.now() / 1000) && this.data.datas.activity.status == 0) {
        //* 未开始
        countTime = parseInt(this.data.datas.activity.activity_start - parseInt(Date.now() / 1000)) * 1000;
      } else {
        //* 进行中
        if (this.data.datas.activity.activity_end < Number(Date.now() / 1000) || this.data.datas.activity.status > 0) {
          countTime = 0;
        } else {
          countTime = parseInt(this.data.datas.activity.activity_end - parseInt(Date.now() / 1000)) * 1000;
        }
      }

      this.setData({
        countTime: countTime,
        activity_id: this.data.datas.activity.id,
        goods_id: this.data.datas.activity.goods_id,
        goods_option_id: this.data.datas.activity.goods_option_id,
        activity_start: this.data.datas.activity.activity_start * 1000 - new Date(),
        activity_end: this.data.datas.activity.activity_end * 1000 - new Date(),
        activity: this.data.datas.activity,
      });

      this.resetSteps(this.data.datas.activity.ladder);
    },
    // 设置 达到哪一阶段
    resetSteps(arr) {
      let steps = 0;
      arr = arr.reverse();
      let firstLadder = {
        ...arr[0]
      };
      let actData = this.data.activity;
      firstLadder.deal_price = actData.bind_goods.price;
      arr.unshift(firstLadder);
      for (let index = 0; index < arr.length; index++) {
        // 第一梯队是一个区间

        if (index === 0 && ((actData.ladder_type === 1 && actData.sale_count < arr[index].sale_num) || (actData.ladder_type === 0 && actData.order_count < arr[index].deal_num))) {
          steps = 0;
          break;
        }
        if (index > 0 && (actData.ladder_type === 1 && actData.sale_count >= arr[index].sale_num) || (actData.ladder_type === 0 && actData.order_count >= arr[index].deal_num)) {
          steps = index;
        }
      }
      this.setData({
        steps,
        ladderList: arr
      });
    },
  },
});