var app = getApp();
Page({
  data: {
    leftEye: '',
    rightEye: '',
    trainingList: [],
    firstTrainType: '',
    secondTrainType: '',
    luoeyeArray: [
      ['4.0','4.1','4.2','4.3','4.4','4.5','4.6','4.7','4.8','4.9','-','5.0','5.1','5.2','5.3'],
      ['4.0','4.1','4.2','4.3','4.4','4.5','4.6','4.7','4.8','4.9','-','5.0','5.1','5.2','5.3']
    ],
    luoeyeIndex: [10, 10],
    trainArray: ['单词训练','动态训练','静态训练'],
    timeArray: ['训练10分钟','训练12分钟','训练14分钟','训练16分钟','训练18分钟','训练20分钟',],
    DArray: [{name:'训练10分钟',time:10},{name:'训练12分钟',time:12},{name:'训练14分钟',time:14},{name:'训练16分钟',time:16},{name:'训练18分钟',time:18},{name:'训练20分钟',time:20},],
    JArray: [{name:'训练10分钟',time:10},{name:'训练12分钟',time:12},{name:'训练14分钟',time:14},{name:'训练16分钟',time:16},{name:'训练18分钟',time:18},{name:'训练20分钟',time:20},],
    firstTrainIndex: 0,//第一组训练类型
    secondTrainIndex: 0,//第二组训练类型
    firstDIndex: 0,//第一组动态详细设置
    secondDIndex: 0,//第二组动态详细设置
    firstJIndex: 0,//第一组静态详细设置
    secondJIndex: 0,//第二组静态详细设置
    wordSetShow: false,//单词训练弹窗

    
    wordNumber: 10,//第一组单词数量
    wordNumberSecond: 10,//第二组单词数量
    wordType: '',
    teachArray: [],
    teachIndex: 0,
    teachIndexSecond: 0,
    bookArray: [],
    bookIndex: 0,
    bookIndexSecond: 0,
    unitArray: [],
    unitArraySecond: [],
    unitIndex: 0,
    unitIndexSecond: 0,
  },
  onLoad: function () {
    let me = this;
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'Trainings/index',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token')
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            leftEye: res.data.data.leftEye,
            rightEye: res.data.data.rightEye,
            firstTrainType: res.data.data.trainingList[0].type,
            secondTrainType: res.data.data.trainingList[1].type,
            trainingList: res.data.data.trainingList,
            firstTrainIndex: me.data.trainArray.indexOf(res.data.data.trainingList[0].type),
            secondTrainIndex: me.data.trainArray.indexOf(res.data.data.trainingList[1].type),
          })
          if(res.data.data.trainingList[0].type=='动态训练'){
            for(let i=0;i<me.data.timeArray.length;i++){
              if(me.data.timeArray[i].indexOf(res.data.data.trainingList[0].duration)<0){
                
              }else{
                me.setData({
                  firstDIndex: i
                })
              }
            }
          }
          if(res.data.data.trainingList[1].type=='动态训练'){
            for(let i=0;i<me.data.timeArray.length;i++){
              if(me.data.timeArray[i].indexOf(res.data.data.trainingList[1].duration)<0){
                
              }else{
                me.setData({
                  secondDIndex: i
                })
              }
            }
          }
          if(res.data.data.trainingList[0].type=='静态训练'){
            for(let i=0;i<me.data.timeArray.length;i++){
              if(me.data.timeArray[i].indexOf(res.data.data.trainingList[0].duration)<0){
                
              }else{
                me.setData({
                  firstJIndex: i
                })
              }
            }
          }
          if(res.data.data.trainingList[1].type=='静态训练'){
            for(let i=0;i<me.data.timeArray.length;i++){
              if(me.data.timeArray[i].indexOf(res.data.data.trainingList[1].duration)<0){
                
              }else{
                me.setData({
                  secondJIndex: i
                })
              }
            }
          }
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  luoeyeChange: function (e) {// 本次裸眼视力
    let luoData = {
      left: this.data.luoeyeArray[0][e.detail.value[0]],
      right: this.data.luoeyeArray[1][e.detail.value[1]]
    }
    if(luoData.left=='-'||luoData.right=='-'){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请选择裸眼视力'
      })
      this.setData({
        luoeyeIndex: [10,10]
      })
    }else{
      this.setData({
        luoeyeIndex: e.detail.value
      })
      console.log('本次裸眼视力：')
      console.log(luoData)
      let me = this;
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'Trainings/recommendTime',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
          leftEye: luoData.left,
          rightEye: luoData.right,
        },
        success: function (res) {
          // 请求成功后关闭Loading
          wx.hideLoading();
          if(res.data.errno==200){
            me.setData({
              wordNumber: res.data.data.newWordsNumber,
              wordNumberSecond: res.data.data.newWordsNumber,
            })
            
            for(let i=0;i<me.data.timeArray.length;i++){
              if(me.data.timeArray[i].indexOf(res.data.data.dynamicTime)<0){
                
              }else{
                me.setData({
                  firstDIndex: i,
                  secondDIndex: i
                })
              }
            }
            for(let i=0;i<me.data.timeArray.length;i++){
              if(me.data.timeArray[i].indexOf(res.data.data.staticTime)<0){
                
              }else{
                me.setData({
                  firstJIndex: i,
                  secondJIndex: i
                })
              }
            }
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })
    }
  },
  // 第一组训练类型
  firstTrainChange: function (e) {
    console.log('第一组训练类型：'+e.detail.value+this.data.trainArray[e.detail.value])
    this.setData({
      firstTrainIndex: e.detail.value,
      firstTrainType: this.data.trainArray[e.detail.value]
    })
    if(e.detail.value==0){
      let me = this;
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'Trainings/publisher',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
        },
        success: function (res) {
          // // 请求成功后关闭Loading
          // wx.hideLoading();
          if(res.data.errno==200){
            me.setData({
              teachArray: res.data.data.list,
            })
            wx.request({
              url: app.globalData.apiUrl + 'Trainings/book',
              method: 'POST',
              data: {
                token: wx.getStorageSync('token'),
                publisherId: res.data.data.list[0].id
              },
              success: function (res) {
                // 请求成功后关闭Loading
                // wx.hideLoading();
                if(res.data.errno==200){
                  me.setData({
                    bookArray: res.data.data.data,
                  })
                  wx.request({
                    url: app.globalData.apiUrl + 'Trainings/chapter',
                    method: 'POST',
                    data: {
                      token: wx.getStorageSync('token'),
                      bookId: res.data.data.data[0].id
                    },
                    success: function (res) {
                      // 请求成功后关闭Loading
                      wx.hideLoading();
                      if(res.data.errno==200){
                        me.setData({
                          unitArray: res.data.data.data,
                        })
                      }else{
                        wx.showModal({
                          title: '提示',
                          showCancel: false,
                          content: res.data.msg
                        })
                      }
                    }
                  })
                }else{
                  wx.showModal({
                    title: '提示',
                    showCancel: false,
                    content: res.data.msg
                  })
                }
              }
            })
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })
    }
  },
  // 第二组训练类型
  secondTrainChange: function (e) {
    console.log('第二组训练类型：'+e.detail.value+this.data.trainArray[e.detail.value])
    this.setData({
      secondTrainIndex: e.detail.value,
      secondTrainType: this.data.trainArray[e.detail.value]
    })
    if(e.detail.value==0){
      let me = this;
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'Trainings/publisher',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
        },
        success: function (res) {
          // // 请求成功后关闭Loading
          // wx.hideLoading();
          if(res.data.errno==200){
            me.setData({
              teachArray: res.data.data.list,
            })
            wx.request({
              url: app.globalData.apiUrl + 'Trainings/book',
              method: 'POST',
              data: {
                token: wx.getStorageSync('token'),
                publisherId: res.data.data.list[0].id
              },
              success: function (res) {
                // 请求成功后关闭Loading
                // wx.hideLoading();
                if(res.data.errno==200){
                  me.setData({
                    bookArray: res.data.data.data,
                  })
                  wx.request({
                    url: app.globalData.apiUrl + 'Trainings/chapter',
                    method: 'POST',
                    data: {
                      token: wx.getStorageSync('token'),
                      bookId: res.data.data.data[0].id
                    },
                    success: function (res) {
                      // 请求成功后关闭Loading
                      wx.hideLoading();
                      if(res.data.errno==200){
                        me.setData({
                          unitArraySecond: res.data.data.data,
                        })
                      }else{
                        wx.showModal({
                          title: '提示',
                          showCancel: false,
                          content: res.data.msg
                        })
                      }
                    }
                  })
                }else{
                  wx.showModal({
                    title: '提示',
                    showCancel: false,
                    content: res.data.msg
                  })
                }
              }
            })
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })
    }
  },
  // 第一组动态详细设置
  firstDChange: function (e) {
    this.setData({
      firstDIndex: e.detail.value
    })
  },
  // 第一组静态详细设置
  firstJChange: function (e) {
    this.setData({
      firstJIndex: e.detail.value
    })
  },
  // 第二组动态详细设置
  secondDChange: function (e) {
    this.setData({
      secondDIndex: e.detail.value
    })
  },
  // 第二组静态详细设置
  secondJChange: function (e) {
    this.setData({
      secondJIndex: e.detail.value
    })
  },
  // 第一组单词训练设置
  firstWordSet: function () {
    console.log('第一组单词训练设置')
    this.setData({
      wordSetShow: true,
      wordType: 'first'
    })
  },
  // 第二组单词训练设置
  secondWordSet: function () {
    console.log('第二组单词训练设置')
    this.setData({
      wordSetShow: true,
      wordType: 'second'
    })
  },
  selectWords(e) {
    console.log(e.target.dataset.number)
    if(this.data.wordType=='first'){
      this.setData({
        wordNumber: e.target.dataset.number
      })
    }else{
      this.setData({
        wordNumberSecond: e.target.dataset.number
      })
    }
  },
  // 关闭单词训练设置弹窗
  close: function () {
    this.setData({
      wordSetShow: false
    })
  },
  // 教材选择
  teachChange(e) {
    console.log('教材：'+e.detail.value+this.data.teachArray[e.detail.value])
    if(this.data.wordType=='first'){
      this.setData({
        teachIndex: e.detail.value
      })
    }else{
      this.setData({
        teachIndexSecond: e.detail.value
      })
    }
  },
  // 课本选择
  bookChange(e) {
    console.log('章节：'+e.detail.value+this.data.bookArray[e.detail.value])
    if(this.data.wordType=='first'){
      this.setData({
        bookIndex: e.detail.value
      })
      let me = this;
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'Trainings/chapter',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
          bookId: me.data.bookArray[e.detail.value].id
        },
        success: function (res) {
          // 请求成功后关闭Loading
          wx.hideLoading();
          if(res.data.errno==200){
            me.setData({
              unitArray: res.data.data.data,
            })
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })
    }else{
      this.setData({
        bookIndexSecond: e.detail.value
      })
      let me = this;
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'Trainings/chapter',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
          bookId: me.data.bookArray[e.detail.value].id
        },
        success: function (res) {
          // 请求成功后关闭Loading
          wx.hideLoading();
          if(res.data.errno==200){
            me.setData({
              unitArraySecond: res.data.data.data,
            })
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })
    }
  },
  // 章节选择
  unitChange(e) {
    if(this.data.wordType=='first'){
      this.setData({
        unitIndex: e.detail.value
      })
    }else{
      this.setData({
        unitIndexSecond: e.detail.value
      })
    }
  },
  sure() {
    if(this.data.wordType=='first'){
      console.log(this.data.wordNumber)
      console.log(this.data.teachIndex)
      console.log(this.data.bookIndex)
      console.log(this.data.unitIndex)
      this.setData({
        wordSetShow: false
      })
    }else{
      console.log(this.data.wordNumberSecond)
      console.log(this.data.teachIndexSecond)
      console.log(this.data.bookIndexSecond)
      console.log(this.data.unitIndexSecond)
      this.setData({
        wordSetShow: false
      })
    }
  },
  // 完成
  updateDone: function () {
    let me = this;
    let trainingTimeFirst = '',
        wordNumberFirst = '',
        publisherIdFirst = '',
        bookIdFirst = '',
        chaptersIdFirst = ''
    if(parseInt(me.data.firstTrainIndex)+1 == 2){//训练类型 1：单词训练； 2：动态训练； 3：静态训练。
      trainingTimeFirst = me.data.DArray[me.data.firstDIndex].time
    }else if(parseInt(me.data.firstTrainIndex)+1 == 3){
      trainingTimeFirst = me.data.JArray[me.data.firstJIndex].time
    }else{
      trainingTimeFirst = ''
      wordNumberFirst = me.data.wordNumber,
      publisherIdFirst = me.data.teachArray[me.data.teachIndex].id,
      bookIdFirst = me.data.bookArray[me.data.bookIndex].id,
      chaptersIdFirst = me.data.unitArray[me.data.unitIndex].id
    }

    let trainingTimeSecond = '',
        wordNumberSecond = '',
        publisherIdSecond = '',
        bookIdSecond = '',
        chaptersIdSecond = ''
    if(parseInt(me.data.secondTrainIndex)+1 == 2){//训练类型 1：单词训练； 2：动态训练； 3：静态训练。
      trainingTimeSecond = me.data.DArray[me.data.secondDIndex].time
    }else if(parseInt(me.data.secondTrainIndex)+1 == 3){
      trainingTimeSecond = me.data.JArray[me.data.secondJIndex].time
    }else{
      trainingTimeSecond = ''
      wordNumberSecond = me.data.wordNumberSecond,
      publisherIdSecond = me.data.teachArray[me.data.teachIndexSecond].id,
      bookIdSecond = me.data.bookArray[me.data.bookIndexSecond].id,
      chaptersIdSecond = me.data.unitArraySecond[me.data.unitIndexSecond].id
    }

    let trainingData = [
      {
        trainingType: parseInt(me.data.firstTrainIndex)+1,//训练类型 1：单词训练； 2：动态训练； 3：静态训练。
        trainingTime: trainingTimeFirst,//训练时长；
        wordNumber: wordNumberFirst,//训练的单词数量；
        publisherId: publisherIdFirst,//出版社
        bookId: bookIdFirst,//课本
        chaptersId: chaptersIdFirst,//章节
      },
      {
        trainingType: parseInt(me.data.secondTrainIndex)+1,//训练类型 1：单词训练； 2：动态训练； 3：静态训练。
        trainingTime: trainingTimeSecond,//训练时长；
        wordNumber: wordNumberSecond,//训练的单词数量；
        publisherId: publisherIdSecond,//出版社
        bookId: bookIdSecond,//课本
        chaptersId: chaptersIdSecond,//章节
      },
    ]
    console.log(trainingData)
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'Trainings/edit',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        trainingData: trainingData
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          // 跳转到训练计划
          wx.redirectTo({
            url: '/pages/plan/plan'
          })
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  }
})
