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

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: 0,
    routerTwo: [
      {
        icon: '../../../assets/che_guan.png',
        text: '撤馆流程',
        time: '',
        url: '',
        paras: {},
        zindex: 3
      }
    ],
    routerThree: [
      {
        icon: '../../../assets/wen_ti.png',
        text: '常见问题',
        time: '',
        url: 'pages/question/question',
        paras: {
          type: 2
        },
        zindex: 3
      }
    ],
    routerFour:[
      {
        icon: '../../../assets/personal_info.png',
        text: '归还解绑图书',
        time: '',
        url: 'dispatcher/pages/book_entry/book_entry',
        paras: {
          type: 2
        },
        zindex: 3
      }
    ],
    footerData: {
      actIndex: 2,
      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.userInfo = getGlobalVal('userInfo')
    this.setData({ options, isLoaded: 1 })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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()
  },
    /**
   * 选择跳转进入页
   */
  selectNavCtl: function () {
      navigateTo({
        url: 'dispatcher/pages/book_entry/book_entry',
      })
  },
  /**
* 扫一扫书籍标签二维码
*/
  codeScanCtl: function () {
    wx.scanCode({
      success: res => {
        const url = res.result
        const detailsId = url.split('_')[1].split('.')[0]
        console.log(res)
        if (!detailsId) {
          wx.showToast({
            title: qrcodeInvalidToastText,
            icon: 'none'
          })
          return
        }
        this.setData({ detailsId })
         wx.setStorageSync('detailsId', detailsId);
        this.setBookInfoByIsbn()
      }
    })
  }, 
  /**
 * 调用微信扫一扫
 */

  /**
   * 根据isbn13查询书籍信息
   */
  setBookInfoByIsbn: function () {
    const continuousFn = { fn: this.setBookInfoByIsbn, param: { ...arguments } }
    const { detailsId } = this.data
    const that=this 
      ScanForBookDetail({ data: { detailId:detailsId }, continuousFn }).then(res => {
      const {bookInfo} = res.data
      wx.setStorageSync('bookInfo', bookInfo);
      this.selectNavCtl()
      cb && cb(res)
    }).catch(res => {
      
      // navigateBack()
      cb && cb(res)
    }) 
  },
  /**
   * 选择路由跳转
   */
  selectRouterCtl: function (e) {
    const continuousFn = { fn: this.selectRouterCtl, param: { ...arguments } }
    const { index } = e.currentTarget.dataset
    const { routerTwo } = this.data
    const { zindex = 3 } = routerTwo[index]
    GetDispatcherInfo({ continuousFn }).then(res => {
      const { cancelType, libId, cancelMsg = '没有撤馆权限' } = res.data
      let url = ''
      let paras = { libId }
      switch (~~cancelType) {
        case 0:
          url = 'dispatcher/pages/withdraw_library/withdraw_library'
          break
        case 1:
          url = 'dispatcher/pages/withdraw/withdraw'
          paras.type = 1
          break
        case 2:
          wx.showToast({
            title: cancelMsg,
            icon: 'none'
          })
          return
          break
        case 3:
          url = 'dispatcher/pages/withdraw/withdraw'
          paras.type = 2
          break
      }
      navigateTo({ url, zindex, paras })
    })
  },
})