<template>
  <el-row v-if="camDetail !== null" class="movie-list">
    <el-col :md="16">
      <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <h3 v-html="camDetail.camName" />
          </div>
          <div id="videoplayer" class="text item">
            <div id="dplayer" ref="dplayer" style="height: 480px;" />
          </div>
        </el-card>
      </el-row>
    </el-col>
    <el-col :md="8">
      <el-row>
        <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <el-select v-model="camNameModel" placeholder="选择摄像头" @change="onSelect">
                <el-option
                  v-for="item in camList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
              <el-button style="float: right; padding: 3px 0" type="text" @click="goToCamRecord">历史录像</el-button>
            </div>
          </el-card>
        </el-row>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import flvjs from 'flv.js'
import DPlayer from 'dplayer'
import { getCamDetail, getCamList } from '@/api/cam'

export default {
  name: 'LivePage',
  data() {
    return {
      flvjs,
      DPlayer,
      video: {
        videoId: 110120119,
        title: '我的直播',
        description: '我的直播',
        view: 10,
        favorite: 10
      },
      user: {
        userId: 10001,
        screenName: '浩',
        avatarUrl: '',
        following: 1024,
        follower: 1024
      },
      calendarDate: new Date(),
      dateList: [],
      dateMap: new Map(),
      showRecordList: false,
      camList: [],
      camNameModel: null,
      camDetail: null,
      form: {},
      recordList: [],
      camRecord: null,
      showCalender: false
    }
  },
  watch: {
    $route() {
      this.$router.go()
    }
  },
  created() {
    const camId = this.$route.params.camId
    if (camId === undefined || camId === null) {
      return
    }

    getCamDetail(camId).then(resp => {
      if (resp.code === 0) {
        var camDetail = resp.data
        this.camDetail = camDetail
        this.camNameModel = camDetail.camName
        document.title = camDetail.camName + '实时录像'

        if (!camDetail.state) {
          const msg = camDetail.camName + '离线'
          console.log(msg)
          var container = document.getElementById('videoplayer')
          console.log(container)
          // container.innerHTML = msg
        } else {
          const pullUrl = camDetail.pullUrl
          this.initFlvPlayer(camId, null, pullUrl)
        }
      }
    })

    getCamList().then(resp => {
      if (resp.code === 0) {
        this.camList = resp.data
      }
    })
  },
  methods: {
    onSelect(value) {
      const path = '/my/cam/live/' + value
      if (this.$route.path === path) {
        this.$router.go(0)
        return
      }
      this.$router.push(path)
    },
    initFlvPlayer(videoId, coverUrl, videoUrl) {
      new DPlayer({
        container: document.getElementById('dplayer'),
        live: true,
        video: {
          url: videoUrl,
          type: 'customFlv',
          customType: {
            customFlv: function(video, player) {
              const flvPlayer = flvjs.createPlayer({
                type: 'flv',
                url: video.src
              })
              flvPlayer.attachMediaElement(video)
              flvPlayer.load()
            }
          }
        }
      })
    },
    goToCamRecord() {
      const path = '/my/cam/record/' + this.camDetail.camId
      if (this.$route.path === path) {
        this.$router.go(0)
        return
      }
      this.$router.push(path)
    }
  }
}
</script>

<style scoped>
/*处于手机屏幕时*/
@media screen and (max-width: 768px) {
  .movie-list {
    padding-top: 8px;
    padding-left: 0.5%;
    padding-right: 0.5%;
  }
}

.movie-list {
  padding-top: 15px;
  padding-left: 5%;
  padding-right: 5%;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.budge{
  width: 10px;
  height: 10px;
  border-radius: 5px;
  margin: 10px auto;
}
.blue{
  background-color: #409eff;
}
</style>
