// dispatcher/pages/dispatch_leave/dispatch_leave.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { DispatcherScanning } from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData } = getApp()
const { baseImgUrl, qrcodeInvalidToastText } = globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    type: 1,
    footerData: {
      actIndex: 1,
      list: [
        {
          text: '送书到馆',
          icon: '../../../assets/song_shu_dao_guan_gray.png',
          actIcon: '../../../assets/song_shu_dao_guan_blue.png',
          url: 'dispatcher/pages/dispatch_go/dispatch_go',
          zindex: 3
        },
        {
          text: '带书离馆',
          icon: '../../../assets/dai_shu_li_guan_gray.png',
          actIcon: '../../../assets/dai_shu_li_guan_blue.png',
          url: 'dispatcher/pages/dispatch_leave/dispatch_leave',
          zindex: 3
        },
        {
          text: '我的',
          icon: '../../../assets/me_gray.png',
          actIcon: '../../../assets/me_blue.png',
          url: 'dispatcher/pages/me/me',
          zindex: 3
        }
      ]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.userId = getGlobalVal('userId')
    options.libId = (options.libId ? options.libId : getGlobalVal(`dispatchLibId_${options.userId}`) ? getGlobalVal(`dispatchLibId_${options.userId}`) : '')
    this.setData({ options })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const obj = { navigateTo, redirectTo, navigateBack, viewImage }
    for (const i in obj) {
      this[i] = obj[i]
    }
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (isFn(sharePage)) return sharePage()
  },

  /**
   * 扫书
   */
  scanCtl: function () {
    wx.scanCode({
      success: res => {
        const url = res.result
        const detailsId = url.split('_')[1].split('.')[0]
        if (!detailsId) {
          wx.showToast({
            title: qrcodeInvalidToastText,
            icon: 'none'
          })
          return
        }
        this.dispatcherScanningFn(detailsId)
      }
    })
  },

  /**
   * 送书离馆
   */
  dispatcherScanningFn: function (detailsId) {
    const continuousFn = { fn: this.dispatcherScanningFn, param: { ...arguments } }
    const { options = {}, type = 0 } = this.data
    const { libId = '' } = options
    DispatcherScanning({ data: { detailsId, libId, type }, continuousFn }).then(res => {
      wx.showToast({
        title: '扫书成功',
        icon: 'success'
      })
      setTimeout(() => {
        this.scanCtl()
      }, 2000)
    }).catch(res => {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    })
  }
})