<template>
  <div
    class="video_container"
    :class="largeScreen ? 'large_screen' : 'normal_screen'"
  >
    <!-- 弹窗内容 -->
    <div class="dialog_box">
      <!-- 头部 -->
      <div class="dialog_header_box">
        <div class="dialog_header_title">
          {{ cameraData ? cameraData.name : "监控视频" }}
        </div>
      </div>
      <!-- 内容 -->
      <div
        class="dialog_content_box"
        :class="{ large_content: largeScreen }"
        v-show="!!cameraId"
      >
        <div class="content_box">
          <div
            class="img_box"
            :class="{ comm_ptz: largeScreen && cameraData.ptzEnable }"
          >
            <img :src="url" v-if="url" />
            <div class="image_slot" v-else>
              <i class="el-icon-video-camera"></i>
              <span>无信号</span>
            </div>
          </div>
          <div class="control_box" v-if="largeScreen && cameraData.ptzEnable">
            <div class="video-opt">
              <div class="comm-panel">
                <div
                  class="btn-box"
                  v-for="item of commandConfig.direction"
                  :key="item.type"
                >
                  <el-button
                    type="primary"
                    :icon="`el-icon-${item.type}`"
                    size="mini"
                    @mousedown.native="changePtzCommParam(item.command, false)"
                    @mouseup.native="changePtzCommParam(item.command, true)"
                  ></el-button>
                </div>
              </div>
              <div class="zoom-panel">
                <div
                  class="btn-box"
                  v-for="item of commandConfig.zoom"
                  :key="item.type"
                >
                  <el-button
                    type="primary"
                    :icon="`el-icon-${item.type}`"
                    size="mini"
                    @mousedown.native="changePtzCommParam(item.command, false)"
                    @mouseup.native="changePtzCommParam(item.command, true)"
                  ></el-button>
                </div>
              </div>
              <div class="speed-panel">
                <span class="form-label">速度：</span>
                <el-select
                  class="myInput"
                  size="mini"
                  v-model="speed"
                  placeholder="请选择速度"
                >
                  <el-option
                    v-for="item in Array.from(
                      { length: 7 },
                      (item, index) => index + 1
                    )"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </div>
              <div class="preset-panel">
                <span class="form-label">预置点：</span>
                <el-select
                  class="myInput"
                  size="mini"
                  v-model="presetIndex"
                  placeholder="请选择预置点"
                >
                  <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
                  type="primary"
                  size="mini"
                  @click="changePtzPresetParam(8)"
                  >设置</el-button
                >
                <el-button
                  type="primary"
                  size="mini"
                  @click="changePtzPresetParam(39)"
                  >转到</el-button
                >
              </div>
              <div class="playback-panel">
                <span class="form-label">时间：</span>
                <el-date-picker
                  style="max-width: 170px"
                  class="myInput"
                  v-model="startTime"
                  type="datetime"
                  placeholder="选择回放开始时间"
                  size="mini"
                ></el-date-picker>
                <el-button
                  type="success"
                  size="mini"
                  @click="changePlayTypeParam(true)"
                  >回放</el-button
                >
                <el-button
                  type="primary"
                  size="mini"
                  @click="changePlayTypeParam(false)"
                  >实时</el-button
                >
              </div>
            </div>
          </div>
        </div>
      </div>
      <div
        class="dialog_content_box"
        :class="{ large_content: largeScreen }"
        v-show="cameraData && !cameraId"
        style="color: #fff"
      >
        <!-- 暂无数据 -->
        <!-- <img :src="require(`@/assets/imgs/maps/video_img_${cameraData.id}.png`)" alt="" /> -->
      </div>
      <div class="info_box">
        <span class="label">浓度</span>
        <span class="value">{{ ndValue || 0 }}</span>
        <span class="unit">ppm.m</span>
      </div>
    </div>
  </div>
