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

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: app.globalData.baseImgUrl,
    isClass:true,
    starList: [
      {
        url: '../../assets/star-2@2x.png',
        activeUrl: '',
      },
      {
        url: '../../assets/star-2@2x.png',
        activeUrl: '',
      },
      {
        url: '../../assets/star-2@2x.png',
        activeUrl: '',
      },
      {
        url: '../../assets/star-2@2x.png',
        activeUrl: '',
      },
      {
        url: '../../assets/star-2@2x.png',
        activeUrl: '',
      }
    ],
    tagList:[
      
    ],
    wordList:[],
    normalSrc: '../../image/min_star_gay.png',
    selectedSrc: '../../image/min_star_red.png',
    halfSrc: '../../image/min_star_red.png',
    key: 0,//评分
    num: '',
    content: ''
  },

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

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  },
  
  // 批改老师提交
  checkTeacher: function () {
    const postData = { fn: this.submitAssessment, param: { ...arguments } }
    const { lever, lableList, isJudge ,teacherId, homeworkId } = this.data;
    console.log(lever, lableList, isJudge, teacherId, homeworkId)
    if (isJudge == 1) {
      wx.showToast({
        title: '已评价,请勿重复评价',
        icon: 'none'
      });
      return
    }
    if (!lever) {
      wx.showToast({
        title: '请选择星级',
        icon: 'none'
      })
      return
    }
    if (!lableList) {
      wx.showToast({
        title: '请选择评语',
        icon: 'none'
      })
      return
    }
    app.post('/api/parents/parents/V2/assessCorrectTeacher', {
      teacherId,
      homeworkId,
      level:lever,
      lableList
    }, 0, postData).then(res => {
      wx.showToast({
        title: '评价成功',
        icon: 'none',
        duration: 1000
      });
      setTimeout((function callback() {
        wx.navigateBack({});
      }).bind(this), 2000);

    })
  },
  /**
     * 查看评价
     */
    seeEvaluate:function(){
      const postData = { fn: this.seeEvaluate, param: { ...arguments } }
      const { teacherId, homeworkId } = this.data;
      app.post('/api/parents/parents/V2/getCorrectTeacherAssessment', {
        teacherId,
        homeworkId
      }, 0, postData).then(res => {
        const { labelList, isJudge, judgeLever } = res.data;
        var tagList=[];
        for (var i in labelList){
          if (judgeLever == labelList[i].lever){
             tagList.push(labelList[i])
          }
        }
        this.setData({ labelList, isJudge, lever:judgeLever, tagList})
      })
    },

  //点击整颗星
  selectRight: function (e) {
    const postData = { fn: this.selectRight, param: { ...arguments } }
    const { key}  = e.currentTarget.dataset;
    const { courseId, isClass, teacherId, homeworkId} = this.data;
    console.log(isClass)
    this.setData({ lever: key});
    var url = !isClass ? '/api/parents/parents/getCourseAssessment' : '/api/parents/parents/V1/getCorrectTeacherAssessment';
    var str = !isClass ? { courseId } : { teacherId, homeworkId };
    app.post(url, str, 0, postData).then(res => {
      const { labelList, isJudge} = res.data;
      
      var tagList=[];
      for (var i in labelList){
        if (key == labelList[i].lever){
          tagList.push(labelList[i].label)
        }
      }
      
      this.setData({ tagList, isJudge });
    })
  },
  // 选择标签
  clickTag:function(e){
    const { tagList}=this.data;
    const {index}=e.target.dataset;
    var temp={};
    temp[`tagList[0][${index}].status`] = tagList[0][index].status == 'active' ? "" : 'active';
    this.setData(temp);
    this.tagList();
  },
  // 标签内容
  tagList:function(){
    const { tagList } = this.data;
    const lableList = [];
    for (var i in tagList[0]) {
      if (tagList[0][i].status == "active") {
        lableList.push(tagList[0][i].labelName);
      }
    }
    this.setData({ lableList});  
  },
  // 提交
  submitAssessment:function(){
    const postData = { fn: this.submitAssessment, param: { ...arguments } }
    const { lever, lableList, courseId, isJudge}=this.data;
    if (isJudge == 1) {
      wx.showToast({
        title: '已评价,请勿重复评价',
        icon: 'none'
      });
      return
    }
    if (!lever) {
      wx.showToast({
        title: '请选择星级',
        icon: 'none'
      })
      return
    }
    if (!lableList) {
      wx.showToast({
        title: '请选择评语',
        icon: 'none'
      })
      return
    }
    app.post('/api/parents/parents/upCourseAssessment', {
       courseId,
       lever,
       lableList
       }, 0, postData).then(res => {
        wx.showToast({
          title: '评价成功',
          icon:'none',
          duration:1000
        });
        setTimeout((function callback() {
          wx.navigateBack({});
        }).bind(this), 2000);
        
    })
  }
})