// pages/evaluationDetail/evaluationDetail.js
const app = getApp()
const util = require("../../utils/util")
const urlDef = require("../../utils/urls") 

const audioContext = wx.createInnerAudioContext() //音频播放对象
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tipList: [
      '人均:该学生在本班历次得分的平均值\r\n',
      '班平:该班级内多有学生得分的平均值\r\n',
      '满星:第一个数是该学生在本班级得满分的次数,第二个数是已评分的次数\r\n',
    ],
    curVideo: '',
    videoFlag: 0,
    videoContext: '',
    curAudio: '',
    curAudioIndex: -1,
    audioPause: 0,
    item: null
  },

  getTip: function () {
    var str = ''
    for (var i in this.data.tipList) {
      str += this.data.tipList[i]
    }
    wx.showModal({
      title: '调课说明',
      content: str,
      showCancel: false,//是否显示取消按钮
      cancelText: "否",//默认是“取消”
      cancelColor: '#999999',//取消文字的颜色
      confirmText: "我知道了",//默认是“确定”
      // confirmColor: 'skyblue',//确定文字的颜色
    })
  },

  bindfullscreenchange: function () {
    if (this.data.videoFlag == 0) {
      this.setData({
        videoFlag: 1
      })
    } else {
      this.setData({
        videoFlag: 0,
        curVideo: ""
      })
    }
  },

  videoShow: function (e) {
    this.setData({
      curVideo: e.currentTarget.dataset.url
    })

    this.videoContext.requestFullScreen({	// 设置全屏时视频的方向,不指定则根据宽高比自动判断。
      direction: 90						// 屏幕逆时针90度
    });
  },

  showImg: function (e) {
    // var list = e.currentTarget.dataset.url
    const index = e.currentTarget.dataset.index
    wx.previewImage({
      current: this.data.imgList[index],
      urls: this.data.imgList,
    })
  },

  showAudio: function (e) {
    const index = e.currentTarget.dataset.index
    if (this.data.curAudioIndex != index) {
      this.setData({
        curAudioIndex: index,
      })
      audioContext.src = this.data.item.records[index].url
      audioContext.play()
      audioContext.onEnded((res) => {
        this.setData({
          curAudioIndex: '-1',
        })
      })
    } else {
      if (this.data.audioPause) {
        audioContext.play()
        this.setData({
          audioPause: false
        })
      } else {
        audioContext.pause()
        this.setData({
          audioPause: true
        })
      }
    }
  },

  bindended: function () {
    this.setData({
      audioPause: 0,
      curAudioIndex: -1,
      curAudio: ''
    })
  },

  bindpause: function () {
    this.setData({
      audioPause: 1
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.videoContext = wx.createVideoContext('play-video');// 	创建 video 上下文 VideoContext 对象。
    const urls = urlDef.urls;
    let it = JSON.parse(options.item)

    if (it.imageUrl) {
      it.imageUrl = urls.oss_file + 'image/' + it.imageUrl
    } else {
      it.imageUrl = "/images/head.png"
    }

    let attachs;
    try {
      if (it.evaluateAttach) {
        attachs = JSON.parse(it.evaluateAttach);
      } else {
        attachs = {};
      }
    } catch (e) {
      console.error(JSON.stringify(e));
    }

    let stu = wx.getStorageSync('student')
    util.apiPost(urls.query_student_star + '&q.orgId=' + stu.orgId + '&q.classesId='  + it.classesId + '&q.studentId=' 
      + stu.studentId + '&q.headId=' + it.headId).then((st) => {
        it.stars = st;
        this.reloadItems(it)
      }, e => {
        console.log(e);
      });

    if (attachs) {
      it.images = attachs.images ? attachs.images : [];
      it.videos = attachs.videos ? attachs.videos : [];
      it.records = attachs.records ? attachs.records : [];

      it.images.forEach(rs => {
        rs.url = urls.oss_file + 'image/' + rs.url
      });

      it.records.forEach(rs => {
        rs.url = urls.oss_file + 'file/' + rs.url
      });

      // 加载视频封面
      it.videos.forEach(r => {
        util.apiPost(urls.video_loadInfo + r.url).then((rs) => {
          r.imgUrl = rs.img;
          r.playUrl = rs.url;
          this.reloadItems(it)
        });
      });
      this.setData({ item: it })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    audioContext.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  reloadItems: function (it) {
    this.setData({ item: it })
  },
  showImg: function (e) {
    var images = [];
    this.data.item.images.map(it => {
      images.push(it.url)
    })
    let url = e.currentTarget.dataset.url;
    console.log(url)
    wx.previewImage({
      urls: images,
      current: url
    })
  }
})