<template>
  <el-popconfirm :title="`确定结束与${advisorName}的连麦吗？`" v-if="isUnite" @confirm="updateStatus">
    <el-button slot="reference" type="primary">结束连麦</el-button>
  </el-popconfirm>
</template>
<script>
import {  getMixData, updateMixStatus } from '@/api/videoLive';
export default {
  props: {
    liveStatus: {
      type: Number
    },
    advisorName: {
      type: String,
      default: ''
    }
  },
  data(){
    return {
      isUnite: false, // 当前用户是否在连麦中 
      uniteLiveTime: null
    }
  },
  created() {
    this.getUniteLiveUser();
    this.timerGetUniteLiveUser()
  },
  beforeDestroy() {
    clearInterval(this.uniteLiveTime)
  },
  watch: {
    liveStatus(){
      this.timerGetUniteLiveUser()
    }
  },
  methods: {
    timerGetUniteLiveUser() {
      if([1,3].includes(this.liveStatus) && this.$route.query.id) {
        clearInterval(this.uniteLiveTime)
        this.uniteLiveTime = setInterval(()=>{
          this.getUniteLiveUser()
        }, 5000)
      }
    },
    async getUniteLiveUser() {
      console.log('getUniteLiveUser')
      let ret = await getMixData({id: this.$route.query.id})
      if(ret && ret.code === 0 && ret.data) {
        const uniteLiveUser = ret.data
        if(this.$store.state.user.user.advisorInfo && uniteLiveUser.guestId ===  this.$store.state.user.user.advisorInfo.id && uniteLiveUser.status === 2) {
          this.isUnite = true
        } else {
          this.isUnite = false
        }
      }
    },
    async updateStatus() {
      let ret = await updateMixStatus({
        status: 3,
        videoId: this.$route.query.id
      })
      if(ret && ret.code === 0) {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.getUniteLiveUser()
      }
    },
  },
}
</script>