<template>
  <div class="student-monitor-record">
    <div class="record-header">
      <div class="header-info">
        <span class="header-info-name">
          <i class="icon icon-user-act"></i>
          {{ info.name }}
        </span>
        <span class="header-info-item"> 证件号：{{ info.identity }} </span>
        <span class="header-info-item"> 课程：{{ info.courseName }} </span>
        <span class="header-info-item"> 批次名称：{{ info.examName }} </span>
        <span class="header-info-item">
          考试时间：{{ info.firstStartTime | datetimeFilter }}
        </span>
      </div>
      <div>
        <el-button
          v-for="source in info.monitorRecord"
          :key="source.videoSource"
          :type="
            curSource.videoSource === source.videoSource ? 'primary' : 'info'
          "
          @click="switchVideo(source)"
          >{{ videoSourceInfo[source.monitorKey] }}</el-button
        >
      </div>
    </div>
    <div class="record-body">
      <div class="record-video">
        <HlsMedia
          v-if="curSource.videoUrl"
          :url="curSource.videoUrl"
          :key="curSource.videoUrl"
          @error="videoError"
        />
        <div v-else class="record-video-none">
          <div>
            <i class="el-icon-video-camera-solid"></i>
            <p>暂无视频</p>
          </div>
        </div>
      </div>
    </div>
    <div class="record-footer">
      <i class="icon icon-error"></i>
      <span
        >提示：无法正常播放，请<i
          class="color-primary record-qr"
          @click="modalIsShow = true"
          >扫描二维码</i
        >。</span
      >
    </div>
    <!-- qr-code -->
    <el-dialog
      custom-class="record-code-dialog"
      :visible.sync="modalIsShow"
      width="280px"
      append-to-body
      destroy-on-close
    >
      <div class="record-close" @click="modalIsShow = false">
        <i class="el-icon-close"></i>
      </div>
      <div class="record-qrcode">
        <vue-qrcode
          :value="curSource.videoUrl"
          :options="{ width: 200, margin: 0 }"
          tag="img"
        ></vue-qrcode>
        <p class="record-tips">请扫描二维码</p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import VueQrcode from "@chenfengyuan/vue-qrcode";
import HlsMedia from "./HlsMedia";

export default {
  name: "StudentMonitorRecord",
  components: { VueQrcode, HlsMedia },
  data() {
    return {
      info: {
        name: "",
        identity: "",
        examName: "",
        courseName: "",
        monitorRecord: [
          {
            videoSource: "",
            videoUrl: "",
          },
        ],
      },
      videoSourceInfo: {
        CLIENT_CAMERA: "电脑摄像头",
        CLIENT_SCREEN: "电脑录屏",
        MOBILE_FIRST: "手机主机位",
        MOBILE_SECOND: "手机辅机位",
      },
      curSource: { videoSource: "", videoUrl: "" },
      modalIsShow: false,
    };
  },
  created() {
    const recordStr = window.sessionStorage.getItem("record");
    if (!recordStr) {
      this.$message.error("数据丢失，请关闭页面！");
      return;
    }
    this.info = JSON.parse(recordStr);
    this.info.monitorRecord = this.info.monitorRecord.map((item) => {
      return {
        ...item,
        monitorKey: this.getVideoMonitorKey(item.videoSource),
      };
    });
    this.switchVideo(this.info.monitorRecord[0]);
  },
  methods: {
    getVideoMonitorKey(videoSource) {
      return Object.keys(this.videoSourceInfo).find((key) =>
        videoSource.includes(key.toLowerCase())
      );
    },
    switchVideo(source) {
      this.curSource = source;
    },
    videoError() {
      this.$message({
        type: "error",
        message: "视频解析错误，您可以尝试扫描二维码，通过手机观看。",
        showClose: true,
        duration: 10 * 1000,
      });
    },
    videoLoad(event) {
      console.log(event);
    },
  },
};
</script>

<style lang="scss" scoped>
.student-monitor-record {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: auto;
  top: 0;
  left: 0;
  background-color: #f0f4f9;
  color: #626a82;
  font-weight: bold;
}
.record-header {
  position: absolute;
  height: 56px;
  width: 100%;
  left: 0;
  top: 0;
  background-color: #fff;
  z-index: 9;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .header-info-name {
    font-size: 18px;
    vertical-align: middle;
  }

  .icon-user-act {
    margin-top: -3px;
    margin-right: 8px;
  }

  .header-info-item {
    padding: 0 12px;
    vertical-align: middle;

    &:not(:last-child) {
      border-right: 1px solid #abb8c9;
    }
  }

  .el-button--info {
    border-color: #f0f4f9;
    background: #f0f4f9;
    border-radius: 6px;
    color: #273a62;

    &:hover {
      border-color: #1886fe;
      background: #1886fe;
      color: #fff;
    }
  }
}
.record-body {
  padding: 76px 20px 70px;
  height: 100%;
}
.record-video {
  background: #000000;
  border-radius: 6px;
  height: 100%;
  overflow: hidden;

  &-none {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-around;
    text-align: center;
    color: #333;

    i {
      font-size: 80px;
    }
  }
}
.record-footer {
  position: absolute;
  width: 100%;
  left: 0;
  bottom: 34px;
  text-align: center;
  .icon {
    margin-right: 10px;
    margin-top: -1px;
  }

  .record-qr {
    color: #1886fe;
    text-decoration: underline;
    font-style: normal;
    cursor: pointer;
    &:hover {
      color: mix(#333, #1886fe, 20%);
    }
  }
}
</style>
<style lang="scss">
.record-code-dialog {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  margin: 0 !important;

  .el-dialog__header,
  .el-dialog__footer {
    display: none;
  }
  .el-dialog__body {
    position: relative;
    padding: 40px 40px 30px 40px;
    text-align: center;
    font-size: 12px;
  }
  .record-close {
    position: absolute;
    height: 20px;
    width: 20px;
    font-size: 16px;
    line-height: 20px;
    top: 8px;
    right: 8px;
    z-index: 9;
    color: #959fb1;
    cursor: pointer;

    &:hover {
      color: #fe5863;
    }
  }
  .record-tips {
    margin: 20px 0 0;
  }
}
</style>
