const tools = require('../../utils/tools.js');
const app = require('../../utils/util.js');
Page({
  data: {
    showNull: false,
    dataArr: [],
    sessionKey: '',
    classInfo: {},
  },
  startAnswer(e) {
    let id = e.currentTarget.dataset.idx;
    let paperId = e.currentTarget.dataset.paperid;
    let answersId = e.currentTarget.dataset.answersid;
    if (answersId) {
      this.toPath(id, paperId, answersId);
    } else {
      this.beginKao(id, paperId, this.data.sessionKey);
    }
  },
  toPath(id, paperId, answersId) {
    wx.navigateTo({
      url: '/pages/lastdetail/lastdetail?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) {
          wx.navigateTo({
            url: '/pages/lastdetailDetail/lastdetailDetail?answersId=' + answersId,
          })
        }
      }
    })
  },
  getExamList(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/quesExamList',
      method: "post",
      data: {
        sessionKey: sessionKey,
        questSub: '2',
        clasId: this.data.classInfo.id
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list.length == 0) {
            this.setData({
              showNull: true
            })
          } else {
            this.setData({
              dataArr: res.data.list
            })
          }
        }
      },
      fail: () => {
        this.setData({
          showNull: true
        })
      }
    })
  },
  // 查询我是否有班&&班级id
  isClass(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selMyClas',
      method: "post",
      data: {
        sessionKey: sessionKey
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            classInfo: res.data.data
          })
          this.getExamList(sessionKey);
        } else {
          wx.showModal({
            content: "您当前还没有开放的班级！",
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '../index/index'
                })
              }
            }
          })
          return false;
        }
      }
    })
  },
  async onLoad() {
    const sessionKey = await tools.checkSessionAndLogin();
    this.setData({
      sessionKey: sessionKey
    })
    this.isClass(sessionKey);
  },
})