// pages/AIWarningList/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoShow:false,
    videoWarningArray:[],
    loadShow:false,
    loginName:'',
    userName:'',
    deptId:'',
    projectName:'',
    projectId:'',
    pageNo:0,
    pageSize:10,
    pageFlag:true,
    videoStreamUrl:''
  },

  onClickShow() {
    this.setData({ loadShow: true });
  },

  onClickHide() {
    this.setData({ loadShow: false });
  },

  //查看视频打开弹窗
  viewVideo(e){
    let token = e.currentTarget.dataset.token;
    this.setData({ videoShow: true });
    this.getVideoRtmp(token);
  },
  //查看视频关闭弹窗
  onViewVideoClose(){
    this.setData({ videoShow: false });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //加载蒙版
    that.onClickShow();
    //获取缓存数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        console.log(res.data);
        that.setData({
          loginName:res.data.loginName,
          userName:res.data.userName,
          deptId:res.data.deptId,
          projectName: app.globalData.projectName,
          projectId:app.globalData.projectId,
        })  
        that.getVideoAlert(0);
      }
    }) 
  },

/**
   * AI视频预警
   */
  getVideoAlert:function(startIndex){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/video/getVideoAlert',
      method: 'get',
      data: {
        deptId:this.data.deptId,
        projectId:this.data.projectId,
        startIndex:startIndex,
        pageSize:this.data.pageSize
      },
      success: resData => {
        this.onClickHide();
        let videoWarningArray = this.data.videoWarningArray;
        for(let i =0;i<resData.data.length;i++){
          videoWarningArray.push(resData.data[i])
        }
        this.setData({
          pageFlag:resData.data.length < this.data.pageSize?false:true,
          videoWarningArray:videoWarningArray
        })
      }
    })
  },

  /**
   * 获取视频流
   */
  getVideoRtmp:function(token){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/video/getVideoRtmp',
      method: 'get',
      data: {
        token:token
      },
      success: resData => {
        if(resData.data.code == "200"){
          let data = JSON.parse(resData.data.data);     
          this.setData({
            videoStreamUrl:data.url
          })
        }else{
          app.toast("播放失败");
          return;
        }
      }
    })
  },

  /**
   * 返回AI视频页面
   */
  goGCLB:function(){
    wx.redirectTo({
      url: '../AIvideoAnalysis/index'
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.onClickShow();
    this.setData({
      pageNo:this.data.pageNo+10
    })
    if(this.data.pageFlag){
      this.getVideoAlert(this.data.pageNo);
    }
  },

})