// packageH/signPage/contractList/contractList.js
const app = getApp();
Page({
  data: {
    active: 0,
    showBox: false,
    listHeight: 0,
    recordsList: [],
    isLoadMore: true,
    dataInfo: {},
    page: 1,
    total_page: 0,
    record: {},
  },
  onLoad: function (options) {
    // this.getlistHeight();
  },

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

  toSign(event) {
    let record = event.currentTarget.dataset.record;
    this.setData({
      record
    });
    wx.navigateTo({
      url: `/packageH/signPage/signPage?id=${record.id}`,
    });
  },

  // 停止签署
  stopUse(event) {
    let record = event.currentTarget.dataset.record;
    this.setData({
      record
    });
    wx.showModal({
      content: "确定取消吗",
      success () {
        this.cancelQuery();
      },
    });
  },

  cancelQuery () {
    let that = this;
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.contract.cancel");
    wx.showLoading({
      title: '正在取消',
      mask: true
    });
    app._getNetWork({
      url: queryUrl,
      data: {
        id: that.data.record
      },
      success (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            recordsList: [],
          });
          that.getData();
          wx.hideLoading();
          app.tips("取消成功");
        } else {
          app.tips("取消失败");
        }
      },
      fail (err) {
        wx.hideLoading();
        console.log(err, '------ 失败 -------');
      }
    });
  },

  downpdf (event) {
    console.log("下载PDF");
    let fileUrl = event.currentTarget.dataset.record.download_url;
    wx.downloadFile({
      url: fileUrl,
      success (res) {
        console.log(res, '成功');
        var filePath = res.tempFilePath;
        wx.openDocument({
          filePath,
          success () {
            app.tips("文件打开成功");
          },
          fail () {
            app.tips("打开文件失败");
          }
        });
      },
      fail (err) {
        console.log(err, "是失败");
        wx.showModal({
          content: "下载失败，请重新下载"
        });
      }
    });
  },

  // 切换tab
  tabChange (event) {
    this.setData({
      active: event.detail.name * 1
    });
    this.getData();
  },

  getData() {
    this.setData({
      page: 1,
    });
    let that = this;
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.contract.get-list");
    wx.showLoading({
      title: '正在取消',
      mask: true
    });
    app._getNetWork({
      url: queryUrl,
      data: {
        page: 1,
        status: that.data.active
      },
      success (resdata) {
        console.log(resdata);
        wx.hideLoading();
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            isLoadMore: true,
            recordsList: res.data.data,
            dataInfo: res.data,
            total_page: res.data.last_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
        } else {
          app.tips(res.msg);
        }
      },
      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.contract.get-list");
      wx.showLoading({
        title: '正在取消',
        mask: true
      });
      app._getNetWork({
        url: queryUrl,
        data: {
          page: that.data.page,
          status: that.data.active
        },
        success (resdata) {
          wx.hideLoading();
          let res = resdata.data;
          if (res.result === 1) {
            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();
        }
      });
    }
  },

  getlistHeight() {
    let that = this;
    const query = wx.createSelectorQuery();
    query.select('#tabs').boundingClientRect();
    let windowHeight = 600;
    try {
      const res = wx.getSystemInfoSync();
      windowHeight = res.windowHeight;
    } catch (e) {
      console.log(e);
    }
    query.exec(function (res) {
      that.setData({
        listHeight: windowHeight - res[0].height + 'px'
      });
    });
  },


  handlerImg () {
    this.setData({
      showBox: !this.data.showBox
    });
  },

  onHide: function () {

  },

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