
import recordSource from '../../utils/recordSource';
import bonusUp from '../../utils/bonusUp';
const getReq = require('../../config.js').getReq;
const getTypeSn = require('../../utils/util.js').getTypeSn;

let app = getApp();
Page({
  data: {
    tabs: [],
    prop_special: [],
    surplus_special: [],
    special_datas: {},
    summery: [],
    isScroll: false,
    isSendProp: false,
    firstLoad: true,
    fromSource: '',
    tipsFlag: false,
    tipsDatas: null,
    daliy_bonus: ''
  },

  onShow: function () 
  {
    this.closeTips()
    if (!this.data.firstLoad) {
      app.setFromSource(this.data.fromSource)
    }

    // if (app.globalData.userInfo) {
    //   setTimeout(() => {
    //     bonusUp((tipsFlag, tipsDatas) => {
    //       this.setData({
    //         tipsFlag,
    //         tipsDatas
    //       })
    //     })
    //   }, 0);
    // }
  },

  onLoad: function (options) {
    console.log('onLoad options:',options)
    if (options && options.relay_id) {
      app.globalData.relay_id = options.relay_id
      console.log('options.relay_id:', options.relay_id)
    }
    this.getDatas()
  },
  freeBonus() {
    if (this.data.daliy_bonus) {
      let type_sn = getTypeSn(this.data.daliy_bonus)
      if (type_sn) {
        app.navigateto(`/pages/shareBonus/shareBonus?type_sn=${type_sn}`)
        return
      }
    }
  },
  getDatas(special_id) {
    let fromSource = recordSource(app, 'act=index&op=index&special_id=0&page=10&curpage=1&client_type=mini')
    wx.showLoading({
      title: '加载中'
    });
    let self = this;
    console.log('fromSource =' + fromSource)
    getReq(app,{
      act: 'index',
      op: 'mini_index',
      page: 10,
      curpage: 1,
      from: fromSource
    }, function (res) {
      wx.stopPullDownRefresh()
      if (res.code == 200) {
        let prop_special = res.datas.special_list;
        let sent_down_special = prop_special.splice(0, 8);
        self.setData({
          surplus_special: prop_special,
          prop_special: sent_down_special,
          special_datas: res.datas,
          summery: res.datas.summary,
          firstLoad: false,
          fromSource: app.globalData.fromSource
        });
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    })
  },
  req_tabs() {
    let self = this
    getReq(app,{
      act: 'index',
      op: 'tabs'
    }, function (res) {
      if (res.code == 200) {
        self.getDatas(res.datas.tabs[0].special_id);
        self.setData({
          tabs: res.datas.tabs,
          daliy_bonus: res.datas.daliy_bonus
        });
      }
    })
  },
  onMyEvent: function (e) {
    this.setData({
      prop_special: [],
      surplus_special: [],
      special_datas: {},
      summery: []
    });
    this.getDatas(e.detail.item.special_id)
  },
  closeTips() {
    this.setData({
      tipsFlag: false,
      tipsDatas: null
    })
  },
  onPageScroll(e) {
    clearTimeout(this.showTop);
    this.showTop = setTimeout(() => {
      let scrollTop = e.scrollTop;
      if (scrollTop >= 300) {
        this.setData({
          isScroll: true
        })
      }
      else {
        this.setData({
          isScroll: false
        })
      }
    }, 100);
  },
  backTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  onShareAppMessage: function () {
    return app.cardShareInfo();
  },
  onReachBottom: function () {
    if (this.data.isSendProp) {
      return;
    }
    
    let surplus_special_num = this.data.surplus_special.length;
    if (surplus_special_num > 0) {
      wx.showLoading({
        title: '加载中'
      });
      this.setData({
        isSendProp: true
      });
      this.data.isSendProp = true;
      let prop_special = this.data.surplus_special;
      let sent_down_special = prop_special.splice(0, 8);
      this.setData({
        surplus_special: prop_special,
        prop_special: this.data.prop_special.concat(sent_down_special)
      })
      this.setData({
        isSendProp: false
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 1500)
    }
    else {
      return;
    }
  },
  onPullDownRefresh(){
    this.getDatas()
  }
})
