// packageI/handwrittenEsign/handwrittenEsignLog/handwrittenEsignLog.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    activeName: 0,
    dataList: [],
    titleArr: [],
    isLoadMore: true
  },

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

  init() {
    let titleArr = [{
      id: 0,
      name: "全部"
    },
    {
      id: 1,
      name: "未签署"
    },
    {
      id: 2,
      name: "已签署"
    }
    ];
    this.isLoadMore = true;
    this.setData({
      titleArr: titleArr
    });
    this.setNewArr();
  },
  setNewArr() {
    let _dataList = [];
    this.data.titleArr.forEach((val, index) => {
      _dataList.push({
        isLoadMore: true,
        loading: false,
        finished: false,
        page: 1,
        total_page: 0,
        list: []
      });
    });
    this.setData({
      dataList: _dataList
    });
    this.handleClick();
  },
  handleClick(event) {
    if (event) {
      this.setData({
        activeName: event.detail.name
      });
    }
    let urlStr = app.getNetAddresss("plugin.hand-sign-protocol.frontend.controllers.protocol.index");
    urlStr = urlStr + '&page=1&search[title]=' + this.data.keyword + '&search[status]=' + this.data.activeName;
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            ['dataList[' + this.data.activeName + '].list']: res.data.list.data
          });
          this.data.isLoadMore = true;
          this.data.dataList[this.data.activeName].isLoadMore = true;
          this.data.dataList[this.data.activeName].page = 1;
          this.data.dataList[this.data.activeName].total_page = res.data.list.last_page;
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });

  },
  onChangeKey(e) {
    this.setData({
      keyword: e.detail,
    });
  },
  onSearch() {
    this.handleClick();
  },
  toUrl(e) {
    let _id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageI/handwrittenEsign/handwrittenEsignIndex/handwrittenEsignIndex?id=' + _id
    });
  },
  _getMoreData() {
    let that = this;
    this.data.dataList[this.data.activeName].isLoadMore = false;
    if (this.data.dataList[this.data.activeName].page >= this.data.dataList[this.data.activeName].total_page) {
      return;
    } else {
      this.data.dataList[this.data.activeName].page = this.data.dataList[this.data.activeName].page + 1;
      let urlStr = app.getNetAddresss("plugin.hand-sign-protocol.frontend.controllers.protocol.index");
      urlStr = urlStr + '&page=' + this.data.dataList[this.data.activeName].page + '&search[title]=' + this.data.keyword + '&search[status]=' + this.data.activeName;
      app._getNetWork({
        url: urlStr,
        data: {},
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.list.data;
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: true,
              ['dataList[' + that.data.activeName + '].list']: that.data.dataList[that.data.activeName].list.concat(myData)
            });
          } else {
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: false,
              ['dataList[' + that.data.activeName + '].page']: that.data.dataList[that.data.activeName].page - 1
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.dataList[this.data.activeName].isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  }
});