<template>
  <div class="slw" :id="videoId">
    <div class="slw-title">
      <h2>{{ name }}</h2>
      <div class="slw-title__close" @click="removeMonitor">
        <i class="el-icon-circle-close"></i>
        <span>关闭视频</span>
      </div>
    </div>
    <iframe
      v-if="isShow"
      :id="id"
      allow="autoplay *; microphone *; fullscreen *" allowfullscreen allowtransparency key="" allowusermedia frameBorder="no"
      style="width: 100%; height: 100%;"
      :src="`https://cdn.cunwuyun.cn/slw2.0/index.html?url=${src}`">
    </iframe>
    <div class="slw-bottom">
      <Timeline class="Timeline" @pause="isLiveing = false" :isLiveing="isLiveing" :width="width" ref="timeline" :style="{width: width}"></Timeline>
      <div class="action-bar">
        <div class="left">
          <div class="left-btns">
            <el-tooltip effect="dark" :content="isPause ? '播放' : '暂停'" placement="top">
              <img
                :src="isPause ? 'https://cdn.cunwuyun.cn/slw2.0/images/play.png' : 'https://cdn.cunwuyun.cn/slw2.0/images/pause.png'"
                @click="changePlayStatus">
            </el-tooltip>
          </div>
          <div
            class="volume"
            @mouseleave.stop="isShowVolume = false">
            <img 
              @mouseenter.stop="isShowVolume = true"
              src="https://cdn.cunwuyun.cn/slw2.0/images/sound.png">
            <div class="volume-slider" :class="[isShowVolume ? 'active' : '']">
              <el-slider
                input-size="mini"
                v-model="volume"
                vertical
                @change="onVolume"
                height="80px">
              </el-slider>
            </div>
          </div>
          <div class="play-status">
            <div class="live" v-if="isLiveing">
              <span class="label"></span>
              <i>直播中</i>
              <em>{{ form.date }}</em>
            </div>
            <div v-else class="back-btn" @click="isLiveing = true">回到直播</div>
          </div>
        </div>
        <div class="right">
          <el-tooltip effect="dark" content="选择日期" placement="top">
            <img src="https://cdn.cunwuyun.cn/slw2.0/images/date.png" @click="isShowDate = true">
          </el-tooltip>
          <el-tooltip effect="dark" content="截屏" placement="top">
            <img src="https://cdn.cunwuyun.cn/slw2.0/images/screenshots.png" @click="screenshots">
          </el-tooltip>
          <el-tooltip effect="dark" :content="isFullscreen ? '退出全屏' : '全屏'" placement="top">
            <img src="https://cdn.cunwuyun.cn/slw2.0/images/full-screen.png" @click="fullscreen">
          </el-tooltip>
        </div>
      </div>
    </div>
    <ai-dialog
      title="选择日期"
      :visible.sync="isShowDate"
      width="520px"
      @onConfirm="onConfirm">
      <el-form class="ai-form" ref="form" :model="form" label-width="80px" size="small">
        <el-form-item label="选择日期" prop="date" :rules="[{ required: true, message: '请选择日期', trigger: 'change' }]">
          <el-date-picker
            value-format="yyyy-MM-DD"
            v-model="form.date"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </div>
</template>

