// packageE/others/referral_officer/referral_officer.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    info: {},
    list: [],
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    language: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },
  getData(){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.recommender.api.index.index");
    urlStr += "&state=" + this.data.active;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            info:res.data,
            list:res.data.list.data,
            isLoadMore: true,
            page: 1,
            total_page: res.data.list.last_page,
          });
          wx.setNavigationBarTitle({
            title: res.data.custom_name
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onChange(e){
    this.data.active = e.detail.index;
    this.getData();
  },
  gotoDetail(e){
    let _id = e.target.dataset.id||e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageE/others/referral_officerDetail/referral_officerDetail?id=' + _id });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  //获取更多数据
  _getMoreData() {
    const that = this;
    let json = {};
    let api = '';
    let urlStr = '';
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      api = 'plugin.recommender.api.index.index';
      json = {
        page: that.data.page,
        state: that.data.active
      };

      urlStr = app.getNetAddresss(api);
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              list: that.data.list.concat(res.data.list.data)
            });

          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});