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

  /**
   * 页面的初始数据
   */
  data: {
    AIvideoSurvey: {},
    show: false,
    loadShow:false,
    loginName:'',
    userName:'',
    deptId:'',
    projectName:'',
    projectId:'',
    ycCount:0,
    jrCount:0,
    yjCount:0,
    videoWarningArray:[],
    videoShow:false,
    videoStreamUrl:"",

    initData:{}
  },

  //项目切换 返回值
  onProjectSelect(e){
    this.onClickShow();
    let projectId = e.detail.id;
    let projectName = e.detail.text;
    app.globalData.projectId = projectId;
    app.globalData.projectName = projectName;
    this.setData({
      projectId:projectId,
      projectName:projectName
    })
    this.onLoad();
  },

  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  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,
          initData:{text:app.globalData.projectName,id:app.globalData.projectId}
        })  
        that.getVideoAnalysis();
        that.getVideoAlert();
      }
    }) 
  },

  /**
   * ai视频分析
   */
  getVideoAnalysis:function(){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/video/getVideoAnalysis',
      method: 'get',
      data: {
        deptId:this.data.deptId,
        projectId:this.data.projectId
      },
      success: resData => {
          this.onClickHide();
          let value = resData.data.jrCount == 0?0:(resData.data.ycCount/resData.data.jrCount*100).toFixed(0);
          let AIvideoSurvey = {
            value:value>100?100:value,
            text: '异常接入率'
          }
          this.setData({
            jrCount:resData.data.jrCount,
            ycCount:resData.data.ycCount,
            yjCount:resData.data.yjCount,
            AIvideoSurvey:AIvideoSurvey
          })
      }
    })
  },

  /**
   * AI视频预警
   */
  getVideoAlert:function(){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/video/getVideoAlert',
      method: 'get',
      data: {
        deptId:this.data.deptId,
        projectId:this.data.projectId,
        dataNum:3
      },
      success: resData => {
          this.onClickHide();
          this.setData({
            videoWarningArray:resData.data
          })
      }
    })
  },

  /**
   * 获取视频流
   */
  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);  
          console.log(data)   
          this.setData({
            videoStreamUrl:data.url
          })
        }else{
          app.toast("播放失败");
          return;
        }
      }
    })
  },

  /**
   * 视频页面跳转
   */
  videoPageJump(e){
    let index = e.currentTarget.dataset.index
    if(index == 1){
      wx.redirectTo({
        url: '../xiangmushipin/xiangmushipin',
      })
    }else if(index == 3){
      wx.redirectTo({
        url: '../vehicleManage/index',
      })
    }
  },

  /**
   * 返回到更多功能页面
   */
  goGCLB:function(){
    wx.redirectTo({
      url: '../../pages/gengduogongneng/gengduogongneng'
    })
  },

  /**
   * 跳转ai视频预警列表
  */
  goList:function(){
    wx.redirectTo({
      url: '../AIWarningList/index',
    })
  }

})