const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    lbArr: [],
    showNull: false,
    sfmArr: [],
    currentId: null,
    currentName: "",
    istask: false,
    istask1: false
  },
  // 点击按跳转
  goIn(e) {
    if(!this.data.istask){
      wx.showModal({
        showCancel: false,
        content: "您的班级二维码还未扫描哦"
      })
      return
    }
    if(!this.data.istask1){
      wx.showModal({
        showCancel: false,
        content: "您的学员登记表还未填写哦"
      })
      return
    }
    let id = e.currentTarget.dataset.item.id
    console.log(this.data.classId)
    if (this.data.classId != -1) {
      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
        })
      }
    } else {
      wx.navigateTo({
        url: '../dbDetails/dbDetails?id=' + id
      })
    }
  },
  // 录播列表
  getArr(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/selectRecordBySessionKey',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            lbArr: res.data.list,
            currentName: "",
            currentId: null
          })
          for (let i = 0; i < res.data.list.length; i++) {
            // 这个节课未全部完成
            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 = [];
          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
            })
          }
          if (this.data.lbArr.length == 0) {
            this.setData({
              showNull: true
            })
          }
        } else {
          if (this.data.lbArr.length == 0) {
            this.setData({
              showNull: true
            })
          }
        }
      },
      fail: () => {
        if (this.data.lbArr.length == 0) {
          this.setData({
            showNull: true
          })
        }
      }
    })
  },
  // 时间处理
  handler(value) {
    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}秒`;
  },
  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;
          }
        }
      }
    })
  },
  // 检查班级群是否加了 && 学员登记任务check
  checkTaskList(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selStudentSessionKey',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.data.joinClas == 0) {
            this.setData({
              istask: true
            })
          }
          if (res.data.data.register == 0) {
            this.setData({
              istask1: true
            })
          }
        }
      }
    })
  },
  async onShow() {
    const sessionKey = await tools.checkSessionAndLogin();
    this.isClass(sessionKey)
    this.checkTaskList(sessionKey)
  }
})