const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    kcArr: [],
    showZhfk: true,
    istask: false
  },
  // 跳转直播点播页面
  goStudy(e) {
    console.log(e.currentTarget.dataset.item)
    let id = e.currentTarget.dataset.item.id
    let courseName = e.currentTarget.dataset.item.courseName
    let courseUrl = e.currentTarget.dataset.item.courseUrl
    let courseInfo = e.currentTarget.dataset.item.courseInfo
    console.log(id)
    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: '../videoPlayBack/videoPlayBack?id=' + id + '&courseUrl=' + courseUrl + '&courseInfo=' + courseInfo + '&courseName=' + courseName
      })
    }
  },
  // 获取列表
  getArr(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/selectByClasId',
      method: "post",
      data: {
        sessionKey,
        clasId: this.data.id
      },
      success: (res) => {
        this.setData({
          kcArr: res.data.list
        });
        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}秒`;
  },
  // 检查综合反馈是否填写完
  checkTaskList(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/status',
      method: "post",
      data: {
        sessionKey: sessionKey,
        clasId: this.data.id
      },
      success: (res) => {
        if (res.data.code == 0 && res.data.status == 1) {
          this.setData({
            istask: true
          })
        }
      }
    })
  },
  // 历史班综合反馈填写
  goWrite2() {
    if (this.data.istask) {
      wx.request({
        url: app.globalData.publicUrl + '/wx/exam/quesExamList',
        method: "post",
        data: {
          sessionKey: this.data.sessionKey,
          questSub: '1',
          clasId: this.data.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.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');
              }
            }
          }
        }
      })
    }
  },
  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, );
        }
      }
    })
  },
  async onLoad(options) {
    const sessionKey = await tools.checkSessionAndLogin();
    this.setData({
      sessionKey: sessionKey,
      id: options.id,
      name: options.name,
      clasEnd: options.clasEnd
    })
    this.getArr(sessionKey);
    this.checkTaskList(sessionKey)
    let currrentTime = new Date().getTime()
    let endTime = new Date(this.data.clasEnd).getTime()
    let day = Math.floor((currrentTime - endTime) / 86400000);
    if (day > 7) {
      this.setData({
        showZhfk: false
      })
    }
  }
})