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

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    thumbnail,
    type: 2,
    isShowToast: false,
    toastImage: '',
    toastTitle: '',
    footerData: {
      actIndex: 0,
      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)
      }
    })
  },

  /**
   * 再次调用扫一扫
   */
  scanAgain: function () {
    this.setTimeoutFn = setTimeout(() => {
      this.setData({ isShowToast: false })
      this.scanCtl()
    }, 2000)
  },

  /**
   * 再次调用扫一扫
   */
  cancelScanAgain: function () {
    if (this.setTimeoutFn) clearTimeout(this.setTimeoutFn)
  },

  /**
   * 送书离馆
   */
  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 => {
      const { bookImg = '', title = '', id = '' } = res.data
      this.setData({ toastImage: bookImg, toastTitle: title, ids: [id], isShowToast: true })
      this.scanAgain()
    }).catch(res => {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    })
  },

  /**
   * 删除
   */
  delCtl: function () {
    const continuousFn = { fn: this.delCtl, param: { ...arguments } }
    const { options = {}, type = 0, ids = [] } = this.data
    const { libId = '' } = options
    this.cancelScanAgain()
    if (!ids.length) {
      wx.showToast({
        title: '请先选中要删除的图书',
        icon: 'none'
      })
      return
    }
    DeleteSweepedBooks({ data: { ids, libId, type }, continuousFn }).then(res => {
      this.setData({ isShowToast: false })
      wx.showToast({
        title: '取消成功',
      })
    })
  },
})