// packageH/signPage/staffManagement/staffManagement.js
const app = getApp();
Page({
  data: {
    showBox: false,
    recordsList: [],
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },
  onLoad: function (options) {
    // this.getcontentHeight();
  },

  onShow: function () {
    this.getData();
  },


  getData() {
    this.setData({
      page: 1,
    });
    let that = this;
    let url = app.getNetAddresss("plugin.yun-sign.frontend.h5.worker.get-list");
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    app._getNetWork({
      url,
      data: {
        page: 1
      },
      success (resdata) {
        console.log(resdata);
        wx.hideLoading();
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            dataInfo: res.data,
            recordsList: res.data.data,
            isLoadMore: true,
            total_page: res.data.last_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
        } else {
          wx.showModal({
            content: res.msg,
            showCancel: false,
            success(res) {
              wx.navigateBack({
                delta: 1,
              });
            },
          });
        }
      },
      fail (err) {
        wx.hideLoading();
        console.log(err, "------失败-------");
      }
    });
  },

  //获取更多数据
  getMoreData () {
    let that = this;
    if (!this.data.isLoadMore) {
      app.tips("没有更多了");
      return;
    }   
    if (this.data.page >= this.data.total_page) {
      this.setData({
        isLoadMore: false
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.worker.get-list");
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      app._getNetWork({
        url: queryUrl,
        data: {
          page: that.data.page
        },
        success (resdata) {
          wx.hideLoading();
          if (res.result === 1) {
            console.log(resdata);
            let res = resdata.data;
            var myData = res.data.data;
            let concatArr = that.data.recordsList.concat(myData);//数组拼接
            that.setData({
              isLoadMore: true,
              recordsList: concatArr
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail (err) {
          console.log(err, "-------失败 -----------");
          that.setData({
            isLoadMore: true
          });
          wx.hideLoading();
        }
      });
    }
  },

  delRow (event) {
    let {roleId} = event.target.dataset;
    let url = app.getNetAddresss("plugin.yun-sign.frontend.h5.worker.del-worker");
    app._getNetWork({
      url,
      data: {
        id: roleId
      },
      success (resdata) {
        if (resdata.data.result === 1) {
          // let res = resdata.data;
          that.setData({
            recordsList: []
          });
          that.getData();
          app.tips("删除成功!");
        } else {
          app.tips("删除失败!");
        }
      },
      fail (err) {
        console.log(err);
      } 
    });
  },


  // 获取内容高度
  getcontentHeight() {
    let that = this;
    const query = wx.createSelectorQuery();
    query.select('#btn').boundingClientRect();
    let windowHeight = 600;
    try {
      const res = wx.getSystemInfoSync();
      windowHeight = res.windowHeight;
    } catch (e) {
      console.log(e);
    }
    query.exec(function (res) {
      that.setData({
        contentHeight: windowHeight - res[0].height + 'px'
      });
    });
  },

  toAdd () {
    wx.navigateTo({
      url: '/packageH/signPage/addStaff/addStaff',
    });
  },

  handleShowBox () {
    this.setData({
      showBox: !this.data.showBox
    });
  },
  onHide: function () {

  },

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