var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');

Page({
  data: { 
    list: [], 
    cash:0,//可提现金额
    page: 1,
    limit: 10,
    totalPages: 1
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数 
    this.setData({
      cash: options.m
    });
    this.getList();
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  }, 
  getList: function() {
    wx.showLoading({
      title: '加载中',
    });

    setTimeout(function() {
      wx.hideLoading()
    }, 2000);

    let that = this;
    util.request(api.DisCashoutList, {
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({ 
          list: that.data.list.concat(res.data.list),
          totalPages: res.data.pages
        });
      } 
      wx.hideLoading();
    });
  }, 
  btnCashout:function(){//提现申请 
    let that = this;  
    wx.showModal({
      title: '',
      content: '确定要申请提现？ 提现金额：'+that.data.cash,
      success: function(res) {
        if (res.confirm) {
          util.request(api.DisCashoutApply, {
            amount: that.data.cash
          }, 'GET').then(function(res) {
            if (res.errno === 0) {
              wx.showToast({
                title: '提现申请成功，请等待管理员处理', 
              });
              setTimeout(function() {
                that.setData({
                  page:1,
                  limit:0,
                  list:[],
                  cash:0
                }) 
                that.getList();
              }, 2000);
              
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  onReachBottom:function(){
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getList();
    } else {
      wx.showToast({
        title: '没有更多提现申请了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  }
})