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

  /**
   * 页面的初始数据
   */
  data: {
    list:[
      {
        subject:'语文',
        imgUrl:'../../image/yuwen.png',
        bookList:[]
      },
      {
        subject: '数学',
        imgUrl: '../../image/shuxue.png',
        bookList: []
      },
      {
        subject: '英语',
        imgUrl: '../../image/yingyu.png',
        bookList: []
      }
    ],
    bookId:'',
    subject:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(options)
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInformation()
    const { subject, isAddChild, isSelect}=this.data;
    if (!subject && isAddChild!=1){
      this.getTeaching();
    }
    if (isAddChild == 1){
      this.localTeaching();
    }
      var list = wx.getStorageSync('list');
      if (subject) {
        this.packaging(list)
      }
      this.setData({ list })
      console.log(list)
    
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  /**
   * 跳转
   */
  navigateCtl: app.navigateCtl,
  /**
   * 底部导航跳转
   */
  redirectCtl: app.redirectCtl,
  /**
  * 获取教辅
  */
  getTeaching: function () {
    const postData = { fn: this.getTeaching, param: { ...arguments } }
    let { list = [], vipId} = this.data
    app.post('/api/parents/parents/V2/childBookList', {vipId}, 0, postData).then(res => {
      const {list}=res.data;
      const correctListTemp = (res => {
        const list = res
        const listTemp = []
        for (let i in res) {
          if (list[i].subject == '语文') {
            list[i].imgUrl= '../../image/yuwen.png';
          } else if (list[i].subject == '数学'){
            list[i].imgUrl = '../../image/shuxue.png';
          } else if (list[i].subject == '英语') {
            list[i].imgUrl = '../../image/yingyu.png';
          }
        }
        return res
      })(list)
      wx.setStorageSync('list', list)
      this.setData({list})
    })
  },
  /**
  * 本地获取教辅
  */
  localTeaching:function(){
    const { isAdd}=this.data;
    var that=this;
    console.log(isAdd)
    if (isAdd=="true"){
      var list =wx.getStorageSync('list');
      wx.setStorageSync('list', list)
      that.setData({ list })
      console.log('1==='+list)
    }else{
      const { list } = that.data;
      wx.setStorageSync('list', list)
      that.setData({ list })
      console.log('2---'+list)
    }
    
   
  },
  /**
  * 封装
  */
  packaging:function(res){
    const {bookId, subject, bookName}=this.data;
    var items= { bookId, subject, bookName};
    var temp={};
    for(var i in res){
      if (res[i].subject===subject){
        console.log(res[i])
        // temp[`list[${i}].bookList`] = items;
        res[i].bookList.push(items)
      }
    }
    wx.setStorageSync('list', res)
    this.setData({list:res})
  },
  /**
  * 删除
  */
  deleteBook:function(e){
    const { index, key } = e.currentTarget.dataset;
    const  {list}=this.data;
    list[index].bookList.splice(key, 1)
    wx.setStorageSync('list', list)
    this.setData({list})
    console.log(list)
  },
  /**
  * 获取个人信息
  */
  getInformation: function (e) {
    const postData = { fn: this.getInformation, param: { ...arguments } }
    const { vipId} = this.data
    app.post('/api/parents/parents/V2/getMyChildInfo', { vipId }, 0, postData).then(res => {
      const { grade}=res.data
      this.setData({grade})
    })
  },
  /**
  * 保存
  */
  save:function(){
    const postData = { fn: this.saveInfo, param: { ...arguments } }
    const { list, vipId,obj}=this.data
    var arr=[];
    for(var i in list){
      arr = arr.concat(list[i].bookList)
    }
    var bookList=[];
    for(var i in arr){
      bookList.push({ bookId: arr[i].bookId, subject: arr[i].subject, bookName: arr[i].bookName,})
    }
    if(obj){
      var newObj = JSON.parse(obj);
    }else{
      var newObj = { vipId}
    }
    newObj['bookList'] = bookList
    console.log(newObj)
    app.post('/api/parents/parents/V2/upMyChildInfo', newObj, 0, postData).then(res => {
      wx.showToast({
        title: '保存成功',
        icon: 'none',
        duration: 1000
      })
      wx.removeStorageSync('list')
    })
  }
})