<template>
  <div :style="dialogStyle" class="dialog-container" v-show="visible">
    <div class="dialog-header">
      <span class="el-icon-video-camera"></span>
      <span style="margin-left: 10px">{{ cameraData.name || '' }}</span>
      <span class="el-icon-close close-icon" @click="closeHandler"></span>
      <span class="el-icon-search zoom-icon" @click="zoomHandler"></span>
    </div>
    <div class="dialog-body">
      <div :style="imgStyle">
        <img style="width: 100%; height: 100%; object-fit: contain" :src="url" v-if="url" />
        <div class="image-slot" v-if="!url">
          <i class="el-icon-video-camera"></i>
        </div>
      </div>
      <div class="ptz-panel" v-if="cameraData.ptzEnable">
        <div style="padding-top: 20px; padding-left: 15px">
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-top-left"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(25, false)"
            @mouseup.native="ptzCommHandle(25, true)"
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-top"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(21, false)"
            @mouseup.native="ptzCommHandle(21, true)"
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-top-right"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(26, false)"
            @mouseup.native="ptzCommHandle(26, true)"
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-back"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(23, false)"
            @mouseup.native="ptzCommHandle(23, true)"
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-rank"
            size="mini"
            disabled
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-right"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(24, false)"
            @mouseup.native="ptzCommHandle(24, true)"
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-bottom-left"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(27, false)"
            @mouseup.native="ptzCommHandle(27, true)"
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-bottom"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(22, false)"
            @mouseup.native="ptzCommHandle(22, true)"
          ></el-button>
          <el-button
            style="margin-left: 0px"
            type="info"
            icon="el-icon-bottom-right"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(28, false)"
            @mouseup.native="ptzCommHandle(28, true)"
          ></el-button>
          <el-button
            style="margin-left: 20px; margin-top: 10px"
            type="info"
            icon="el-icon-zoom-in"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(11, false)"
            @mouseup.native="ptzCommHandle(11, true)"
          ></el-button>
          <el-button
            style="margin-left: 10px; margin-top: 10px"
            type="info"
            icon="el-icon-zoom-out"
            size="mini"
            :disabled="onPlayBack"
            @mousedown.native="ptzCommHandle(12, false)"
            @mouseup.native="ptzCommHandle(12, true)"
          ></el-button>
          <br />
          <a>速度：</a>
          <el-select
            style="margin-top: 10px; width: 70px"
            size="mini"
            v-model="speed"
            placeholder="请选择速度"
            :disabled="onPlayBack"
          >
            <el-option
              v-for="item in Array.from({ length: 10 }, (item, index) => index + 1)"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </div>
        <div style="padding-top: 20px; padding-left: 15px">
          <a>预置点：</a>
          <el-select
            style="width: 70px"
            size="mini"
            v-model="presetIndex"
            placeholder="请选择预置点"
            :disabled="onPlayBack"
          >
            <el-option
              v-for="item in Array.from({ length: 255 }, (item, index) => index + 1)"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
          <el-button
            style="margin-left: 10px; margin-top: 10px"
            type="info"
            size="mini"
            :disabled="onPlayBack"
            @click="ptzPresetHandle(8)"
            >设置</el-button
          >
          <el-button
            style="margin-left: 10px; margin-top: 10px"
            type="info"
            size="mini"
            :disabled="onPlayBack"
            @click="ptzPresetHandle(39)"
            >转到</el-button
          >
        </div>
      </div>
      <div class="playback-panel" v-if="cameraData.canPlayBack">
        <el-date-picker
          v-model="startTime"
          type="datetime"
          placeholder="选择回放开始时间"
          :picker-options="datePickerOptions"
        ></el-date-picker>
        <el-button style="margin-left: 10px" type="success" @click="playBackHandle" :disabled="!startTime">回放</el-button>
        <el-button type="primary" @click="realTimekHandle" :disabled="!onPlayBack">实时</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import Dayjs from 'dayjs'

