<template>
  <div v-if="showType == 1" :class="pageType" :style="stylCompoment()" @click="edit">
    <div>
      <template v-if="videoUrl || cameraCode"><span></span>{{ topData.line }}</template>
    </div>
    <div ref="mse" v-if="!isHf" style="width: 100%;height: 100%;" :class="videoUrl || cameraCode ? 'yes' : 'no_select'">
    </div>
    <div ref="mse" v-else style="width: 100%"></div>
    <div v-if="videoUrl || cameraCode">
      {{ topData.mileagePosition }} {{ topData.lineDir
      }}{{ topData.measuringPoint }}
    </div>
    <div v-else></div>
  </div>
  <div v-else-if="showType == 2" class="videoPlay-type-2" @click="edit">
    <div class="content">
      <div>
        <template v-if="videoUrl || cameraCode">
          <span :class="topData.isLock == 1 ? 'blue' : 'red'"></span>{{ topData.line }}</template>
      </div>

      <div ref="mse" style="width: 100%" :class="videoUrl || cameraCode ? 'yes' : 'no_select'"
        :style="{ height: heigthIsFull == true ? '100%' : 'auto' }"></div>
      <div v-if="videoUrl || cameraCode">
        <!-- {{ videoListSelectNum }} -->
        {{ topData.mileagePosition }}
        {{ topData.lineDir }}
        {{ topData.measuringPoint }}
        <spanspan>{{ topData.measuringPoint }}</spanspan>
        <!-- {{ numberVideos }} -->
      </div>
      <div v-else></div>
    </div>
  </div>
</template>

<script>
import flvjs from "flv.js";
import { records, lineDate } from "@/api/video";
import { msgAlarmDetail } from "@/api/alarmList";

