const util = require("../../utils/util.js");
Component({
  properties: {
    aid: {
      type: String,
      observer: function (aid) {
        this.setData({ aid });
      }
    },
    source: {
      type: Number,
      observer: function (source) {
        this.setData({ source });
      }
    },
    isPrizetable: {
      type: Boolean,
      observer: function (newVal) {
        this.setData({ isPrizetable: newVal });
      }
    },
    isShareGroup: {
      type: Boolean,
      observer: function (newVal) {
        this.setData({ isShareGroup: newVal });
      }
    },
    listData: {
      type: Object,
      observer: function (newVal) {
        this.setData({ listData: newVal });
      }
    },
  },
  data: {
    cardOverUsers: [],
    source: 0,
    downTimer: null,
    inTimer: null,
    index: 0,
    show: false,
    isShow: false,
    isPrizetable: false, //是否在大转盘页面
    isShareGroup: false, //是否为分享家或者机构轮播
    listData: {},//大转盘或分享家页面传递数据
  },
  pageLifetimes: {
    show() {
      this.getSold();
    },
    hide() {
      clearTimeout(this.data.downTimer);
      clearTimeout(this.data.inTimer);
    }
  },
  methods: {
    downTime() {
      let that = this;
      that.data.downTimer = setTimeout(() => {
        // 首页使用：order/soldRecord返回数据大于index；分享家页面或大转盘页面展示：对应状态值为true且传过来的的数据大于1
        if (that.data.index < that.data.cardOverUsers.length - 1 || ((that.data.isPrizetable || this.data.isShareGroup) && that.data.index < that.data.listData.length - 1)) {
          that.data.index++;
        } else {
          // index等于数据数组长度-1时重置index
          that.data.index = 0;
        }
        that.setData({ show: true, index: that.data.index });
        that.data.inTimer = setTimeout(() => {
          that.setData({ show: false });
          that.downTime();
        }, 3000);
      }, 2000);
    },
    img(e) {
      let index = e.currentTarget.dataset.index;
      this.data.cardOverUsers[index].avatar = '/images/default_logo.jpg';
      this.setData({ cardOverUsers: this.data.cardOverUsers });
    },
    goActivity(e) {
      if (this.data.source == 1) return false;
      wx.navigateTo({
        url: '/pages/product/activity/index?aid=' + e.currentTarget.dataset.aid,
      });
    },
    getSold() {
      let that = this;
      // 大转盘或者分享家
      if (that.data.isPrizetable || this.data.isShareGroup) {
        that.setData({ show: false, isShow: false, index: 0, downTimer: null, inTimer: null });
        that.downTime();
        setTimeout(function () {
          that.setData({ isShow: true });
        }, 3000);
      } else {
        util.ajax({
          func: "order/soldRecord",
          data: { "source": that.data.source, "aid": that.data.aid },
          load: false
        }, function (res) {
          if (res.code == 0) {
            // 有数据
            if (res.data && res.data.length > 0) {
              that.setData({ show: false, isShow: false, index: 0, downTimer: null, inTimer: null, cardOverUsers: res.data });
              that.downTime();
              setTimeout(function () {
                that.setData({ isShow: true });
              }, 3000);
            }
          }
        })
      }
    }
  }
})