export default {
  name: 'ImageVideo',
  components: {},
  data() {
    return {
      url: '',
      lastUrl: '',
      ws: null,
      zoom: false,
      addWidth: 0,
      addHeight: 0,
      startTime: null,
      speed: 5,
      presetIndex: 1,
      datePickerOptions: {
        disabledDate: (currData) => currData > new Date()
      },
      onPlayBack: false
    }
  },
  props: {
    visible: false,
    cameraData: {
      type: Object,
      required: true,
    },
    size: {
      type: Object,
      default: () => {
        return { height: 420, width: 680 }
      },
    },
  },
  computed: {
    dialogStyle() {
      if (this.zoom) {
        let clientHeight = document.documentElement.clientHeight
        return {
          height: clientHeight - 174 + this.addHeight + 'px',
          width:
            ((clientHeight - 234) / (this.size.height - 60)) * (this.size.width - 40) +
            40 +
            this.addWidth +
            'px',
        }
      } else {
        return {
          height: this.size.height + this.addHeight + 'px',
          width: this.size.width + this.addWidth + 'px',
        }
      }
    },
    imgStyle() {
      return {
        position: 'absolute',
        left: '0px',
        top: '0px',
        right: this.addWidth + 'px',
        bottom: this.addHeight + 'px',
        'object-fit': 'contain',
      }
    },
  },
  watch: {
    cameraData(val) {
      if (val.id) {
        //console.log(val)
        if (val.canPlayBack) this.addHeight = 50
        else this.addHeight = 0
        if (val.ptzEnable) this.addWidth = 150
        else this.addWidth = 0
        if (this.ws && this.ws.readyState == 1) {
          this.ws.send(JSON.stringify({ id: val.id, canControl: val.ptzEnable }))
        } else {
          this.init(JSON.stringify({ id: val.id, canControl: val.ptzEnable }))
        }
        setTimeout(() => {
          this.url = ''
          if (this.lastUrl) URL.revokeObjectURL(this.lastUrl)
          this.lastUrl = ''
        }, 500)
      }
    },
  },
  mounted() {},
  created() {
    //this.init()
  },
  methods: {
    init(str) {
      this.ws = new WebSocket(
        'ws://' + window.location.host + process.env.VUE_APP_BASE_API + '/VideoOverWebSocket'
      )
      let _this = this
      this.ws.onmessage = function (evt) {
        if (typeof evt.data === 'string') {
          console.log(JSON.parse(evt.data).Msg)
        } else {
          let currUrl = URL.createObjectURL(evt.data)
          if (_this.lastUrl) URL.revokeObjectURL(_this.lastUrl)
          _this.lastUrl = currUrl
          _this.url = currUrl
        }
      }
      if (str) {
        setTimeout(() => {
          this.ws.send(str)
        }, 500)
      }
    },
    zoomHandler() {
      this.zoom = this.zoom ? false : true
    },
    closeHandler() {
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(JSON.stringify({ id: '0' }))
      }
      setTimeout(() => {
        this.ws.close()
        if (this.url) URL.revokeObjectURL(this.url)
        this.url = ''
        this.lastUrl = ''
        this.zoom = false
        this.$emit('update:cameraData', {})
        this.$emit('update:visible', false)
      }, 500)
    },
    playBackHandle() {
      this.onPlayBack = true
      if (this.ws && this.ws.readyState == 1) {
        let time = Dayjs(this.startTime).format('YYYY-MM-DD HH:mm:ss')
        this.ws.send(JSON.stringify({ id: this.cameraData.id, startTime: time }))
      } else {
        this.init(JSON.stringify({ id: this.cameraData.id, startTime: time }))
      }
      setTimeout(() => {
        this.url = ''
        if (this.lastUrl) URL.revokeObjectURL(this.lastUrl)
        this.lastUrl = ''
      }, 500)
    },
    realTimekHandle() {
      this.onPlayBack = false
      this.startTime = null
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(JSON.stringify({ id: this.cameraData.id, canControl: this.cameraData.ptzEnable }))
      } else {
        this.init(JSON.stringify({ id: this.cameraData.id, canControl: this.cameraData.ptzEnable }))
      }
      setTimeout(() => {
        this.url = ''
        if (this.lastUrl) URL.revokeObjectURL(this.lastUrl)
        this.lastUrl = ''
      }, 500)
    },
    ptzCommHandle(comm, stop) {
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzCommand: comm,
            ptzStop: stop,
            ptzSpeed: this.speed,
          })
        )
      } else {
        this.init(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzCommand: comm,
            ptzStop: stop,
            ptzSpeed: this.speed,
          })
        )
      }
    },
    ptzPresetHandle(comm) {
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzPreset: comm,
            ptzPresetIndex: this.presetIndex,
          })
        )
      } else {
        this.init(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzPreset: comm,
            ptzPresetIndex: this.presetIndex,
          })
        )
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.dialog-container {
  position: absolute;
  right: 20px;
  bottom: 20px;
  z-index: 5;
  color: white;
  background-color: rgba(48, 65, 86, 0.8);
  box-sizing: border-box;
  border-radius: 20px;
}
.dialog-header {
  position: absolute;
  left: 20px;
  top: 0px;
  right: 20px;
  height: 40px;
  font-size: 20px;
  padding-top: 10px;
  float: left;
}
.close-icon {
  margin-left: 10px;
  float: right;
}
.close-icon:hover {
  color: rgb(28, 188, 156);
}
.zoom-icon {
  float: right;
}
.zoom-icon:hover {
  color: rgb(28, 188, 156);
}
.dialog-body {
  position: absolute;
  left: 20px;
  top: 40px;
  right: 20px;
  bottom: 20px;
}
.ptz-panel {
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  width: 150px;
}
.playback-panel {
  position: absolute;
  left: 0px;
  right: 0px;
  bottom: 0px;
  height: 50px;
  padding-top: 15px;
}
::v-deep .image-slot {
  width: 100%;
  height: 100%;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