// videoPlayer
// 单个视频播放插件
export default {
  name: "VideoPlayer",
  props: {
    showType: {
      type: Number,
      default() {
        return 1;
      },
    },
    videoListSelectNum: {
      type: String,
      default: "",
    },
    pageType: {
      // 类型类型 palyAndPlayback
      type: String,
      default() {
        return "";
      },
    },
    videoUrl: {
      // 视频流,优先级比cameraCode高，当有videoUrl时，cameraCode不再请求数据
      type: String,
      default() {
        return "";
      },
    },
    items: {
      type: String,
      default() {
        return "";
      },
    },
    isHf: false,
    cameraCode: {
      // 视频code，默认情况下会使用这个请求地址
      type: String,
      default() {
        return "";
      },
    },
    heigthIsFull: {
      type: Boolean,
      default: true,
    },

    isTime: {
      type: Boolean,
      default: false,
    },
    flay: {
      type: Boolean,
      default: true,
    },
    videoId: {
      // 摄像机code
      type: String,
      default() {
        return "";
      },
    },
  },
  data() {
    return {
      jessibuca: null,
      player: "",
      setTime: "",
      topData: {
        line: "",
        mileagePosition: "",
        lineDir: "",
        electricQuantity: "",
        cameraCoding: "",
        measuringPoint: "",
        isLock: 1,
      },
    };
  },
  watch: {
    items() {
      this.checkChange("showType");
    },
    showType() {
      this.checkChange("showType");
      this.destroyPlayback();
    },
    cameraCode() {
      this.setN && clearTimeout(this.setN);
      this.destroyPlayback();
console.log(123,)
      this.cameraCode && this.checkChange("cameraCode");
    },
    videoUrl() {
      this.setN && clearTimeout(this.setTime);
      this.destroyPlayback();
      this.setTime && clearTimeout(this.setTime);
      this.videoUrl && this.checkChange("videoUrl");
    },
  },
  beforeDestroy() {
    console.log("[视频单例]视频卸载");
    this.destroyPlayback();

    if (this.setTime) {
      clearTimeout(this.setTime);
    }

    if (this.setN) {
      clearTimeout(this.setN);
    }
  },
  mounted() {
    console.log(12345)
    this.checkChange("mounted");
  },
  methods: {
    stylCompoment() {
      if(window.location.href.includes("alarmDetail")){
        return 'width: 100%;height: calc(100vh - 317px); position: relative; background-color: #000'
      }
      return 'width: 100%;height: 665px; position: relative; background-color: #000'
    },
    // 视频卸载
    destroyPlayback() {
      if (this.jessibuca) {
        this.jessibuca.destroy();
      }
    },
    // 摄像机发生变化
    checkChange(source) {
      console.log(
        "[视频单例]发生,摄像机码",
        source,
        this.videoUrl,
        this.cameraCode,
        this.flay
      );

      if (!this.videoUrl && !this.cameraCode) {
        // console.log("请除代码");
        // this.$refs.mse.querySelector("video").src = "";
        // this.$refs.mse.querySelector("xg-controls").innerHTML = "";
        // return;
      }

      // 获取视频流

      if (this.videoUrl) {
        this.videoInit(this.videoUrl);
      } else if (this.cameraCode) {
        // 类型二增加线信息
        if (this.showType == 2) {
          lineDate(this.cameraCode).then((res) => {
            if (res.code == 200) {
              this.topData.line = res.data[0];
              this.topData.mileagePosition = res.data[1];
              this.topData.lineDir = res.data[2] == 1 ? "上行" : "下行";
              this.topData.cameraCoding = res.data[3];
              this.topData.measuringPoint = res.data[5];
              this.topData.isLock = res.data[6];
              console.log(this.videoListSelectNum, "00994343----------啊啊啊");
              if (this.videoListSelectNum == "9") {
                this.topData.measuringPoint = res.data[5].slice(0, 5);
              } else {
                this.topData.measuringPoint = res.data[5].slice(0, 15);
              }
            } else {
              this.topData.line = "";
              this.topData.mileagePosition = "";
              this.topData.lineDir = "";
              this.topData.cameraCoding = "";
              this.topData.measuringPoint = "";
              this.topData.isLock = 1;
            }
          });
        }

        if (this.$route.name === "/alarmDetail") {
          var set = setInterval(() => {
            // if (this.$store.state.lineDateProPost) {
              if (this.videoId) {
                msgAlarmDetail(
                // this.$store.state.lineDateProPost.cameraCode,
                // this.flay
                this.videoId
              ).then((res) => {
                this.videoInit(res.data.alarmUrl);
              });
              clearInterval(set);
            }
          }, 100);
          return;
        }

        records(this.cameraCode, this.flay).then((res) => {
          this.videoInit(res.data);
        });
      } else {
        this.destroyPlayback();
      }
    },
    // 视频初始化
    videoInit(url) {
      console.log(123,url)
      if (!url) {
        return;
      }
      console.log(url, "urlurlurlurl");
      if (!this.$refs.mse) {
        return;
      }
      const isMp4 = url.includes(".mp4") || false;
      if (isMp4) {
        this.jessibuca && this.jessibuca.destroy();
        setTimeout(() => {
          const config = {
            el: this.$refs.mse,
            url: url,
            fluid: isMp4 ? false : true,
            poster: "",
            plugins: [],
            isLive: isMp4 ? false : !this.isTime,
            autoplay: true,
            autoplayMuted: true,
          };
          this.player = new Player(config);
        }, 0);
        return;
      }
      try {
        this.jessibuca = new JessibucaPro({
          container: this.$refs.mse,
          videoBuffer: 0.2,
          isResize: true,
          text: "",
          loadingText: "加载中",
          useMSE: false,
          useSIMD: false,
          autoWasm: true,
          timeout: 180,
          heartTimeout: 180,
          supportDblclickFullscreen: true,
          loadingTimeoutReplayTimes: -1,
          heartTimeoutReplayTimes: -1,
          // debug: true,
          // debugLevel: "debug",
          controlAutoHide: true,
          showBandwidth: true,
          heartTimeoutReplayUseLastFrameShow: true,
          replayUseLastFrameShow: true,
          operateBtns: {
            fullscreen: true,
            screenshot: true,
            play: true,
            audio: true,
          },
          forceNoOffscreen: true,
          useWebFullScreen: true,
          isNotMute: false,
        });
        this.jessibuca.on('error',e => {console.log('error',error)})
        console.log(111,this.jessibuca)
        this.jessibuca.play(url);
      } catch (error) {
        console.log(error);
      }
    },
    edit() {
      this.$emit("onClick");
    },
  },
};
</script>

