// parents/pages/me/me.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { VipDetailInfo, EditVipservice } 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,
    starttime: '',
    endTime:'',
    MembersVipId:"",
    callGetPhone: '',
    childId:'',
    canIUseGetUserInfo: wx.canIUse('button.open-type.getUserInfo'),
    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/information/information',
        zindex: 3
      },
      {
        icon: '../../../assets/apply_pages.png',
        text: '借阅记录',
        time: '',
        paras: {},
        url: 'president/pages/management_loan_records/management_loan_records',
        zindex: 3
      },
      {
        icon: '../../../assets/problem_icon.png',
        text: '到馆记录',
        time: '',
        paras: {},
        url: 'president/pages/pavilion_record/pavilion_record',
        zindex: 3
      },
    ],
    footerData: {
      actIndex: 1,
      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) {
    // this.GetDate()
    this.setData({
      MembersVipId: options.vipId
    });
    wx.setStorageSync('vipId', options.vipId);
    options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`)
    options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : ''
    wx.setNavigationBarTitle({
      title: `详情`,
    })
    this.setData(options)
    this.getvipInfoData(res => {
      this.setData({ 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()
  },
  GetDate: function () {
    const now = new Date();
    const year = now.getFullYear();         //年
    const month = now.getMonth() + 1;     //月
    const day = now.getDate();              //日
    const nowdate = year + '-' + month + '-' + day
    this.setData({ endTime: nowdate })

  },


  // 拨打电话给收件人
   callGetPhone(e) {
      // 号码
      let telPhone = e.currentTarget.dataset.getphone;
      this.callPhone(telPhone);
     },

  /**
  * 拨打电话 - 可简单封装工具集
   */
   callPhone(phoneNumber) {
      wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function() {
        console.log("拨打电话成功!")
      
      },
      fail: function() {
      console.log("拨打电话失败!")
    
      }
    })
   },
  /**
   * 选择跳转进入页
   */
  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 })
    // }
    
  },
  /**
 * 提示框隐藏
 */
  allShowModel: function () {
    this.setData({ isShowVipModal: false })
  },
  /**
* 会员管理详情
*/
  getvipInfoData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { libId, month, MembersVipId } = this.data
    VipDetailInfo({ data: { libId, vipId: MembersVipId }, continuousFn }).then(res => {
      const { vipInfo } = res.data
      const { phone } = vipInfo
      this.setData({ vipInfo, callGetPhone: phone})
      console.log(vipInfo)
      if (isFn(cb)) cb()
    }).catch(res => {
      this.setData({ isLoaded: true })
      if (isFn(cb)) cb()
    })
  },
  /**
   * 显示修改会员服务有效期换框
   */
  toggleSelectVipCtl: function (e) {
    const { starttime, endtime, childid } = e.currentTarget.dataset
    this.setData({ isShowVipModal: !this.data.isShowVipModal, childId: childid,endTime: endtime, starttime: starttime })
  },
  /**
 * 修改会员服务有效期
 */
  submitCtl: function () {
    const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
    const { libId, endTime, childId } = this.data
    EditVipservice({ data: { endTime, id: childId }, continuousFn }).then(res => {
      const { msg = [] } = res
      wx.showToast({
        title: msg ,
        icon: 'none'
      })
    
    })
  },
  /**
* 选择日期
*/
  changeDate: function (e) {
    const { value } = e.detail;
    this.setData({ endTime: value })
    this.submitCtl(res => {
      this.setData({ isShowVipModal: false })
    })
  },
  /**
   * 获取微信用户信息 
   */
  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'
      })
    }
  }
})