// pages/childList/childList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    thumbnail: app.globalData.thumbnail,
    baseImgUrl: app.globalData.baseImgUrl,
    hiddenModel: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(options);
    console.log(options)
    if (!options.vipId) {
      const userId = app.globalData.userId || wx.getStorageSync('userId')
      const vipId = app.globalData.parentVipId || wx.getStorageSync(`parentVipId-${userId}`)
      options.vipId = vipId
      options.userId = userId
      this.setData(options);
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getChildInfo();
  },

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

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

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

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

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

  },

  /**
   * 底部导航跳转
   */
  redirectCtl: app.redirectCtl,

  /**
   * 路由跳转
   */
  // navigateCtl: app.navigateCtl,
  navigateCtl: function (e) {
    const { add } = e.currentTarget.dataset
    console.log(e)
    if (add==1) {
      wx.removeStorageSync('list')
      app.navigateCtl(e);
    }else{
      app.navigateCtl(e);
    }
    
  },
  /**
   * 我的信息
   */
  getMyInfo: function () {
    const userId = app.globalData.userId || wx.getStorageSync('userId')
    if (userId) {
      console.log('123')
      const parentInfo = app.globalData.parentInfo || wx.getStorageSync(`parentInfo-${userId}`)
      const parentVipList = wx.getStorageSync(`parentVipList-${userId}`) || []
      const parentVipLiIdx = app.globalData.parentVipLiIdx || wx.getStorageSync(`parentVipLiIdx-${userId}`) || 0
      const parentVipId = app.globalData.parentVipId || wx.getStorageSync(`parentVipId-${userId}`) || ''
      const parentImg = parentInfo.imgUrl || '';
      const { imgUrl } = parentVipList[parentVipLiIdx] ? parentVipList[parentVipLiIdx] : { imgUrl: '' };
      this.setData({ userId, parentInfo, parentVipList, parentVipLiIdx, parentVipId, parentImg, childImg: imgUrl })
      // console.log(parentVipList);
    }
  },
  // 获取孩子信息
  getChildInfo:function(){
    wx.showLoading({
      title: '加载中',
    })
    const { userId}=this.data;
    if (userId) {
      const parentVipLiIdx = app.globalData.parentVipLiIdx || wx.getStorageSync(`parentVipLiIdx-${userId}`) || 0;
      this.setData({ parentVipLiIdx })
    }
    const postData = { fn: this.getChildInfo, param: { ...arguments } }
    app.post('/api/parents/parents/V2/getMyInfo', {}, 0, postData).then(res => {
      wx.hideLoading();
      const { childList}=res.data;
      this.setData({ parentVipList: childList})
    })
  },
  /**
   * 提示框隐藏
   */
  allShowModel: function () {
    this.setData({ hiddenModel: false })
  },
  /**        
   * 切换孩子信息
   */
  modalInfo: function (e) {
    const { index } = e.currentTarget.dataset;
    this.setData({ hiddenModel: true, childIndex: index })
  },

  checkActChildCtl: function (res) {
    const { childIndex } = this.data;
    const { parentVipLiIdx, parentVipList, userId } = this.data;
    this.setData({
      parentVipLiIdx: childIndex,
      parentVipId: parentVipList[childIndex]['vipId'],
      hiddenModel: false
    })
    getApp().globalData.parentVipLiIdx = childIndex
    getApp().globalData.parentVipId = parentVipList[childIndex]['vipId']
    wx.setStorageSync(`parentVipLiIdx-${userId}`, childIndex)
    wx.setStorageSync(`parentVipId-${userId}`, parentVipList[childIndex]['vipId'])
    this.setData({ pageNo: 1 })
    // this.getChildInfo();
  },
})