// parents/pages/me/me.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { CheckRole } from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, hasLibraryService, hasVipService, saveUserInfo, getWxloginCode} = getApp()
const { baseImgUrl, qrcodeInvalidToastText } = globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: 0,
    isShowVipModal: 0,
    positionArray: ['馆长', '家长'],
    positionIndex: 0,
    canIUseGetUserInfo: wx.canIUse('button.open-type.getUserInfo'),
    vip: {
      position: ''
    },
    routerOne: [
      {
        icon: '../../../assets/vip.png',
        text: '会员服务',
        time: '',
        paras: {},
        url: '',
        zindex: 3
      },
    ],
    routerTwo: [
      {
        icon: '../../../assets/personal_info.png',
        text: '调书申请记录',
        time: '',
        paras: {
          type: 1
        },
        url: 'president/pages/allocate_books_record/allocate_books_record',
        zindex: 3
      },
      {
        icon: '../../../assets/apply_pages.png',
        text: '申请新书记录',
        time: '',
        paras: {},
        url: 'president/pages/newbooks_apply_record/newbooks_apply_record',
        zindex: 3
      },
    ],
  
    footerData: {
      actIndex: 2,
      list: [
        {
          text: '日常事务',
          icon: '../../../assets/lock_gary.png',
          actIcon: '../../../assets/lock_blue.png',
          url: 'president/pages/home/home',
          zindex: 3,
        },
        {
          text: '人员管理',
          icon: '../../../assets/people_manager_gary.png',
          actIcon: '../../../assets/people_manager_blue.png',
          url: 'president/pages/Personnel_management/Personnel_management',
          zindex: 3,
        },
        {
          text: '我的',
          icon: '../../../assets/me_gray.png',
          actIcon: '../../../assets/me_blue.png',
          url: 'president/pages/me/me',
          zindex: 3,
        }
      ]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userId = getGlobalVal('userId')
    options.library = getGlobalVal('library')
    options.vipList = getGlobalVal('vipList', `vipList_${userId}`)
    options.vipIndex = getGlobalVal('vipIndex', `vipIndex_${userId}`)
    options.vipId = getGlobalVal('vipId', `vipId_${userId}`)
    options.vip = getGlobalVal('vip', `vip_${userId}`)
    options.userId = userId
    options.userInfo = getGlobalVal('userInfo')
    options.list = getGlobalVal('list')
    console.log(options)
    this.setData({ options, isLoaded: 1 })
    getWxloginCode(code => {
      this.setData({ code })
    })
  },

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

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

  },
  testclick: function () {
    navigateTo({
      url: 'president/pages/newbooks_apply_record/newbooks_apply_record?id=113f76b8-8d84-4b18-b882-c4098d515067',
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  /**
   * 选择跳转进入页
   */
  selectRouteCtl: function (e) {
    const { index } = e.currentTarget.dataset
    const { routerTwo } = this.data
    const { url, zindex } = routerTwo[index]
    // if (index==2){
      navigateTo({ url, zindex })
    // }else{
    //   if (!hasVipService()) return
    //   navigateTo({ url, zindex })
    // }
    
  },
  /**
   * 身份切换
   */
  pickerpositionCtl: function (e) {
    const { value } = e.detail
    const { positionArray, positionIndex } = this.data
    this.setData({ 'vip.position': ~~value + 1, positionIndex: value })
    if (value ==0) {
      wx.showToast({
        title: '您目前的身份已经是馆长啦',
        icon: 'none'
      })
      return
    }
    else{
      this.toggleSubmit()
    }
  },
  /**
 * 身份切换家长
 */
   toggleSubmit: function () {
    const continuousFn = { fn: this.delCtl, param: { ...arguments } }
    CheckRole({ data: { }, continuousFn }).then(res => {
      wx.showToast({
        title: res.msg
      })
      if (res.code ==999){
        redirectTo({ url: 'parents/pages/main/main', paras: { isDirector: 1 } })
      }
    })
  },
  /**
   * 显示学员切换框
   */
  toggleSelectVipCtl: function () {
    const { vipList } = this.data.options
    // if (!vipList || vipList.length < 2) return
    this.setData({ isShowVipModal: !this.data.isShowVipModal })
  },
    /**
   * 添加孩子跳转
   */
  addchildPage: function () {
    const { routerTwo } = this.data
    const { url, zindex } = routerTwo[0]
    if (!hasVipService()) return
    navigateTo({ url, zindex })
  },
  /**
   * 学员切换
   */
  selectVipCtl: function (e) {
    const { index } = e.currentTarget.dataset
    const { options } = this.data
    const { vipIndex, vipList, userId } = options
    if (index == vipIndex) {
      return
    }
    const { vip_id } = vipList[index]
    if (vip_id) {
      options.vip = vipList[index]
      getApp().globalData['vip'] = vipList[index]
      wx.setStorageSync(`vip_${userId}`, vipList[index])

      options.vipIndex = index
      getApp().globalData['vipIndex'] = index
      wx.setStorageSync(`vipIndex_${userId}`, index)

      options.vipId = vip_id
      getApp().globalData['vipId'] = vip_id
      wx.setStorageSync(`vipId_${userId}`, vip_id)

      wx.removeStorageSync('library')
    }

    this.setData({ options, isShowVipModal: 0 })
  },
  /**
   * 获取微信用户信息 
   */
  getUserInfoCtl: function (e) {
    const { detail } = e
    const { userInfo, errMsg = '触发获取微信用户信息失败' } = detail
    if (userInfo) {
      if (isFn(saveUserInfo)) saveUserInfo(detail, this.data.code)
    } else {
      wx.showToast({
        title: '触发微信授权失败',
        icon: 'none'
      })
    }
  }
})