<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="8" class="list" v-for="(item, index) in list" :key="index">
          <el-col :span="24" class="title">
            <span>[{{ item.room_id }}]</span>{{ item.title }}
          </el-col>
          <el-col :span="24" class="video">
            <video :id="item.room_id" controls class="h5video"></video>
          </el-col>
          <el-col :span="24" class="btn">
            <el-button type="danger" size="mini">强制关闭直播</el-button>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: dock } = createNamespacedHelpers('dock');
import flvjs from 'flv.js';
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'index',
  props: {},
  components: {},
  data: function() {
    return {
      list: [],
      datasss: [],
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...dock(['query']),
    async search() {
      let res = await this.query({ status: '1' });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
      }
    },
    searchLive() {
      for (const val of this.list) {
        let player = document.getElementById(val.room_id);
        let rmtpUrl = `${process.env.VUE_APP_LIVE_URL + val.room_id}`;
        if (flvjs.isSupported()) {
          var flvPlayer = flvjs.createPlayer({
            isLive: true,
            type: 'flv',
            url: rmtpUrl,
          });
          flvPlayer.attachMediaElement(player);
          flvPlayer.load(); //加载
          this.datasss.push(flvPlayer);
        }
      }
    },
    get() {
      if (this.list.length > 0) {
        clearInterval(this.timer);
        this.searchLive();
      }
    },
  },
  mounted() {
    this.timer = setInterval(this.get, 1000);
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

<style lang="less" scoped>
.main {
  .list {
    width: 32%;
    height: 295px;
    margin: 0 10px 10px 0;
    border-radius: 5px;
    padding: 10px;
    box-shadow: 0 0 5px #ccc;
    .title {
      text-align: center;
      font-size: 18px;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 2;
      word-break: break-all;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      font-weight: bold;
      height: 54px;
      line-height: 25px;
      span {
        font-size: 16px;
        color: #ff0000;
      }
    }
    .video {
      height: 180px;
      overflow: hidden;
      margin: 0 0 10px 0;
      text-align: center;
      border: 1px solid #f1f1f1;
      .h5video {
        width: 100%;
        height: 180px;
        overflow: hidden;
      }
    }
    .btn {
      text-align: center;
    }
  }
  .list:nth-child(3n) {
    margin: 0 0 10px 0;
  }
  .list:hover {
    cursor: pointer;
    box-shadow: 0 0 5px #666;
    .title {
      color: #409eff;
    }
  }
}
</style>
