const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    isRz: false,
    showMore: false, //更多
    showzs: false, //证书显示
    show: false, //协议显示
    notice: "暂无未读通知",
    classInfo: {}, //班级信息
    kcArr: [], //课程列表
    sfmArr: [], //换算时分秒数组
    studentName: "", //学生名字
    status: '', //状态
    myCanvasId: 0, //画布id
    byzsbj: app.globalData.imgUrl+"/images/zsbj.jpg", //结业证背景  用于合成图片
    IngCourseArr: [], //如果有正在进行的课就存一下
    istask: false, //判断班级群是否添加
    istask1: false, //学员登记表是否完成
    istask2: false, //课程是否完成
    istask3: false, //期末考试是否完成
    istask4: false, //综合反馈是否完成
    currentId: null,
    currentName: "",
    tva: 32,
    noticeVis: false,
    defAvatarm:app.globalData.imgUrl+ "/images/mm.jpg",
    defAvatarf: app.globalData.imgUrl+"/images/fm.jpg",
    gender_icon_m:app.globalData.imgUrl+ "/images/g_m.png",
    gender_icon_f: app.globalData.imgUrl+"/images/g_f.png",
    cardVis: false,
    stuItem: null,
    pageNum: 1,
    pageSize: 20,
    stuInfo: {},
    jobArr: [],
    classVis: false,
    classErrVis: false,
    classNVis: false,
    scheduleList: [],
    icon_school:app.globalData.imgUrl+"/images/icon_school.png",
    icon_doc:app.globalData.imgUrl+"/images/icon_doc.png",
    icon_stu:"/images/icon_stu.png"

  },
  // 查询我是否认证
  isRz(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selStudentSessionKey',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {

        if (res.data.code == 0 && (res.data.data !== null || res.data.data !== "")) {
          this.setData({
            isRz: false
          })
          this.isClass(sessionKey)
        } else {
          this.setData({
            isRz: true
          })
        }
      }
    })
  },
  // 取消认证
  escRz() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 确认认证
  goRz() {
    wx.navigateTo({
      url: '/pages/Identity/Identity',
    })
  },
  // 查询我是否有班&&班级名字
  isClass(sessionKey) {
    var that = this;
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selMyClas',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {

        if (res.data.code == 0) {
          let classInfo = res.data.data;
          classInfo.clasBegin = app.timeFmt(classInfo.clasBegin, "yyyy\/MM\/dd hh:mm ");
          classInfo.clasEnd = app.timeFmt(classInfo.clasEnd, "yyyy\/MM\/dd hh:mm ");
          console.log(classInfo);
          // classInfo.clasBegin=app.timeFilter(classInfo.clasBegin);
          // classInfo.clasEnd=app.timeFilter(classInfo.clasEnd);
          //console.log("classInfo.clasBegin:",app.timeFilter(classInfo.clasBegin));
          that.setData({
            classInfo: classInfo,
            classVis: true
          })


          this.isAgree(sessionKey);
          this.getMynotice(sessionKey);
          // this.getMycourse(sessionKey);
          // this.checkTaskList(sessionKey);
          // this.checkTaskList3(sessionKey, res.data.data.id);
          // this.checkTaskList4(sessionKey, res.data.data.id);
        } else {
          if (res.data.msg == "运行时异常:学员不可同时存在两个班级中。") {
            that.setData({ classErrVis: true });
            // wx.showModal({
            //   content: "学员不可同时存在多个班级中哦",
            //   showCancel: false,
            //   success(res) {
            //     if (res.confirm) {
            //       wx.switchTab({
            //         url: '../index/index'
            //       })
            //     }
            //   }
            // })
            return false;
          } else {


            that.setData({ classNVis: true });


            wx.request({
              url: app.globalData.publicUrl + '/wx/student/selClasToo',
              method: "post",
              data: {
                sessionKey,
                isActive: 0
              },
              success: (res) => {
                console.log(res);
                let list = res.data.data.map(item => {
                  item.clasBegin = app.timeFmt(item.clasBegin, "yyyy\/MM\/dd hh:mm ");
                  item.clasEnd = app.timeFmt(item.clasEnd, "yyyy\/MM\/dd hh:mm ");
                  return item;
                })
                that.setData({ scheduleList: list })


              }
            })
            // wx.showModal({
            //   content: "您当前还没有正在开放的班级！",
            //   showCancel: false,
            //   success(res) {
            //     if (res.confirm) {
            //       wx.switchTab({
            //         url: '../index/index'
            //       })
            //     }
            //   }
            // })
            return false;
          }
        }
      }
    })
  },

  // 协议是否已同意  
  isAgree(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selStudentSessionKey',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.code == 0 && res.data.data) {
          if (res.data.data.agreement == 0) {
            this.setData({
              studentName: res.data.data.studentName,
              show: false
            });
          } else {
            this.setData({
              studentName: res.data.data.studentName,
              show: true
            });
          }
        }
      }
    })
  },
  // 点击同意协议 并且记一下
  hidePopup() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/verification',
      method: "post",
      data: {
        sessionKey: this.data.sessionKey
      },
      success: (res) => {
        this.setData({
          show: false
        });
      }
    })
  },
  moreThink() {
    wx.switchTab({
      url: '../index/index'
    })
  },
  // 检查班级群是否加了 && 学员登记任务check
  checkTaskList(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selStudentSessionKey',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.code == 0) {
          console.log(res)
          this.setData({
            studentName: res.data.data.studentName,
          })
          if (res.data.data.joinClas == 0) {
            this.setData({
              istask: true
            })
          }
          if (res.data.data.register == 0) {
            this.setData({
              istask1: true
            })
          }
        }
      }
    })
  },
  // 期末考试任务检查
  checkTaskList3(sessionKey, id) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/clasExamstatus',
      method: "post",
      data: {
        sessionKey: sessionKey,
        clasId: id
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 0 && res.data.status == 1) {
          this.setData({
            istask3: true
          })
        }
      }
    })
  },
  // 综合反馈任务check
  checkTaskList4(sessionKey, id) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/status',
      method: "post",
      data: {
        sessionKey: sessionKey,
        clasId: id
      },
      success: (res) => {
        if (res.data.code == 0 && res.data.status == 1) {
          this.setData({
            istask4: true
          })
        }
      }
    })
  },
  // 页面上显示的我的未读通知
  getMynotice(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/eduNoticeSee/list',
      method: "post",
      data: {
        sessionKey: sessionKey,
        isSee: 1
        // isNotice: 0
      },
      fail: () => {
        this.setData({
          noticeVis: false
        })
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list && res.data.list.length !== 0) {
            this.setData({
              notice: res.data.list[0].noticeContent,
              noticeVis: true
            });
          } else {
            this.setData({
              noticeVis: false
            });
          }

        } else {
          this.setData({
            noticeVis: false
          });
        }
      }
    })
  },
  // 跳转通知页
  gomyNotice() {
    wx.navigateTo({
      url: '../myNotice/myNotice'
    })
  },
  // 学员登记表
  goWrite() {
    if (this.data.classInfo.id == -1) {
      wx.navigateTo({
        url: '../studentRegistration/studentRegistration'
      })
    } else {
      if (!this.data.istask) {
        wx.showModal({
          showCancel: false,
          content: "请先去扫描二维码加入班级群吧",
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../myMission/myMission'
              })
            }
          }
        })
        return false
      }
      wx.navigateTo({
        url: '../studentRegistration/studentRegistration'
      })
    }
  },
  // 学院介绍
  toAdd1() {
    wx.navigateTo({
      url: '../schoolIntrouce/schoolIntrouce'
    })
  },
  // 学员名单
  toAdd2() {
    wx.navigateTo({
      url: '../studentList/studentList?id=' + this.data.classInfo.id
    })
  },
  // 项目组名单
  toAdd3() {
    wx.navigateTo({
      url: '../groupsList/groupsList'
    })
  },
  // 我的课程
  getMycourse(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/selectBySessionKey',
      method: "post",
      data: {
        sessionKey: sessionKey
      },
      success: (res) => {
        console.log(res, "我的课程")
        this.setData({
          kcArr: res.data.list,
          istask2: res.data.allDone,
          currentName: "",
          currentId: null
        });
        for (let i = 0; i < res.data.list.length; i++) {
          if (res.data.list[i].isLive == -1) {
            // 这个节课未全部完成
            if (res.data.list[i].isDone == "0") {
              // 这个视频已经进行了 看一半或者看完了
              if (res.data.list[i].isStudyDone == 0 || res.data.list[i].isStudyDone == 1) {
                this.setData({
                  currentName: res.data.list[i].courseName,
                  currentId: res.data.list[i].id
                })
              }
            }
          }
        }
        let result = [];
        if (res.data.list)
          for (let i = 0; i < res.data.list.length; i++) {
            result.push({
              courseTime: res.data.list[i].courseTime
            });
          }
        const timesArr = result.map((item) => {
          return item.courseTime;
        });
        let sfmArr = [];
        for (let i = 0; i < timesArr.length; i++) {
          let value = timesArr[i];
          const resultValue = this.handler(value);
          sfmArr.push({
            courseTime: resultValue
          })
          this.setData({
            sfmArr: sfmArr
          })
        }
      }
    })
  },
  // 时间换算
  handler(value) {
    //换算为秒 进制1000
    const second = Math.floor(value / 1000); //判断是否够1秒
    if (second < 1) {
      return "0秒";
    }
    const secondValue = second % 60; // 秒
    const minutes = Math.floor(second / 60); //判断是否够1分钟
    if (minutes < 1) {
      return `${secondValue}秒`;
    }
    const minutesValue = minutes % 60; //分
    const hours = Math.floor(minutes / 60); //判断是否够1小时
    if (hours < 1) {
      return `${minutesValue}分${secondValue}秒`;
    }
    const hoursValue = hours % 60; //小时
    return `${hoursValue}时${minutesValue}分${secondValue}秒`;
  },
  // 查看更多列表展开
  listToggle: function () {
    this.setData({
      showMore: !this.data.showMore
    })
  },
  // 去学习
  goStudy(e) {
    let id = e.currentTarget.dataset.item.id //此课程的id
    let courseUrl = e.currentTarget.dataset.item.courseUrl //此课程的id
    let courseName = e.currentTarget.dataset.item.courseName //此课程的id
    let courseInfo = e.currentTarget.dataset.item.courseInfo //此课程的id
    if (this.data.classInfo.id == -1) {
      if (e.currentTarget.dataset.item.isLive == 1) {
        if (e.currentTarget.dataset.item.isActive == 1) {
          wx.navigateTo({
            url: '../zbDetails/zbDetails?id=' + id
          })
        } else if (e.currentTarget.dataset.item.isActive == -1) {
          if (courseUrl == null || courseUrl == "") {
            wx.showModal({
              showCancel: false,
              content: "直播回放还未上传噢"
            })
          } else {
            wx.navigateTo({
              url: '../livePlayback/livePlayback?courseName=' + courseName + '&courseUrl=' + courseUrl + '&courseInfo=' + courseInfo
            })
          }
        } else {
          wx.showModal({
            showCancel: false,
            content: "直播还未开始哦"
          })
          return false;
        }
      } else {
        wx.navigateTo({
          url: '../dbDetails/dbDetails?id=' + id
        })
      }
    } else {
      console.log(this.data.istask)
      if (!this.data.istask) {
        wx.showModal({
          showCancel: false,
          content: "请先去扫描二维码加入班级群吧",
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../myMission/myMission'
              })
            }
          }
        })
        return false
      }
      if (!this.data.istask1) {
        wx.showModal({
          showCancel: false,
          content: "请先填写学员登记表再来学习哦！"
        })
        return false
      }
      // 直播
      if (e.currentTarget.dataset.item.isLive == 1) {
        if (e.currentTarget.dataset.item.isActive == 1) {
          wx.navigateTo({
            url: '../zbDetails/zbDetails?id=' + id
          })
        } else if (e.currentTarget.dataset.item.isActive == -1) {
          if (courseUrl == null || courseUrl == "") {
            wx.showModal({
              showCancel: false,
              content: "直播回放还未上传噢"
            })
          } else {
            wx.navigateTo({
              url: '../livePlayback/livePlayback?courseName=' + courseName + '&courseUrl=' + courseUrl + '&courseInfo=' + courseInfo
            })
          }
        } else {
          wx.showModal({
            showCancel: false,
            content: "直播还未开始哦"
          })
          return false;
        }
      } else {
        if (this.data.currentId != null) {
          // 没学或者没学完
          if (e.currentTarget.dataset.item.isDone == 0) {
            if (id == this.data.currentId) {
              wx.navigateTo({
                url: '../dbDetails/dbDetails?id=' + id
              })
            } else {
              // 有未完成的但不是我 就不能跳转
              wx.showModal({
                showCancel: false,
                content: "请确认您的 ‘" + this.data.currentName + "’ 这节课中的学习、考试、评价是否都已完成？"
              })
            }
          } else {
            // 我看完了这个视频 还能看
            wx.navigateTo({
              url: '../dbDetails/dbDetails?id=' + id
            })
          }
        } else {
          wx.navigateTo({
            url: '../dbDetails/dbDetails?id=' + id
          })
        }
      }
    }
  },
  // 去考试
  gokhAnswer(e) {
    console.log(e)
    wx.showLoading({
      mask: true,
      title: '加载中',
    })
    if (this.data.classInfo.id == -1) {
      this.examination(e.currentTarget.dataset.item.id);
      wx.hideLoading();
    } else {
      wx.hideLoading();
      if (!this.data.istask) {
        wx.showModal({
          showCancel: false,
          content: "请先去扫描二维码加入班级群吧",
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../myMission/myMission'
              })
            }
          }
        })
        return false
      }
      if (!this.data.istask1) {
        wx.showModal({
          showCancel: false,
          content: "请先填写学员登记表再来学习哦！"
        })
        return false
      }
      let isStudyDone = e.currentTarget.dataset.item.isStudyDone
      console.log(isStudyDone, '我是学习的')
      if (isStudyDone != 1) {
        wx.showModal({
          showCancel: false,
          content: "您当前的学习课程还未完成哦，请先完成再来考试吧！"
        })
        return false
      } else {
        this.examination(e.currentTarget.dataset.item.id);
      }
    }
  },
  // 考试方法单拿出来
  examination(id) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/examList',
      method: "post",
      data: {
        sessionKey: this.data.sessionKey,
        courseId: id,
        clasId: this.data.classInfo.id
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list.length == 0) {
            wx.showModal({
              showCancel: false,
              content: '老师还未出考试题哦'
            })
          } else {
            console.log(res, '1111')
            if (res.data.list[0]) {
              if (res.data.list[0].answersId) {
                this.toPath(res.data.list[0].id, res.data.list[0].paperId, res.data.list[0].answersId, 'answerAfterclass');
              } else {
                this.beginKao(res.data.list[0].id, res.data.list[0].paperId, this.data.sessionKey, 'answerAfterclass');
              }
            }
          }
        }
      }
    })
  },
  //去评价
  gopjClass(e) {
    if (this.data.classInfo.id == -1) {
      this.evaluate(e.currentTarget.dataset.item.id)
    } else {
      if (!this.data.istask) {
        wx.showModal({
          showCancel: false,
          content: "请先去扫描二维码加入班级群吧",
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../myMission/myMission'
              })
            }
          }
        })
        return false
      }
      if (!this.data.istask1) {
        wx.showModal({
          showCancel: false,
          content: "请先填写学员登记表再来学习哦！"
        })
        return false
      }
      let isStudyDone = e.currentTarget.dataset.item.isStudyDone
      if (isStudyDone != 1) {
        wx.showModal({
          showCancel: false,
          content: "您的学习课程还未完成哦，请先完成再来评价吧！"
        })
        return false
      } else {
        this.evaluate(e.currentTarget.dataset.item.id)
      }
    }
  },
  // 评价方法单拿出来
  evaluate(id) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/quesExamList',
      method: "post",
      data: {
        sessionKey: this.data.sessionKey,
        courseId: id,
        clasId: this.data.classInfo.id
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list.length == 0) {
            wx.showModal({
              showCancel: false,
              content: '老师还未出评价模板哦'
            })
          } else {
            if (res.data.list[0]) {
              if (res.data.list[0].answersId) {
                this.toPath(res.data.list[0].id, res.data.list[0].paperId, res.data.list[0].answersId, 'evaluationCourse');
              } else {
                this.beginKao(res.data.list[0].id, res.data.list[0].paperId, this.data.sessionKey, 'evaluationCourse');
              }
            }
          }
        }
      }
    })
  },
  // 查看详情成绩
  gockcj(e) {
    console.log(e.currentTarget.dataset.item.isExamId)
    if (e.currentTarget.dataset.item.isExamId) {
      wx.request({
        url: app.globalData.publicUrl + '/wx/answer/detail',
        method: "post",
        data: {
          answersId: e.currentTarget.dataset.item.isExamId
        },
        success: (res) => {
          if (res.data.list[0] && res.data.list[0].answerScore >= 0 && res.data.list[0].answerScore != null) {
            wx.navigateTo({
              url: '/pages/grade/grade?answersId=' + e.currentTarget.dataset.item.isExamId,
            })
          } else {
            wx.showModal({
              showCancel: false,
              content: '当前老师批阅中,请耐心等待'
            })
          }
        },
        fail: () => {
          wx.showModal({
            showCancel: false,
            content: '当前老师批阅中,请耐心等待'
          })
        }
      })
    }
  },
  // 查看详情评价
  gockpj(e) {
    wx.navigateTo({
      url: '/pages/evaluationCourseDetail/evaluationCourseDetail?answersId=' + e.currentTarget.dataset.item.isFeedbackId,
    })
  },
  // 期末考试
  goQmks() {
    if (this.data.classInfo.id == -1) {
      wx.request({
        url: app.globalData.publicUrl + '/wx/exam/examList',
        method: "post",
        data: {
          sessionKey: this.data.sessionKey,
          clasId: this.data.classInfo.id
        },
        success: (res) => {
          if (res.data.code == 0) {
            if (res.data.list.length == 0) {
              wx.showModal({
                showCancel: false,
                content: '期末考试还未开始哦！'
              })
            } else {
              if (res.data.list[0].answersId) {
                if (res.data.list[0].status == '1') {
                  wx.request({
                    url: app.globalData.publicUrl + '/wx/answer/detail',
                    method: "post",
                    data: {
                      answersId: res.data.list[0].answersId
                    },
                    success: (res1) => {
                      if (res1.data.list[0].answerScore >= 0 && res1.data.list[0].answerScore != null) {
                        wx.navigateTo({
                          url: '/pages/grade/grade?answersId=' + res.data.list[0].answersId,
                        })
                      } else {
                        wx.showModal({
                          showCancel: false,
                          content: '当前老师批阅中,请耐心等待'
                        })
                      }
                    }
                  })
                } else {
                  this.toPath(res.data.list[0].id, res.data.list[0].paperId, res.data.list[0].answersId, 'answerAfterclass');
                }
              } else {
                this.beginKao(res.data.list[0].id, res.data.list[0].paperId, this.data.sessionKey, 'answerAfterclass');
              }
            }
          }
        }
      })
    } else {
      if (!this.data.istask) {
        wx.showModal({
          showCancel: false,
          content: "请先去扫描二维码加入班级群吧",
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../myMission/myMission'
              })
            }
          }
        })
        return false
      }
      if (!this.data.istask2) {
        wx.showModal({
          showCancel: false,
          content: "您的课程任务还未全部完成哦！请先去完成再来期末考试吧！"
        })
        return false
      }
      wx.request({
        url: app.globalData.publicUrl + '/wx/exam/examList',
        method: "post",
        data: {
          sessionKey: this.data.sessionKey,
          clasId: this.data.classInfo.id
        },
        success: (res) => {
          if (res.data.code == 0) {
            if (res.data.list.length == 0) {
              wx.showModal({
                showCancel: false,
                content: '期末考试还未开始哦！'
              })
            } else {
              if (res.data.list[0].answersId) {
                if (res.data.list[0].status == '1') {
                  wx.request({
                    url: app.globalData.publicUrl + '/wx/answer/detail',
                    method: "post",
                    data: {
                      answersId: res.data.list[0].answersId
                    },
                    success: (res1) => {
                      if (res1.data.list[0].answerScore >= 0 && res1.data.list[0].answerScore != null) {
                        wx.navigateTo({
                          url: '/pages/grade/grade?answersId=' + res.data.list[0].answersId,
                        })
                      } else {
                        wx.showModal({
                          showCancel: false,
                          content: '当前老师批阅中,请耐心等待'
                        })
                      }
                    }
                  })
                } else {
                  this.toPath(res.data.list[0].id, res.data.list[0].paperId, res.data.list[0].answersId, 'answerAfterclass');
                }
              } else {
                this.beginKao(res.data.list[0].id, res.data.list[0].paperId, this.data.sessionKey, 'answerAfterclass');
              }
            }
          }
        }
      })
    }
  },
  // 综合反馈表
  goWrite2() {
    if (this.data.istask4) {
      wx.request({
        url: app.globalData.publicUrl + '/wx/exam/quesExamList',
        method: "post",
        data: {
          sessionKey: this.data.sessionKey,
          questSub: '1',
          clasId: this.data.classInfo.id
        },
        success: (res) => {
          if (res.data.code == 0) {
            if (res.data.list[0]) {
              if (res.data.list[0].answersId) {
                wx.navigateTo({
                  url: '/pages/feedBackDetail/feedBackDetail?answersId=' + res.data.list[0].answersId,
                })
              } else {
                this.beginKao(res.data.list[0].id, res.data.list[0].paperId, this.data.sessionKey, 'feedBack');
              }
            }
          }
        }
      })
    } else {
      wx.request({
        url: app.globalData.publicUrl + '/wx/exam/quesExamList',
        method: "post",
        data: {
          sessionKey: this.data.sessionKey,
          questSub: '1',
          clasId: this.data.classInfo.id
        },
        success: (res) => {
          if (res.data.code == 0) {
            if (res.data.list.length == 0) {
              wx.showModal({
                showCancel: false,
                content: '当前综合反馈还不可以填写哦！'
              })
            } else {
              if (res.data.list[0].answersId) {
                if (res.data.list[0].status == '1') {
                  wx.navigateTo({
                    url: '/pages/feedBackDetail/feedBackDetail?answersId=' + res.data.list[0].answersId,
                  })
                } else {
                  this.toPath(res.data.list[0].id, res.data.list[0].paperId, res.data.list[0].answersId, 'feedBack');
                }
              } else {
                this.beginKao(res.data.list[0].id, res.data.list[0].paperId, this.data.sessionKey, 'feedBack');
              }
            }
          }
        }
      })
    }
  },
  // 我的积分
  goMyPoints() {
    wx.navigateTo({
      url: '../myPoints/myPoints'
    })
  },
  // 领取结业证
  // lq() {
  //   if (!this.data.istask3) {
  //     wx.showModal({
  //       showCancel: false,
  //       content: "您的期末考试还未完成哦！"
  //     })
  //     return false
  //   }
  //   wx.showLoading({
  //     mask: true,
  //     title: '加载中',
  //   })
  //   this.setData({
  //     myCanvasId: this.data.myCanvasId + 1
  //   })
  //   let str = this.data.studentName + "同志："
  //   let str1 = "于" + this.data.classInfo.beginDate + "至" + this.data.classInfo.endDate + "参加学习" + this.data.classInfo.clasName + "完成规定培训内容。准予结业，特发此证。"
  //   let strArr1 = [];
  //   // let a = str1.slice(0, 22);
  //   // let b = str1.slice(22, 36);
  //   // let c = str1.slice(36, 50);
  //   // let d = str1.slice(50, 67);
  //   // strArr1.push(a, b, c, d);
  //   // const ctx = wx.createCanvasContext(this.data.myCanvasId);
  //   // ctx.drawImage(this.data.byzsbj, 0, 0, 300, 415); //里面的参数无非就是图片放置的位置即图片的横纵坐标，图片的宽高
  //   // ctx.setFillStyle("#000");
  //   // ctx.setFontSize(15); //字大小
  //   // ctx.font = 'normal bold 15px sans-serif'
  //   // ctx.setTextAlign('left'); //是否居中显示，参考点画布中线
  //   let a = str1.slice(0, 22);
  //   let b = str1.slice(22, 37);
  //   let c = str1.slice(37, 52);
  //   let d = str1.slice(52, 67);
  //   strArr1.push(a, b, c, d);
  //   const ctx = wx.createCanvasContext(this.data.myCanvasId);
  //   ctx.drawImage(this.data.byzsbj, 0, 0, 300, 415); //里面的参数无非就是图片放置的位置即图片的横纵坐标，图片的宽高
  //   ctx.setFillStyle("#000");
  //   ctx.setFontSize(15); //字大小
  //   ctx.font = 'normal bold 15px sans-serif'
  //   ctx.setTextAlign('left'); //是否居中显示，参考点画布中线
  //   let leftPositionAarr = [60, 30, 30, 30]
  //   ctx.fillText(str, 30, 160);
  //   for (let i = 0; i < strArr1.length; i++) {
  //     ctx.fillText(strArr1[i], leftPositionAarr[i], 190 + i * 30);
  //   }
  //   ctx.draw();
  //   wx.hideLoading();
  //   this.setData({
  //     showzs: true
  //   });
  //   wx.request({
  //     url: app.globalData.publicUrl + '/wx/student/wxGraduation',
  //     method: "post",
  //     data: {
  //       sessionKey: this.data.sessionKey
  //     },
  //     success: (res) => {
  //       console.log(res.data, "领取结业证提交")
  //     }
  //   })
  // },





  //孙喆 2020-9-22 10:38最后更改
  lq() {
    var that = this;
    if (!this.data.istask3) {
      wx.showModal({
        showCancel: false,
        content: "您的期末考试还未完成哦！"
      })
      return false
    }
    wx.showLoading({
      mask: true,
      title: '加载中',
    })
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/wxGraduation',
      method: "post",
      data: {
        sessionKey: this.data.sessionKey
      },
      success: (res) => {
        console.log(res);
        console.log(res.data, "领取结业证提交")

        wx.hideLoading();
        if (res.data.code == 500) {
          wx.showModal({
            showCancel: false,
            content: "领取失败，请稍后再试"
          })
        } else {
          that.setData({
            myCanvasId: that.data.myCanvasId + 1
          })
          let str = that.data.studentName + " 同志于" + app.dateFilter(that.data.classInfo.beginDate) + "-" + app.dateFilter(that.data.classInfo.endDate) + "参加" + that.data.classInfo.clasName + ",完成规定培训内容。准予结业，特发此证。"
          //汉字占两格，英文数字占一格
          let re = /^[\u4e00-\u9fa5]$/;
          let stra = str.split("");
          let tva = 30;  //阈值
          let strTar = [];
          let num = 0;
          let st = "";
          for (let i = 0; i < str.length; i++) {
            let s = stra.shift()
            st = st + s;
            num += re.test(s) ? 2 : 1;
            tva = strTar.length < 1 ? that.data.tva - 4 : that.data.tva;
            if (num >= tva || i == str.length - 1) {
              strTar.push(st);
              num = 0;
              st = "";
            }
          }
          const ctx = wx.createCanvasContext(that.data.myCanvasId);
          ctx.drawImage(that.data.byzsbj, 0, 0, 300, 415); //里面的参数无非就是图片放置的位置即图片的横纵坐标，图片的宽高
          ctx.setFillStyle("#000");
          ctx.setFontSize(14); //字大小
          ctx.font = 'normal bold 14px sans-serif'
          ctx.setTextAlign('left'); //是否居中显示，参考点画布中线
          let leftPositionAarr = [59, 30]
          let graduationNum = res.data.data.graduationNum != "" ? "编号：" + res.data.data.graduationNum : "";
          ctx.fillText(graduationNum, 130, 150);
          let topPt = 190 - (strTar.length - 4) * 5;
          let lineHeight = 30 - (strTar.length > 4 ? strTar.length > 6 ? 10 : (strTar.length - 4) * 5 : 0)
          for (let i = 0; i < strTar.length; i++) {

            ctx.fillText(strTar[i], i == 0 ? leftPositionAarr[0] : leftPositionAarr[1], topPt + i * lineHeight);
          }
          ctx.draw();
          that.setData({
            showzs: true
          });
        }

      },
      fail: () => {
        wx.hideLoading();
        wx.showModal({
          showCancel: false,
          content: "网络错误，请稍后再试"
        })
      }
    })
  },



  // 存储结业证
  saveZs() {
    wx.canvasToTempFilePath({ //把当前画布指定区域的内容导出生成指定大小的图片
      canvasId: this.data.myCanvasId,
      success(res) {
        wx.authorize({ //向用户发起授权请求
          scope: 'scope.writePhotosAlbum', //保存相册授权
          success: () => {
            wx.saveImageToPhotosAlbum({ //保存图片到系统相册
              filePath: res.tempFilePath,
              success: () => {
                wx.showToast({
                  title: '图片保存成功'
                })
              }
            })
          }
        })
      }
    })
  },
  // 证书隐藏
  onClose1() {
    this.setData({
      showzs: false
    });
  },
  toPath(id, paperId, answersId, address, status) {
    wx.navigateTo({
      url: '/pages/' + address + '/' + address + '?id=' + id + '&paperId=' + paperId + '&answersId=' + answersId + '&status=' + status,
    })
  },
  beginKao(id, paperId, sessionKey, address) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/begin',
      method: "post",
      data: {
        sessionKey: sessionKey,
        id: id,
        paperId: paperId
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            status: res.data.status
          })
          this.toPath(id, paperId, res.data.answersId, address);
        }
      }
    })
  },
  onClickShow(e) {

    this.setData({ stuItem: e.currentTarget.dataset.item });

    this.setData({ cardVis: true });
  },

  onClickHide() {
    this.setData({ cardVis: false });
  },


  onUnload() {
    this.setData({ cardVis: false,pageNum:1,stuInfo:{} });
  },
  onHide() {
    this.setData({ cardVis: false,pageNum:1,stuInfo:{} });
  },
  studentList(page) {
    var that = this;
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.publicUrl + '/wx/student/progress',
        method: "post",
        data: {
          sessionKey: that.data.sessionKey,
          pageNum: page,
          pageSize: that.data.pageSize
        },
        success: (res) => {

          if (res.data.code == 0) {
            let info = res.data.data;
 
            info.list = info.list.map(item => {
              if (item.studentHead == "" || item.studentHead == null) {
                item.avatar = item.studentSex == 1 ? that.data.defAvatarf : that.data.defAvatarm;
              } else {
                item.avatar = item.studentHead;
              }
              return item;
            })
            console.log(info);
            resolve(info)
          }

        },
        fail: (error) => {
          reject(error)
        }
      })
    })

  },
  getjob(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/position',
      method: "get",
      data: {
        sessionKey
      },
      success: (res) => {
        console.log(res, '我是获取到的数组')
        let Arr = [];
        res.data.data.forEach(item => {
          let newdata = {};
          newdata.dictLabel = item.dictLabel;
          newdata.dictValue = item.dictValue;
          Arr.push(newdata);
        });
        this.setData({
          jobArr: Arr
        })

      },
      fail: () => {
        wx.hideLoading();
      }
    })
  },
  onReachBottom() {
    console.log("下拉刷新");
    var that = this;
    if (that.data.classVis && that.data.stuInfo.list.length > 0) {
      var page = that.data.pageNum;
      page++;
      that.setData({ pageNum: page });
      wx.showLoading({
        title: '加载更多',
      })
      that.studentList(this.data.pageNum).then(res => {
        wx.hideLoading();
        
        let oldList = that.data.stuInfo.list.slice();
        let newInfo = Object.assign({}, that.data.stuInfo, res)
        console.log("that.data.pageNum",that.data.pageNum);
        console.log("res.pages:",res.pages);
        if (that.data.pageNum <= res.pages) {
          newInfo.list=oldList.concat(newInfo.list)
          that.setData({
            stuInfo:newInfo
          });
        }else{
          wx.showToast({
            title: '没有更多了',
          })
        }
        console.log(res)

      },()=>{
        wx.hideLoading();
        wx.showToast({
          title: '加载失败',
        })
      })
    }

  },
  // onReachBottom: function () {
  //   var that = this;
  //   console.log(that)
  //   var page = that.data.page
  //   page++;
  //   that.setData({
  //   page: page
  //   })
  //   // 显示加载图标
  //   wx.showLoading({
  //   title: '玩命加载中',
  //   })
  //   wx.request({
  //   url: '',   //请求的接口地址
  //   data: {
  //   page: that.data.page,    //  页数

  //   },
  //   header: {
  //   'content-type': 'application/json'
  //   },

  //   method: 'GET',
  //   success: function (res) {
  //   console.log(res)
  //   var goods = that.data.grade.concat(res.data.data.list)     //grade  为一进入页面请求完数据定义的集合
  //   if (res.data.data.list == '') {
  //   console.log(1)
  //   wx.showToast({
  //   title: '暂无更多',
  //   icon: 'none',
  //   })
  //   } else {
  //   that.setData({
  //   grade : goods,
  //   });
  //   }
  //   wx.hideLoading();

  //   },
  //   })
  //   },
  async onShow() {
    console.log(app.globalData.imgUrl);
    var that = this;
    // wx.showLoading({
    //   title: '加载中',
    //   mask:true
    // })
    const sessionKey = await tools.checkSessionAndLogin();
    this.setData({
      sessionKey
    });
    await this.isRz(sessionKey)
    that.getjob(sessionKey)
    console.log(that.data.classVis);
    this.studentList(this.data.pageNum).then((info) => {
      this.setData({ stuInfo: info });
      console.log(this.data.stuInfo)
    })


  }
})