const tools = require('../../utils/tools.js');
const app = require('../../utils/util.js');
Page({
  data: {
    showNull: false,
    dataArr: [],
    sessionKey: '',
  },
  startAnswer(e) {
    console.log(e);
    let id = e.currentTarget.dataset.idx;
    let paperId = e.currentTarget.dataset.paperid;
    let answersId = e.currentTarget.dataset.answersid;
    console.log(answersId,"9999999")
    if (answersId) {
      this.toPath(id, paperId, answersId);
    } else {
      this.beginKao(id, paperId, this.data.sessionKey);
    }
  },
  toPath(id, paperId, answersId) {
    wx.navigateTo({
      url: '/pages/answerAfterclasses/answerAfterclasses?id=' + id + '&paperId=' + paperId + '&answersId=' + answersId,
    })
  },
  beginKao(id, paperId, sessionKey) {
    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.toPath(id, paperId, res.data.answersId);
        }
      }
    })
  },
  lookAnswer(e) {
    let answersId = e.currentTarget.dataset.answersid;
    wx.request({
      url: app.globalData.publicUrl + '/wx/answer/detail',
      method: "post",
      data: {
        sessionKey: this.data.sessionKey,
        answersId: answersId
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list[0].answerScore >= 0 && res.data.list[0].answerScore != null) {
            wx.navigateTo({
              url: '/pages/grade/grade?answersId=' + answersId,
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '当前老师批阅中,请耐心等待',
              showCancel: false
            })
          }
        }
      }
    })
  },
  getBanner(sessionKey, allDone) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/list',
      method: "post",
      data: {
        sessionKey,
        allDone
      },
      success: (res) => {
        console.log(res,"ssdsdsads");
        if (res.data.code == 0 && res.data.list.length != 0) {
          this.setData({
            dataArr: res.data.list
          })
        } else {
          this.setData({
            showNull: true,
          })
        }
      }
    })
  },
  // 我的课程
  getMycourse(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/selectBySessionKey',
      method: "post",
      data: {
        sessionKey: sessionKey
      },
      success: (res) => {
        console.log(res)
        this.getBanner(sessionKey, res.data.allDone)
      }
    })
  },
  isClass(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selMyClas',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            classId: res.data.data.id
          })
          this.getMycourse(sessionKey)
        } else {
          if (res.data.msg == "运行时异常:学员不可同时存在两个班级中。") {
            wx.showModal({
              content: "学员不可同时存在多个班级中哦",
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }
              }
            })
            return false;
          } else {
            wx.showModal({
              content: "您当前还没有正在开放的班级！",
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }
              }
            })
            return false;
          }
        }
      }
    })
  },
  async onShow() {
    const sessionKey = await tools.checkSessionAndLogin();
    this.setData({
      sessionKey: sessionKey
    })
    
    this.isClass(sessionKey);
  },
})