<script>
  import Timeline from './Timeline'

  export default {
    props: ['src', 'name'],

    name: 'slwVideo',

    components: {
      Timeline
    },

    data () {
      return {
        currIndex: 0,
        isShowDate: false,
        isShowPlayBtn: false,
        isShow: true,
        isShowVolume: false,
        isLiveing: true,
        form: {
          date: ''
        },
        date: '',
        isPause: false,
        width: '',
        volume: 100,
        videoId: `slwvideo-${new Date().getTime()}`,
        id: `video-${new Date().getTime()}`,
        isFullscreen: false
      }
    },

    watch: {
      src: {
        handler (val) {
          if (val) {
            this.isShow = false

            this.$nextTick(() => {
              this.isShow = true
            })
          }
        },
        immediate: true,
        deep: true
      }
    },

    mounted () {
      this.form.date = this.$moment(new Date()).format('YYYY-MM-DD')
      this.$nextTick(() => {
        this.width = document.querySelector(`#${this.videoId}`).offsetWidth + 'px'
      })
    },

    methods: {
      exitFullscreen () {
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen()
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
        } else if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen()
        }
      },

      changePlayStatus () {
        const subPage = document.querySelector(`#${this.id}`).contentWindow
        subPage.postMessage({
          type: 'play',
          value: this.isPause
        }, '*')
        
        this.isPause = !this.isPause
      },

      onConfirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
          }
        })
      },

      onVolume (e) {
        const v = (e / 100).toFixed(1)
        const subPage = document.querySelector(`#${this.id}`).contentWindow
        subPage.postMessage({
          type: 'volume',
          value: Number(v)
        }, '*')
      },

      fullscreen () {
        if (this.isFullscreen) {
          this.exitFullscreen()
        } else {
          this.requestFullScreen(document.querySelector(`#${this.videoId}`))
        }

        this.isFullscreen = !this.isFullscreen
        this.reset()
      },

      reset () {
        setTimeout(() => {
          this.width = document.querySelector(`#${this.videoId}`).offsetWidth + 'px'
          this.$nextTick(() => {
            this.$refs.timeline.init()
          })
        }, 60)
      },

      screenshots () {
        const subPage = document.querySelector(`#${this.id}`).contentWindow
        subPage.postMessage({
          type: 'screenshot'
        }, '*')
      },

      removeMonitor () {
        this.$emit('close')
      },

      requestFullScreen (element) {
        var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
        if (requestMethod) {
          requestMethod.call(element);
        } else if (typeof window.ActiveXObject !== 'undefined') {
          var wscript = new ActiveXObject('WScript.Shell')
          if (wscript !== null) {
            wscript.SendKeys('{F11}')
          }
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .slw {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;

    iframe {
      border: none;
    }

    .slw-bottom {
      position: absolute;
      bottom: 0;
      left: 0;
      z-index: 1;
      width: 100%;
      transition: all ease-in-out 0.5s;
      // transform: translateY(100%);
    }

    &:hover {
      .slw-title {
        transform: translateY(0%);
      }
      .slw-bottom {
        transform: translateY(0%);
      }
    }

    .slw-title {
      display: flex;
      position: absolute;
      align-items: center;
      justify-content: space-between;
      top: 0;
      left: 0;
      z-index: 1;
      width: 100%;
      height: 40px;
      line-height: 40px;
      padding: 0 16px;
      background: rgba(0, 0, 0, 0.8);
      transition: all ease 0.5s;
      transform: translateY(-100%);

      h2 {
        max-width: 70%;
        font-size: 16px;
        color: #fff;
        font-weight: normal;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;  
      }

      .slw-title__close {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 84px;
        height: 32px;
        background: linear-gradient(180deg, #2E3447 0%, #151825 100%);
        border-radius: 2px;
        cursor: pointer;
        font-size: 12px;
        color: #fff;

        &:hover {
          opacity: 0.9;
        }

        span {
          margin-left: 4px;
        }

        i {
          position: relative;
          font-size: 16px;
        }
      }
    }

    .action-bar {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      height: 40px;
      padding: 0 16px;
      background: rgba(0, 0, 0, 0.8);

      .left {
        display: flex;
        align-items: center;

        .play-status {
          display: flex;
          align-items: center;
          margin-left: 12px;

          em {
            margin-left: 12px;
            font-style: normal;
            color: #fff;
            font-size: 12px;
          }

          .back-btn {
            padding: 4px 10px;
            border-radius: 6px;
            color: #ddd;
            font-size: 12px;
            cursor: pointer;
            background: #343747;

            &:hover {
              opacity: 0.6;
            }
          }

          .live {
            display: flex;
            align-items: center;
            padding: 2px 5px;
            color: rgba(0,255,0,.8);

            i {
              font-size: 12px;
              font-style: normal;
            }

            .label {
              width: 6px;
              height: 6px;
              margin-right: 12px;
              background: rgba(0,255,0,.8);
              border-radius: 50%;
              position: relative;

              &:after {
                content: "";
                width: 12px;
                height: 12px;
                position: absolute;
                left: 50%;
                top: 50%;
                -webkit-transform: translate(-50%,-50%);
                transform: translate(-50%,-50%);
                border-radius: 50%;
                border: 4px solid rgba(0,255,0,.2);
              }
            }
          }
        }

        .volume {
          display: flex;
          align-items: center;
          position: relative;
          height: 100%;

          .volume-slider {
            display: none;
            position: absolute;
            bottom: 20px;
            left: 50%;
            z-index: -1;
            opacity: 0;
            padding: 20px 0 10px;
            background-color: rgba(0, 0,0,.8);
            transform: translate(-50%, 0);
            transition: all ease 0.3s;

            &.active {
              display: block;
              z-index: 1;
              opacity: 1;
            }
          }
        }

        img {
          cursor: pointer;
        }

        .left-btns {
          display: flex;
          align-items: center;
          margin-right: 10px;

          span {
            flex: 1;
            height: 100%;
            line-height: 24px;
            background: #222838;
            color: #c9c9c9;
            font-size: 12px;
            cursor: pointer;

            &.active {
              color: #fff;
              background: linear-gradient(180deg, #28B2EB 0%, #193D91 100%);
            }
          }
        }
      }

      .right {
        color: #c9c9c9;
        font-size: 12px;

        span {
          margin-right: 32px;
          cursor: pointer;

          &:hover {
            opacity: 0.6;
          }
        }

        img {
          margin-right: 16px;
          cursor: pointer;

          &:last-child {
            margin-right: 0;
          }

          &:hover {
            opacity: 0.6;
          }
        }
      }

      & > div {
        display: flex;
        align-items: center;
      }
    }
  }
</style>