const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    playerHidden: true,
    projectId:'',
    projectName:'' ,
    myheight:'',
    bottom:'',
    left:'',
    resinit:'',
    btnbottom:'',
    btnleft:'',
    down:'',
    show: false,
    token:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
     token:options.token
   })

    var that = this
    wx.getSystemInfo({
      success: function(res){
        that.setData({
          resinit:res.windowWidth
        })
      }
    })
    
   
    this.setData({
      btnbottom:0,
      btnleft:(this.data.resinit-60) / 2+'px'
    })
    
    console.log(this.data.resinit)
    this.setData({
      myheight: (this.data.resinit) + 'px',
      bottom: -((this.data.resinit-150) / 2 + 150) + 'px',
      left:(this.data.resinit-150) / 2+ 'px'
    })

    this.setData({
       //赋值跳转参数
      projectId:app.globalData.projectId,
      projectName:app.globalData.projectName,
    })
    // 参数检查合法性
    if(undefined == options.rtmpUrl || "" == options.rtmpUrl){
      wx.showModal({
        title: "参数错误",
        content: "1. 请检查参数 Key 值是否为 rtmpUrl \n2. 请检查参数 rtmpUrl 是否为空字符",
        showCancel: false,
        success: res => {
          wx.navigateBack({});
        }
      })
      return;
    }

    // 初始化页面数据
    this.initView(options);
  },

  /**
   * 初始化页面数据
  */
  initView:function(options){
    console.log(options.rtmpUrl);
    this.setData({
      height: wx.getSystemInfoSync().windowHeight,
      width: wx.getSystemInfoSync().windowWidth,
      playerHidden: false,
      rtmpUrl: options.rtmpUrl
    })
  },

  /**
   * 加载状态变化监听
  */
 onStatechange: function(e){

  // wx.showLoading({
  //   title: '请稍后'
  // })

  if(e.detail.code == -2301){ // 经多次自动重连失败，放弃连接

   // 隐藏加载提示
   wx.hideLoading({});

   // 弹窗提示状态
   wx.showModal({
     title:"加载失败",
     content: "经多次自动重连失败，放弃连接",
     showCancel:false,
     success: res => {
       wx.navigateBack({});
     }
   })
   return;
  }

  if(e.detail.code == 2004 || e.detail.code == 2003){ // 视频播放开始
   // 隐藏加载提示
   setTimeout(function(){
    wx.hideLoading({});
    wx.showToast({
      title: '开始播放',
      icon: "none"
    })
   }, 1500)
  }
 },

  /**
   * 返回监听
   */
  onBack: function(){
    wx.navigateBack({});
  },

  openyt() {
    if(!this.data.show){
      this.setData({
        bottom: -((this.data.resinit-150) / 2) + 'px',
        btnbottom: 150+'px',
        down:'rgba(0,0,0,0.3) url("http://fileimg.makalu.cc/WEB_BE024BE5CFF04825B96441B57BF221D5.png") no-repeat center/15px',
        show: true
      })
    }else{
      this.setData({
        bottom: -((this.data.resinit-150) / 2 + 150) + 'px',
        btnbottom:0,
        down:'rgba(0,0,0,0.3) url("http://fileimg.makalu.cc/WEB_121316FF680C47009E7768A13F3C5AAD.png") no-repeat center/15px;',
        show: false
      })
    }
    
  },

  /**
     * 控制
     */
    tripodHandle: function(e) {
      console.log(e.currentTarget.dataset.action)
      var tokenStr = this.data.token;
      console.log(this.data.token);
        wx.request({
            url: 'https://video.makalu.cc/mkl/api/moveCamera',
            data: JSON.stringify({
              "token": tokenStr,
              "action":e.currentTarget.dataset.action,
              "h5s": true
            }),
            method:"POST",
            contentType:"application/json",
            success:function(res){
              console.log(res)
              setTimeout(function () {
                wx.request({
                  url: 'https://video.makalu.cc/mkl/api/moveCamera',
                  data: JSON.stringify({
                    "token": tokenStr,
                    "action":"stop",
                    "h5s": true
                  }),
                  method: "POST",
                  contentType:"application/json",
                  success: function (res){
                    console.log(res)
                  },
                  complete: function (res){
                    console.log(res)
                  },
                })
              }, 1000 )
            }
          })
    },

})