</template>
<script>
import Dayjs from "dayjs";
import { mapGetters } from "vuex";
export default {
  name: "VideoImageWindow",
  props: {
    cameraId: {
      type: String,
      require: true,
    },
    cameraData: {
      type: Object,
      default: () => ({}),
    },
  },
  computed: {
    ...mapGetters(["rtData"]),
  },
  watch: {
    rtData: {
      handler(newVal) {
        console.log(newVal);
        if (!!this.cameraData.tags) {
          if (!!this.cameraData.dataList) {
            this.cameraData.dataList.forEach((item) => {
              if (!!item.tag && !!newVal[item.tag]) {
                item.value = newVal[item.tag].value + " " + item.unit;
              }
              if (item.isTag && !!newVal[item.value]) {
              }
            });
          }
        }
      },
      deep: true,
    },
  },
  data() {
    return {
      largeScreen: false,
      ws: null,
      url: "",
      lastUrl: "",
      // 视频控制相关 start
      commandConfig: {
        //视频控制开关配置
        direction: [
          //方向
          { type: "top-left", name: "左上", command: 25 },
          { type: "top", name: "上", command: 21 },
          { type: "top-right", name: "右上", command: 26 },
          { type: "back", name: "左", command: 23 },
          { type: "rank", name: "中", command: 0 },
          { type: "right", name: "右", command: 24 },
          { type: "bottom-left", name: "左下", command: 27 },
          { type: "bottom", name: "下", command: 22 },
          { type: "bottom-right", name: "右下", command: 28 },
        ],
        zoom: [
          //放大缩小
          { type: "zoom-in", name: "放大", command: 11 },
          { type: "zoom-out", name: "缩小", command: 12 },
        ],
      },
      startTime: null,
      speed: 4,
      presetIndex: 1,
      // 视频控制 end
      ndValue: "0",
    };
  },
  beforeDestroy() {
    this.closeHandler();
  },
  created() {
    this.init(JSON.stringify({ id: this.cameraId }));
  },
  mounted() {
    const ndTopic = "DataCommunication/TagData/" + this.cameraData.tags.ndTag;
    const _this = this;
    this.$store.dispatch("mqtt/subscribe", {
      topic: [ndTopic],
      onMessage: (topic, message, packet) => {
        // console.log(topic);
        const content = message.toString();
        if (topic === ndTopic) {
          //console.log(new Date(), content);
          const data = JSON.parse(content);
          _this.ndValue = data.Value;
        }
      },
    });
    this.$once("hook:beforeDestroy", () => {
      this.$store.dispatch("mqtt/unsubscribe", [ndTopic]);
    });
  },
  methods: {
    getTagValue(tag) {
      return this.rtData[this.cameraData.tags[tag]]?.Value || 0;
    },
    // 放大缩小
    showVideoPopup(data) {
      this.largeScreen = data;
      this.$nextTick(() => {
        if (data) {
          this.baseDialogConfig.width = 937;
          this.baseDialogConfig.height = 598;
          this.baseDialogConfig.center = true;
        } else {
          this.baseDialogConfig.width = 420;
          this.baseDialogConfig.height = 252;
          this.baseDialogConfig.center = false;
        }
      });
    },
    init(str) {
      // this.socketList[`ws_${this.cameraId}`];
      this.ws = new WebSocket(
        VUE_APP_BASE_WS() + "/VideoOverWebSocket"
        // "wss://" + window.location.host + BASE_URL + "/VideoOverWebSocket"
      );
      this.ws.onmessage = (evt) => {
        if (typeof evt.data === "string") {
          console.log(JSON.parse(evt.data).Msg);
        } else {
          let _this = this;
          // const updateUrl = _.debounce(function () {
          let currUrl = URL.createObjectURL(evt.data);
          if (_this.lastUrl) URL.revokeObjectURL(_this.lastUrl);
          _this.lastUrl = currUrl;
          _this.url = currUrl;
          // }, 500);
          // updateUrl();
        }
      };
      this.handleSend(str);
      // console.log(window);
    },
    handleSend(str) {
      if (this.ws.readyState === WebSocket.OPEN) {
        this.ws.send(str);
      } else if (this.ws.readyState == WebSocket.CONNECTING) {
        // Wait for the open event, maybe do something with promises
        // depending on your use case. I believe in you developer!
        this.ws.addEventListener("open", () => this.handleSend(str));
      } else {
        // etc.
      }
    },
    updateEvent(data, state) {
      const param = JSON.stringify(data);
      if (this.ws && this.ws.readyState == 1) {
        this.handleSend(param);
      } else {
        this.init(param);
      }
      if (state) {
        setTimeout(() => {
          this.url = "";
          if (this.lastUrl) URL.revokeObjectURL(this.lastUrl);
          this.lastUrl = "";
        }, 500);
      }
    },
    closeHandler() {
      this.ws && this.ws.close();
      setTimeout(() => {
        if (this.url) URL.revokeObjectURL(this.url);
        this.url = "";
        this.lastUrl = "";
        this.ws &&
          (this.ws.onclose = (evt) => {
            // this.ws = null;
            console.log("websocket已关闭");
          });
      }, 500);
    },
    changePtzCommParam(comm, stop) {
      if (!comm) return;
      let param = {
        id: this.cameraId,
        ptzCommand: comm,
        ptzStop: stop,
        ptzSpeed: this.speed,
      };
      this.updateEvent(param);
    },
    changePtzPresetParam(comm) {
      if (!comm) return;
      let param = {
        id: this.cameraId,
        ptzPreset: comm,
        ptzPresetIndex: this.presetIndex,
      };
      this.updateEvent(param);
    },
    /**
     * state  true代表回放模式
     */
    changePlayTypeParam(state) {
      let param = { id: this.cameraId };
      if (state) {
        if (!this.startTime) {
          this.$message.warning("请选择回放开始时间！");
          return;
        }
        param["startTime"] = Dayjs(this.startTime).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      this.updateEvent(param, true);
    },
  },
};
</script>
<style lang="less" scoped>
.normal_screen {
  background: url("~@/assets/imgs/maps/img_SPJK_bg@2x.png") center center
    no-repeat;
  background-size: 100% 100%;
  width: 1.984375rem /* 381/192 */;
  height: 1.385417rem /* 266/192 */;
}
.large_screen {
  background: url("~@/assets/imgs/dialog/pop_enlarge_TW@2x.png") center center
    no-repeat;
  background-size: 100% 100%;
  width: 4.880208rem /* 937/192 */;
  height: 3.114583rem /* 598/192 */;
}
.video_container {
  pointer-events: auto;
  .dialog_box {
    width: 100%;
    height: 100%;
    .dialog_header_box {
      position: relative;
      .dialog_header_title {
        width: 100%;
        height: 35px;
        line-height: 40px;
        // font-family: 时尚中黑简体;
        font-size: 16px;
        padding-left: 24px;
        color: #fff;
      }
      .dialog_header_retract {
        // line-height: 40px;
        position: absolute;
        top: -0.041667rem /* 8/192 */;
        right: 0.182292rem /* 35/192 */;
        cursor: pointer;
        img {
          width: 20px;
          height: 20px;
        }
      }
      .dialog_header_close {
        // line-height: 40px;
        position: absolute;
        top: -0.041667rem /* 8/192 */;
        right: 0.052083rem /* 10/192 */;
        img {
          width: 20px;
          height: 20px;
        }
        cursor: pointer;
      }
    }
    .dialog_content_box {
      box-sizing: border-box;
      width: 342px;
      height: 173px;
      margin: 0 auto;
      display: flex;
      justify-content: center;
      align-items: center;
      .content_box {
        width: 100%;
        height: 100%;
        display: flex;
        .img_box {
          width: 100%;
          height: 100%;
          &.comm_ptz {
            width: 75%;
          }
        }
        .control_box {
          width: 25%;
          height: 100%;
          border-left: 1px solid #7bacaa;
        }
      }

      img {
        width: 100%;
        height: 100%;
        object-fit: fill;
        // display: block;
      }
      .image_slot {
        width: 100%;
        height: 100%;
        // background-color: #f5f7fa;
        color: #f5f7fa;
        font-size: 24px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-weight: 500;
        span {
          font-size: 16px;
        }
      }

      &.large_content {
        width: 4.630208rem /* 889/192 */;
        height: 2.770833rem /* 532/192 */;
      }
    }
    .info_box {
      width: 100%;
      height: 32px;
      margin-top: 6px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      color: #fff;
      .value {
        padding: 0 8px;
        color: #ffc800;
      }
    }
  }
}
.video-opt {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  .comm-panel {
    width: 180px;
    display: flex;
    flex-wrap: wrap;
    .el-button {
      background: #51c6c1;
      border-color: #51c6c1;
    }
  }
  .zoom-panel {
    width: 120px;
    display: flex;
    .el-button {
      background: #51c6c1;
      border-color: #51c6c1;
    }
  }
  .speed-panel {
    margin-top: 10px;
    display: flex;
    align-items: center;
  }
  .preset-panel {
    margin-top: 10px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .el-select {
      width: 150px;
    }
    .el-button {
      margin-top: 10px;
      background: rgba(0, 60, 57, 0.3);
      border: 1px solid #65f1ea;
      color: #7cd6dd;
    }
  }
  .playback-panel {
    margin-top: 10px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .form-label {
      margin-bottom: 10px;
    }
    .el-button {
      margin-top: 15px;
      background: rgba(0, 60, 57, 0.3);
      border: 1px solid #65f1ea;
      color: #7cd6dd;
    }
  }
  .btn-box {
    flex-shrink: 0;
    width: 60px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .form-label {
    color: #7cd6dd;
    display: inline-block;
    width: 50px;
    flex-shrink: 0;
    // text-align: right;
  }
  .myInput {
    ::v-deep .el-input__inner {
      // width: 110px;
      height: 30px;
      border-radius: 0;
      background: rgba(0, 20, 23, 0.6);
      border: 0.5px solid #51c6c1;
      color: #7cd6dd;
      // padding-right: 35px;
    }
    ::v-deep .el-input__inner:focus {
      border: 0.5px solid #51c6c1 !important;
    }
    ::v-deep .el-input.is-focus .el-input__inner {
      border: 0.5px solid #51c6c1 !important;
    }
    ::v-deep .el-input__suffix {
      right: 5px;
    }
    ::v-deep .el-input__icon {
      line-height: 30px;
    }
  }
}
</style>
