const util = require("../../utils/util.js");
const config = require("../../utils/config.js");
const app = getApp();
var updateTimer;
Page({
  data: {
      swiperIndex:0,
      swiperList:[],
      equity:{},
      swiper:{
        next: 0,
        previous: 0
      },
      questionShow:false
  },
  onLoad: function () {
    clearTimeout(updateTimer);
    this.getList();
  },
  getList(){
    let that = this;
    util.ajax({
      func: "v2/user/growthCard/list",
    }, function (res) {
      if (res.code == 0) {
        if(res.data.list.length > 1){
          that.data.swiper.next = 150;
        }
        that.setData({ swiperList: res.data.list, swiper: that.data.swiper });
        that.getEquity();
      }else{
        util.showTips(res.reason)
      }
    })
  },
  getEquity() {
    let that = this;
    util.ajax({
      func: "v2/user/growthCard/equity",
      data:{id:that.data.swiperList[that.data.swiperIndex].id}
    }, function (res) {
      if (res.code == 0) {
        that.setData({equity:res.data});
        if(!util.isObjEmpty(res.data.equityThree)){
          that.updateTime();
        } 
      } else {
        util.showTips(res.reason)
      }
    })
  },
  updateTime() {
    let that = this, equity = this.data.equity, i = 0;
    equity.equityThree.forEach((item, index) => {
      let lag = item.saleTime/ 1000;
      if (lag > 0) {
        equity.equityThree[index].updateTime = util.isTimeLeng(Math.floor((lag / 3600) % 60)) + ":" + util.isTimeLeng(Math.floor((lag / 60) % 60)) + ":" + util.isTimeLeng(Math.floor(lag % 60));
        equity.equityThree[index].saleTime = item.saleTime-1000;
        i = 1;
      } else {
        equity.equityThree[index].updateTime = null;
      }
    });
    that.setData({ equity: equity });
    if (i == 0){
      clearTimeout(updateTimer);
    }else{
      updateTimer = setTimeout(function () {
        that.updateTime();
      }, 1000);
    }
  },
  swiperChange(e){
    let index = e.detail.current, count = this.data.swiperList.length - 1;
    if (index == 0) {
      this.data.swiper.next = 150;
      this.data.swiper.previous = 0;
    }
    if (index > 0 && index < count){
      this.data.swiper.next = 75;
      this.data.swiper.previous = 75;
    }
    if (index == count) {
      this.data.swiper.next = 0;
      this.data.swiper.previous = 150;
    }
    this.setData({ swiperIndex: index, swiper: this.data.swiper });
    clearTimeout(updateTimer);
    this.getEquity();
  },
  questionClick(){
    wx.showModal({
      title:"赠送说明",
      content: "券全部未使用时，可赠送亲友；券部分已使用，则不允许赠送；赠送后，在个人中心不再显示。",
      showCancel:false,
      confirmText:"知道了"
    })
  },
  vouche(e){
    if (e.currentTarget.dataset.status == 1){
      wx.navigateTo({
        url: '/pages/growthCard/activity?vid=' + e.currentTarget.dataset.vid
      });
    }
  },
  binderror(e){
    var index = e.currentTarget.dataset.index;
    this.data.equity.equityThree[index].logo = '/images/noimg.png';
    this.setData({ equity: this.data.equity });
  },
  onShareAppMessage: function (options) {
    let that = this, data = that.data.swiperList[that.data.swiperIndex];
    if(options.from=="button"){
      return {
        title: '送你一张冬夏令营成长卡，让宝贝去探索、去挑战、去成长！',
        path: '/pages/growthCard/give?id='+data.id,
        imageUrl: util.config.imgUrl + '/img/group_card_share.jpg'
      }
    }
  }
})