import { documentList, createQRcode } from "../../api/document";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrcodeDialog: false,
    currentPage: 1,
    hasNext: false,
    fileList: [],
    baseStr: 'data:image/jpg;base64,',
    QRCodeBase64: '',
    qrUserName: ''
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDocumentList()
  },

  // 获取档案列表
  getDocumentList () {
    var data = {
      currentPage: this.data.currentPage
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    documentList(data).then(res => {
      wx.hideLoading()
      var response = res.data.vos
      response.map(item => {
        item.birthDay = item.birthday.split(' ')[0]
      })
      this.setData({
        hasNext: res.data.hasNext,
        fileList: response
      })
    }).catch(e => {
      wx.hideLoading()
      wx.showModal({
        content: e,
        confirmColor: '#333',
        showCancel: false
      })
    })
  },

  onReachBottom () {
    if (this.data.hasNext) {
      var _currentPag = this.data.currentPage + 1
      this.setData({
        currentPage: _currentPag
      })
      this.getDocumentList()
    }
  },

  handleReportDetail (e) {
    var reportid = e.currentTarget.dataset.reportid
    wx.navigateTo({
      url: '/pages/reportDetail/reportDetail?reportid=' + reportid
    })
  },

  /**
   * 新建档案
   */
  handleAddFile() {
    wx.navigateTo({
      url: '/pages/createFile/createFile?from=myFile'
    })
  },

  /**
   * 管理档案
   */
  handleFile() {
    if (this.data.fileList.length !== 0) {
      wx.navigateTo({
        url: '/pages/handleFile/handleFile',
      })
    } else {
      wx.showToast({
        title: '暂无档案',
        icon: 'error'
      })
    }
  },
  handleShowQRCode (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var realname = e.currentTarget.dataset.realname
    this.setData({
      qrUserName: realname
    })
    wx.showLoading({
      title: '生成中...'
    })
    createQRcode({ documentId: id }).then(res => {
      wx.hideLoading()
      that.setData({
        QRCodeBase64: that.data.baseStr + res.data,
        qrcodeDialog: true
      })
    }).catch(e => {
      wx.hideLoading()
      wx.showModal({
        content: e,
        confirmColor: '#333',
        showCancel: false
      })
    })
  },
  handleCloseQRCode () {
    this.setData({
      qrcodeDialog: false,
      QRCodeBase64: ''
    })
  }
})