const tools = require('../../utils/tools.js');
const app = require('../../utils/util.js');
Page({
  data: {
    active: 0,
    zbArr: [],
    showNull: false
  },
  goIn(e) {
    let id = e.currentTarget.dataset.item.id
    if (e.currentTarget.dataset.item.isActive == 1) {
      wx.navigateTo({
        url: '../zbDetails/zbDetails?id=' + id
      })
    } else {
      wx.showModal({
        showCancel: false,
        content: "直播还未开始哦"
      })
    }
  },
  getArr(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/selectLivesBySessionKey',
      method: "post",
      data: {
        sessionKey: sessionKey
      },
      success: (res) => {
        if (res.data.code == 0 && res.data.list.length != 0) {
          this.setData({
            zbArr: res.data.list
          })
        } else {
          this.setData({
            showNull: true
          })
        }
      }
    })
  },
  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.getArr(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.isClass(sessionKey);
  }
})