const util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    coinUnpaidAccount: 0,
    queryData:{
      transitionType: 0,
      pageIndex: 1,
      pageSize: 10
    },
    stat:{},
    list:[],
    total: 0,
    loadMore: true
  },
  onLoad() {
    this.getList(false);
    this.stat();
  },
  setTabbar(e){
    let index = e.currentTarget.dataset.index;
    this.data.queryData.transitionType = index;
    this.data.queryData.pageIndex = 1;
    this.setData({ queryData: this.data.queryData, list: [] });
    this.getList(true);
  },
  stat() {
    let that = this;
    util.ajax({
      func: 'v2/user/coin/stat',
      type: 1,
      load: false
    }, res => {
      if (res.code == 0) {
        that.setData({ coinUnpaidAccount: res.data.coinUnpaidAccount });
      }
    });
  },
  getList(load){
    let that = this;
    util.ajax({
      func:"v2/user/coin/transition/list",
      data:{...that.data.queryData, transitionType: that.data.queryData.transitionType == 0 ? 0 : 2},
      load: load
    },function(res){
      if (res.code == 0) {
        let datas = res.data.list.map(item => {
          return {...item, avatar: item.avatar ? item.avatar : '/images/default_logo.jpg'}
        })
        let loadMore = true, newData = datas || [], oldData = !load ? newData : [...that.data.list, ...newData];
        if (newData.length < that.data.queryData.pageSize && oldData.length > that.data.queryData.pageSize) loadMore = false;
        that.setData({ list: oldData, loadMore, total: res.data.total });
      } else
        util.showTips(res.reason);
    });
  },
  binderror(e) {
    console.log(e)
    var list = this.data.list, index = e.currentTarget.dataset.index;
    list[index].avatar = '/images/default_logo.jpg'
    this.setData({ list });
  },
  onPullDownRefresh() {
    var self = this;
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.data.queryData.pageIndex = 1;
      self.setData({ queryData: self.data.queryData });
      self.getList(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom() {
    var self = this;
    if (self.data.loadMore == false || self.data.total <= self.data.queryData.pageSize) return false;

    setTimeout(function () {
      self.data.queryData.pageIndex = self.data.queryData.pageIndex + 1;
      self.setData({ queryData: self.data.queryData });
      self.getList(true);
    }, 1000);
  },
})