<style scoped lang="scss">
.warning-view {
  position: relative;
  display: flex;
  align-items: center;
  border: 1px solid #20a0ff;
  padding: 5px;
  margin-top: 10px;

  .status-icon {
    padding: 2px 5px;
    background-color: #d68e15;
    position: absolute;
    right: 5px;
    top: 5px;
    font-size: 12px;
    border-radius: 5px;
    color: #fff;
  }

  .warning-image {
    width: 120px;
    height: 120px;
    border: 1px solid #fff;
    margin-right: 10px;
  }

  .warning-content {
    color: #8eacc8;
    font-size: 13px;
    flex: 1;

    .text-base {
      margin-top: 5px;
    }

    .text-1 {
      color: #50a4f2;
      font-size: 15px;
    }
  }

  .warning-operation {
    display: flex;
    font-size: 12px;
    color: #fff;
    margin-top: 4px;
    justify-content: flex-end;

    .button-1 {
      width: 60px;
      padding: 2px 0px;
      border-radius: 5px;
      border: 1px solid #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .button-2 {
      width: 60px;
      padding: 2px 0px;
      border-radius: 5px;
      margin-left: 5px;
      border: 1px solid #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.videoPlay-type-2 {
  position: relative;
  width: 100%;
  height: 100%;
  position: relative;
  border-radius: 10px;
  padding: 1px;

  .content {
    width: 100%;
    height: 100%;
    position: relative;
    border-radius: 5px;
    border: 1px solid #70b0ff;
    overflow: hidden;
    color: #fff;
    text-indent: 21px;
    background: #05152f;
    border-radius: 10px;

    >div {
      height: 35px;
      line-height: 32px;
      font-size: 14px;

      >span {
        width: 7px;
        height: 7px;
        display: inline-block;
        background: red;
        border-radius: 50%;
        margin-right: 9px;

        &.red {
          background-color: #ff0000;
          box-shadow: 0px 2px 6px #ff0000;
        }

        &.blue {
          background-color: #00ffc6;
          box-shadow: 0px 2px 6px #00ffc6;
        }
      }
    }
  }
}

.palyAndPlayback {
  height: calc(100% - 40px) !important;
}

.palyAndPlayback .xgplayer {
  height: 100% !important;
  padding-top: 0 !important;
}

.palyAndPlayback .xgplayer-is-error {
  height: 100% !important;
  padding-top: 0 !important;
  display: flex;
  align-items: center;
  justify-content: center;
}

.palyAndPlayback ::v-deep .xgplayer-is-error video:first-child {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  outline: none;
}

@media screen and (max-width: 1400px) {}

@media screen and (max-width: 1200px) {}

@media screen and (max-width: 900px) {}

@media screen and (max-width: 720px) {}

@media screen and (max-height: 880px) {
  .videoPlay-type-2 {
    .content {
      >div {
        height: 26px;
        line-height: 26px;
        font-size: 13px;
      }
    }
  }

  .jessibuca-container {
    height: calc(100% - 54px) !important;
  }

  .el-card .yes {
    height: 100% !important;
  }
}

@media screen and (max-height: 800px) {
  .jessibuca-container {
    height: calc(100% - 44px) !important;
  }

  .el-card .yes {
    height: 100% !important;
  }

  .videoPlay-type-2 {
    .content {
      >div {
        height: 22px;
        line-height: 22px;
        font-size: 13px;
      }
    }
  }
}

@media screen and (max-height: 720px) {
  .el-card .yes {
    height: 100% !important;
  }

  .videoPlay-type-2 {
    .content {
      >div {
        height: 18px;
        line-height: 18px;
        // font-size: 12px;
      }
    }
  }

  .jessibuca-container {
    height: calc(100% - 36px) !important;
  }
}
</style>

<style>
.homeRight-video-content .list-2-3 {
  height: 28% !important;
}
</style>
