// pages/home/home.js
const app = getApp();
const { formatDate, formatTime2, getDateDiff, formatDateZyb} = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: app.globalData.baseImgUrl,
    thumbnail: app.globalData.thumbnail,
    maxCreateTime: formatTime2(new Date()),
    isOne:false,
    hiddenModel:false,
    parentalConcern: [
                "知识点掌握情况",
                "学习习惯养成"
    ],
    subjectTaskList: [],
   pageNo:1,
   pageSize:10,
   list:[],
   isTimes:false,
   isOutTime:false,
   isAll: false,
    footerData: {
      actIndex: 0,
      list: [
        {
          text: '首页',
          url: '../../image/shou_ye_hui.png',
          actUrl: '../../image/shou_ye.png',
          src: 'homework_list',
          id: 'homework_list'
        },
        {
          text: '学习成长报告',
          url: '../../image/xue_qing_hui.png',
          actUrl: '../../image/xue_qing.png',
          src: 'learning_report',
          id: 'learning_report'
        },
        {
          text: '我的',
          url: '../../image/wo_de_hui.png',
          actUrl: '../../image/wo_de.png',
          src: 'me',
          id: 'me'
        }
      ],

    },
    headerData: {
      actIndex: 0,
      list: [
        {
          text: '作业',
          src: 'homework_list',
          id: 'homework_list'
        },
        {
          text: '难题',
          src: 'problem_list',
          id: 'problem_list'
        },
        // {
        //   text: '辅导课',
        //   src: 'one_to_one',
        //   id: 'one_to_one'
        // },
        
      ],

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getWxloginCode(code => {
      this.setData({ code })
    })
    options.vipId = app.getGlobalAttributeValue(`parentVipId-${app.getGlobalAttributeValue(`userId`)}`)
    this.setData({ options})
    console.log(options)
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getVipList()
    const userInfo = wx.getStorageSync('userInfo')
    const { vipId } = this.data.options
    if (!userInfo) {
      this.setData({ isOne: true })
    } else if (vipId){
      this.getMyInfo();
      this.getHomeworkList();
    }
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({pageNo:1})
    this.getHomeworkList(res => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { isAll} = this.data
    if (isAll) {
      return
    } 
      this.getHomeworkList()
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 底部导航跳转
   */
  redirectCtl: app.redirectCtl,

  /**
   * 路由跳转
   */
  navigateCtl: app.navigateCtl,
  /**
   * 我的信息
   */
  getMyInfo: function () {
    const { isTimes}=this.data;
    const userId = app.globalData.userId || wx.getStorageSync('userId');
    const postData = { fn: this.getChildInfo, param: { ...arguments } }
    app.post('/api/parents/parents/V2/getMyInfo', {}, 0, postData).then(res => {
      const { childList, phone } = res.data;
        if (!phone) {
          this.redirectCtl({ url: 'phone', method: {} }, true)
        } 
    if (userId) {
      const parentInfo = app.globalData.parentInfo || wx.getStorageSync(`parentInfo-${userId}`)
      const parentVipList = childList
      const parentVipLiIdx = app.globalData.parentVipLiIdx || wx.getStorageSync(`parentVipLiIdx-${userId}`) || 0;
      if (parentVipList.length>0) {
        var times = parentVipList[parentVipLiIdx].zybEndDate;
        var messageTime = formatDateZyb(times, 'Y-M-D');
        var nowTime = new Date().getTime();
        var today = formatDateZyb(nowTime/1000, 'Y-M-D');
        var day1 = new Date(today);
        var day2 = new Date(messageTime);
        var differDay = (day2 - day1) / (1000 * 60 * 60 * 24);
        // console.log(differDay)
        if (0 <=differDay && differDay<=6){
          this.setData({ isTimes: true, messageTime, times})
        } else if (differDay<0){
          this.setData({ isOutTime: true, times})
        } else if (differDay > 6) {
          this.setData({ isOutTime: false, isTimes:false })
        }
      }
      this.setData({ userId, parentInfo, parentVipLiIdx, parentVipList})
    }
    })
  },
  
  // 获取作业列表
  getHomeworkList:function(){
    const postData = { fn: this.getHomeworkList, param: { ...arguments } }
    const { vipId } = this.data.options
    const { pageNo, pageSize, maxCreateTime, list = []} = this.data;
    wx.showLoading({
      title: '加载中',
    })
    app.post('/api/parents/parents/V2/homeworkTask', { vipId, pageNo, pageSize, maxCreateTime}, 0, postData).then(res => {
      wx.hideLoading();
      const listTemp = res.data.list;
      const {todayTask}=res.data;
      let arr = []
      if (pageNo == 1) {
        arr = [...listTemp]
      } else {
        arr = [...list, ...listTemp]
      }
      this.setData({
        todayTask,
        list: arr,
        pageNo: listTemp.length == pageSize ? pageNo + 1 : pageNo,
        isAll: listTemp.length < pageSize
      })
    })
  }, 
  /**
   * 微信授权 
   */
  bindGetUserInfo: function (e) {
    const { isSelect, code } = this.data;
    if (e.detail.userInfo) {
      app.saveUserInfo(e.detail, code)
    } else {
      wx.showToast({
        title: '微信授权失败',
        icon: 'none'
      })
    }
  }
})