// parents/pages/home/home.js
import { routers, viewImage, sharePage, isFn, getGlobalVal, getWxLocationSetting } from '../../utils/util.js'
import { CheckNext} from '../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, hasLibraryService, hasVipService } = getApp()
const { baseImgUrl, qrcodeInvalidToastText, isIphoneX, noVipListToastText } = globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isIphoneX,
    isFirst: 0,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.q){
        var scene = decodeURIComponent(options.q)  // 使用decodeURIComponent解析  获取当前二维码的网址
        const detailsId = scene.split('=')[1].split('.')[0]
        if (!detailsId) {
          wx.showToast({
            title: qrcodeInvalidToastText,
            icon: 'none'
          })
          return
        }
        this.scanCtl(detailsId)
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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 () {
    // this.getList(res => {
    //   wx.stopPullDownRefresh()
    // })
  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (isFn(sharePage)) return sharePage()
  },
  
  /**
   * 跳转页面
   */
  scanCtl: function (detailsId) {
    const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
    const { library, vipId, userId } = this.data
    var roleType=wx.getStorageSync('isDispatcher')
    CheckNext({ data: { detailsId, roleType }, continuousFn }).then(res => {
      const { type, detailsId } = res.data
      
      if (type==1){
        redirectTo({ url: 'president/pages/book_entry/book_entry', paras: { detailsId,isTwo:true,isFirst:false} })
      }else if (type==2){
        redirectTo({ url: 'president/pages/book_entry/book_entry', paras: { detailsId,isTwo:false,isFirst:true} })
      }else if (type==3){
        redirectTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
      }else if (type==4){
        redirectTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
      }else if (type==5){
        redirectTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
      }else if (type==6){
        redirectTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
      }else if (type==7){
        redirectTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
      }else if (type==8&&roleType==1){
        redirectTo({ url: 'dispatcher/pages/home/home', paras: {} })
      }else if (type==8&&roleType==2){
        redirectTo({ url: 'parents/pages/main/main', paras: {} })
      }else if (type==8&&roleType==3){
        redirectTo({ url: 'president/pages/home/home', paras: {} })
      }
     
      this.setData({ ...res.data })
    })
  },

  
})