// pages/member/presentationRecord/presentationRecord.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    orderType: '',
    member_id: '',
    api: '',
    selected: 0,
    // 代打款
    waitPay: [],
    // 打款
    pay: [],
    // 待审核
    review: [],
    // 无效数据
    invalid: [],
    // 全部数据
    all: [],
    // loadMore
    loading: false,
    allLoaded: false,
    goload: false,
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.supplier) {
      this.setData({
        orderType: options.supplier
      });
      if (options.member_id) {
        this.setData({
          member_id: options.member_id
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._initData();
    if (this.data.orderType == 'supplier') {
      this._getSupplierNetData();
    } else {
      this._getNetData(this.data.selected);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.orderType != 'supplier') {
      if (this.data.isLoadMore) {
        this.getMoreData(this.data.selected);
      } else {
        console.log('没有更多数据');
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  _initData() {
    this.setData({
      page: 1,
      goload: true,
      total_page: 0,
      loading: true,
      allLoaded: false,
      isLoadMore: true
    });
  },
  // 供应商
  _getSupplierNetData() {
    if (this.data.selected == 0) {
      // 全部
      this.setData({
        api: 'plugin.supplier.frontend.withdraw-log.index'
      });
    } else if (this.data.selected == 1) {
      // 待审核
      this.setData({
        api: 'plugin.supplier.frontend.withdraw-log.applying'
      });
    } else if (this.data.selected == 2) {
      // 待打款
      this.setData({
        api: 'plugin.supplier.frontend.withdraw-log.pending'
      });
    } else if (this.data.selected == 3) {
      // 打款
      this.setData({
        api: 'plugin.supplier.frontend.withdraw-log.already'
      });
    } else if (this.data.selected == 4) {
      // 无效
      this.setData({
        api: 'plugin.supplier.frontend.withdraw-log.reject'
      });
    }
    let that = this;
    let urlStr = app.getNetAddresss(that.data.api);
    urlStr += '&member_id=' + that.data.member_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          var mydata = res.data;
          that.setData({
            total_page: mydata.total,
            allLoaded: false
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          if (that.data.selected == 0) {
            // 全部
            that.setData({
              all: mydata.data
            });
          } else if (that.data.selected == 1) {
            // 待审核
            that.setData({
              review: mydata.data
            });
          } else if (that.data.selected == 2) {
            // 待打款
            that.setData({
              waitPay: mydata.data
            });
          } else if (that.data.selected == 3) {
            // 打款
            that.setData({
              pay: mydata.data
            });
          } else if (that.data.selected == 4) {
            // 无效
            that.setData({
              invalid: mydata.data
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 发起http请求
  _getNetData(type) {
    let status = '';
    if (type == 0) {
      status = '';
    } else if (type == 1) {
      status = '0';
    } else if (type == 2) {
      status = '1';
    } else if (type == 3) {
      status = '2';
    } else if (type == 4) {
      status = '-1';
    }
    var that = this;
    that.setData({
      isLoadMore: false,
      page: 1
    });
    let urlStr = app.getNetAddresss('finance.withdraw.withdraw-log');
    urlStr += '&status=' + status;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            allLoaded: false,
            isLoadMore: true,
            total_page: res.data.last_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          var mydata = res.data.data;
          if (that.data.selected == 0) {
            // 全部
            that.setData({
              all: mydata
            });
          } else if (that.data.selected == 1) {
            // 待审核
            that.setData({
              review: mydata
            });
          } else if (that.data.selected == 2) {
            // 待打款
            that.setData({
              waitPay: mydata
            });
          } else if (that.data.selected == 3) {
            // 打款
            that.setData({
              pay: mydata
            });
          } else if (that.data.selected == 4) {
            // 无效
            that.setData({
              invalid: mydata
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  swichTabTItem(e) {
    let index = e.detail.index;
    this.setData({
      selected: index
    });
    if (this.data.orderType === 'supplier') {
      this._getSupplierNetData();
    } else {
      this._getNetData(this.data.selected);
    }
  },
  gopresentationDetails(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/member/presentationDetails/presentationDetails?record_id=' + id
    });
  },
  // 获取更多数据
  getMoreData(type) {
    let status = '';
    if (type == 0) {
      status = '';
    } else if (type == 1) {
      status = '0';
    } else if (type == 2) {
      status = '1';
    } else if (type == 3) {
      status = '2';
    } else if (type == 4) {
      status = '-1';
    }
    const that = this;
    that.setData({
      isLoadMore: false // 防止多次请求分页数据
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss('finance.withdraw.withdraw-log');
      app._getNetWork({
        url: urlStr,
        data: {
          status: status,
          page: that.data.page
        },
        success: function (resdata) {
          that.setData({
            isLoadMore: true
          });
          var res = resdata.data;
          if (res.result == 1) {
            var mydata = res.data.data;
            if (that.data.selected == 0) {
              // 全部
              that.setData({
                all: that.data.all.concat(mydata)
              });
            } else if (that.data.selected == 1) {
              // 待审核
              that.setData({
                review: that.data.review.concat(mydata)
              });
            } else if (that.data.selected == 2) {
              // 待打款
              that.setData({
                waitPay: that.data.waitPay.concat(mydata)
              });
            } else if (that.data.selected == 3) {
              // 打款
              that.setData({
                pay: that.data.pay.concat(mydata)
              });
            } else if (that.data.selected == 4) {
              // 无效
              that.setData({
                invalid: that.data.invalid.concat(mydata)
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
});
