<template>
  <div class="realtime-monitoring">
    <div class="realtime-top clear-float">
      <p :title="curExamBatch.roomName">
        考场名称：{{ curExamBatch.roomName }}
      </p>
      <p class="realtime-top-select" @click="$refs.ExamBatchDialog.open()">
        <span>{{ curExamBatch.label || "请选择批次" }}</span>
        <i class="el-icon-caret-bottom"></i>
      </p>
      <text-clock></text-clock>
    </div>

    <div class="part-box-head">
      <div class="part-box-head-left">
        <h1>实时监控台</h1>
      </div>
      <div class="part-box-head-right">
        <div
          :class="[
            'realtime-switch',
            { 'realtime-switch-warning': hasNewWarning },
          ]"
        >
          <div
            :class="[
              'realtime-switch-item',
              { 'realtime-switch-item-act': pageType === '0' },
            ]"
            @click="pageTypeChange('0')"
          >
            <i class="el-icon-s-fold"></i><span>列表</span>
          </div>
          <div
            :class="[
              'realtime-switch-item',
              { 'realtime-switch-item-act': pageType === '1' },
            ]"
            @click="pageTypeChange('1')"
          >
            <i class="el-icon-video-camera"></i><span>视频</span>
          </div>
        </div>
      </div>
    </div>

    <div class="part-filter">
      <div class="part-filter-info">
        <summary-line
          class="part-filter-info-main"
          data-type="trouble"
          :exam-id="filter.examId"
          ref="SummaryLine"
        ></summary-line>
        <div class="part-filter-info-sub">
          <el-badge
            :value="communicationCount"
            :max="99"
            :hidden="!communicationCount"
          >
            <el-button
              icon="icon icon-ring"
              type="success"
              @click="toCommunication"
              >通话待办</el-button
            >
          </el-badge>
        </div>
      </div>

      <div class="part-filter-form">
        <el-form ref="FilterForm" label-position="left" inline>
          <el-form-item>
            <el-select
              v-model="filter.paperDownload"
              placeholder="试题下载"
              clearable
            >
              <el-option
                v-for="(val, key) in BOOLEAN_INVERSE_TYPE"
                :key="key"
                :value="key * 1"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="filter.status" placeholder="考试状态" clearable>
              <el-option
                v-for="(val, key) in STUDENT_ONLINE_STATUS"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.monitorStatusSource"
              placeholder="通讯故障"
              clearable
            >
              <el-option
                v-for="(val, key) in BOOLEAN_TYPE"
                :key="key"
                :value="key * 1"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="预警量">
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.minWarningCount"
              placeholder="下限"
              :controls="false"
            ></el-input-number>
            <span class="line-split">-</span>
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.maxWarningCount"
              placeholder="上限"
              :controls="false"
            ></el-input-number>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="filter.name"
              placeholder="姓名"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="filter.identity"
              placeholder="证件号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toSearch">查询</el-button>
          </el-form-item>
        </el-form>

        <div class="part-filter-form-action">
          <el-dropdown
            @command="viewingAngleChange"
            style="margin-right: 10px;"
            trigger="click"
            v-if="pageType === '1'"
          >
            <el-button type="primary"
              >切换视频源<i class="el-icon-arrow-down el-icon--right"></i
            ></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="item in viewingAngles"
                :key="item.code"
                :command="item"
              >
                <span
                  :class="{
                    'color-primary': item.code === curViewingAngle.code,
                  }"
                >
                  {{ item.name }}
                </span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <el-button
            type="primary"
            icon="icon icon-handle"
            @click="finishInvigilation"
            >手动收卷</el-button
          > -->
          <el-button
            type="danger"
            icon="icon icon-over"
            @click="finishInvigilationExam"
            >结束监考</el-button
          >
        </div>
      </div>
    </div>

    <el-table
      ref="TableList"
      :data="dataList"
      @selection-change="handleSelectionChange"
      v-if="pageType === '0'"
    >
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column prop="identity" label="证件号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="courseName" label="科目名称"></el-table-column>
      <el-table-column prop="courseCode" label="科目代码"></el-table-column>
      <el-table-column prop="remainTime" label="剩余时间"></el-table-column>
      <el-table-column prop="paperDownload" label="试题下载">
        <span slot-scope="scope">
          {{ BOOLEAN_INVERSE_TYPE[scope.row.paperDownload] }}
        </span>
      </el-table-column>
      <el-table-column prop="status" label="考试状态"></el-table-column>
      <el-table-column prop="progress" label="进度">
        <span slot-scope="scope">{{ scope.row.progress }}%</span>
      </el-table-column>
      <el-table-column prop="clientWebsocketStatus" label="通讯">
        <template slot-scope="scope">
          <right-or-wrong
            :status="CLIENT_WEBSOCKET_STATUS[scope.row.clientWebsocketStatus]"
          ></right-or-wrong>
        </template>
      </el-table-column>
      <el-table-column
        v-for="source in viewingAngles"
        :key="source.param"
        :prop="source.param"
        :label="`${source.name}通讯`"
      >
        <template slot-scope="scope">
          <right-or-wrong
            :status="MONITOR_STATUS_SOURCE[scope.row[source.param]]"
          ></right-or-wrong>
        </template>
      </el-table-column>
      <el-table-column
        prop="clientCurrentIp"
        label="IP"
        v-if="curExamBatch.enableIpLimit"
      >
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间">
        <span slot-scope="scope">
          {{ scope.row.updateTime | datetimeFilter }}
        </span>
      </el-table-column>
      <el-table-column prop="warningCount" label="预警数"></el-table-column>
      <el-table-column prop="breachStatus" label="违纪">
        <template slot-scope="scope">
          <span :class="{ 'color-danger': !scope.row.breachStatus }">
            {{ !scope.row.breachStatus ? "违纪" : "正常" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="125" fixed="right">
        <template slot-scope="scope">
          <el-button
            :class="[
              'btn-table-icon',
              { 'warn-new-tips': scope.row.warningNew },
            ]"
            type="primary"
            icon="icon icon-view"
            @click="toDetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="invigilation-student-list" v-else>
      <div
        class="invigilation-student-item"
        v-for="item in dataList"
        :key="item.examRecordId"
      >
        <invigilation-student :data="item"></invigilation-student>
      </div>
    </div>

    <div class="part-page">
      <el-pagination
        background
        hide-on-single-page
        layout="prev, pager, next,total,jumper"
        :current-page="current"
        :total="total"
        :page-size="size"
        @current-change="toPage"
      >
      </el-pagination>
    </div>

    <!-- 考试批次选择 -->
    <exam-batch-dialog
      :initExamid="initExamid"
      @confirm="examChange"
      ref="ExamBatchDialog"
    ></exam-batch-dialog>
    <!-- 手动收卷 -->
    <handle-rollup-dialog
      :data-list="multipleSelection"
      @modified="rollupOver"
      ref="handleRollupDialog"
    ></handle-rollup-dialog>
  </div>
</template>

<script>
import {
  invigilateVideoList,
  invigilateExamFinish,
  monitorCallCount,
  invigilationWarningMessage,
} from "@/api/invigilation";
import ExamBatchDialog from "./ExamBatchDialog";
import RightOrWrong from "../common/RightOrWrong";
import InvigilationStudent from "../common/InvigilationStudent";
import SummaryLine from "../common/SummaryLine";
import handleRollupDialog from "./handleRollupDialog";
import TextClock from "../common/TextClock";
import {
  BOOLEAN_TYPE,
  VIDEO_SOURCE_TYPE,
  BOOLEAN_INVERSE_TYPE,
  STUDENT_ONLINE_STATUS,
  CLIENT_WEBSOCKET_STATUS,
  MONITOR_STATUS_SOURCE,
} from "@/constant/constants";
import { mapState, mapMutations, mapActions } from "vuex";

export default {
  name: "realtime-monitoring",
  components: {
    ExamBatchDialog,
    RightOrWrong,
    InvigilationStudent,
    SummaryLine,
    handleRollupDialog,
    TextClock,
  },
  data() {
    return {
      initExamid: this.$route.params.examId,
      initRoomCode: this.$route.params.roomCode,
      filter: {
        examId: "",
        paperDownload: null,
        status: null,
        monitorStatusSource: null,
        monitorVideoSource: null,
        name: null,
        identity: null,
        maxWarningCount: undefined,
        minWarningCount: undefined,
      },
      BOOLEAN_TYPE,
      VIDEO_SOURCE_TYPE,
      BOOLEAN_INVERSE_TYPE,
      STUDENT_ONLINE_STATUS,
      CLIENT_WEBSOCKET_STATUS,
      MONITOR_STATUS_SOURCE,
      hasNewWarning: false,
      loopRunning: false,
      loopSetTs: [],
      communicationCount: 0,
      curExamBatch: {},
      curViewingAngle: {},
      current: 1,
      total: 0,
      size: 24,
      multipleSelection: [],
      batchId: "",
      batchs: [],
      exams: [],
      subjects: [],
      pageType: "0",
      dataList: [],
      videoSourceStatusParams: {
        CLIENT_CAMERA: "cameraMonitorStatusSource",
        CLIENT_SCREEN: "screenMonitorStatusSource",
        MOBILE_FIRST: "mobileFirstMonitorStatusSource",
        MOBILE_SECOND: "mobileSecondMonitorStatusSource",
      },
      viewingAngles: [],
    };
  },
  created() {
    window.inviligateWarning = (id) => {
      this.toDetail({ examRecordId: id });
    };
  },
  computed: {
    ...mapState("invigilation", ["liveDomains"]),
  },
  methods: {
    ...mapActions("invigilation", ["updateDetailIds"]),
    ...mapMutations("invigilation", ["setDetailIds"]),
    clearLoopSetTs() {
      if (!this.loopSetTs.length) return;
      this.loopSetTs.forEach((sett) => {
        clearTimeout(sett);
      });
      this.loopSetTs = [];
    },
    async timerUpdatePage() {
      this.clearLoopSetTs();
      if (!this.loopRunning) return;

      let fetchAll = [this.getList()];
      if (this.$refs.SummaryLine)
        fetchAll.push(this.$refs.SummaryLine.initData());

      fetchAll.push(this.getMonitorCallCount());
      fetchAll.push(this.fetchWarningNotice());

      await Promise.all(fetchAll).catch(() => {});

      this.loopSetTs.push(
        setTimeout(() => {
          this.timerUpdatePage();
        }, 10 * 1000)
      );
    },
    examChange(examBatch) {
      if (!examBatch) return;
      this.filter.examId = examBatch.id;
      this.curExamBatch = examBatch;
      if (examBatch.monitorVideoSource) {
        this.viewingAngles = examBatch.monitorVideoSource
          .split(",")
          .map((item) => {
            return {
              code: item,
              name: this.VIDEO_SOURCE_TYPE[item],
              param: this.videoSourceStatusParams[item],
            };
          });
      } else {
        this.viewingAngles = [];
      }
      this.curViewingAngle = this.viewingAngles[0] || {};
      this.filter.monitorVideoSource = this.curViewingAngle.code || "";

      this.toSearch();

      this.getMonitorCallCount();
      this.fetchWarningNotice();

      // 正在考试的考试，开启定时更新；
      if (examBatch.isExaming) {
        this.loopRunning = true;
        this.clearLoopSetTs();
        this.loopSetTs.push(
          setTimeout(() => {
            this.timerUpdatePage();
          }, 10 * 1000)
        );
      } else {
        this.loopRunning = false;
        this.clearLoopSetTs();
      }
    },
    pageTypeChange(pageType) {
      this.pageType = pageType;
      this.multipleSelection = [];
    },
    viewingAngleChange(data) {
      if (data.code === this.curViewingAngle.code) return;
      this.curViewingAngle = data;
      this.filter.monitorVideoSource = data.code;
      this.dataList = [];
      this.getList();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };

      const res = await invigilateVideoList(datas);
      const domainLen = this.liveDomains.length;
      this.dataList = res.data.data.records.map((item, index) => {
        const domain = domainLen ? this.liveDomains[index % domainLen] : "";
        item.label = `${item.identity} ${item.courseName}(${item.courseCode}) ${item.name}`;
        item.liveUrl = item.monitorLiveUrl
          ? `${domain}/live/${item.monitorLiveUrl.toLowerCase()}.flv`
          : "";
        item.progress = item.progress ? Math.round(item.progress * 100) : 0;
        return item;
      });

      this.hasNewWarning = this.dataList.some((item) => item.warningNew);
      this.total = res.data.data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toSearch() {
      this.current = 1;
      await this.getList();

      if (this.total > this.size) {
        this.updateDetailIds({
          filterData: this.filter,
          fetchFunc: invigilateVideoList,
        });
      } else {
        const ids = this.dataList.map((item) => item.examRecordId);
        this.setDetailIds([...new Set(ids)]);
      }
    },
    async getMonitorCallCount() {
      if (!this.filter.examId) return;
      const res = await monitorCallCount({
        examId: this.filter.examId,
        callStatus: "START",
      });
      this.communicationCount = res.data.data.count || 0;
    },
    async fetchWarningNotice() {
      if (!this.filter.examId) return;
      let noticeCaches = {};
      const showAlert = async (item) => {
        return new Promise((resolve) => {
          setTimeout(() => {
            this.$notify({
              duration: 5 * 1000,
              dangerouslyUseHTMLString: true,
              customClass: "msg-monitor-magbox",
              position: "bottom-right",
              offset: 50,
              message: `
                <div class="msg-monitor">
                  <span class="msg-monitor-icon"><i class="icon icon-warning"></i></span>
                  <span>注意：<b>${item.name}</b>发现异常，</span>
                  <span class="msg-monitor-action" onclick="window.inviligateWarning('${item.examRecordId}')">立即处理</span>
                </div>
              `,
            });
            resolve();
          }, 200);
        });
      };

      const res = await invigilationWarningMessage(this.filter.examId);
      for (let i = 0, len = res.data.data.length; i < len; i++) {
        const item = res.data.data[i];
        const stdKey = item.examRecordId;
        if (!noticeCaches[stdKey]) {
          noticeCaches[stdKey] = item;
          await showAlert(item);
        }
      }
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
    async finishInvigilation() {
      if (!this.multipleSelection.length) {
        this.$message.error("请先选择数据！");
        return;
      }
      this.$refs.handleRollupDialog.open();
    },
    rollupOver() {
      this.multipleSelection = [];
      this.getList();
    },
    async finishInvigilationExam() {
      const result = await this.$confirm(
        "确定要结束监考吗?",
        "结束监考确认提醒",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          iconClass: "el-icon-warning",
          customClass: "el-message-box__error",
        }
      ).catch(() => {});

      if (!result) return;

      await invigilateExamFinish(this.filter.examId);
      this.$refs.ExamBatchDialog.getExamList();
      this.$message({
        type: "success",
        message: "操作成功!",
      });
    },
    toCommunication() {
      this.$router.push({
        name: "VideoCommunication",
        params: {
          examId: this.filter.examId,
        },
      });
    },
    toDetail(row) {
      this.$router.push({
        name: "WarningDetail",
        params: { recordId: row.examRecordId },
      });
    },
  },
  beforeDestroy() {
    this.loopRunning = false;
    this.clearLoopSetTs();
    delete window.inviligateWarning;
  },
};
</script>

<style lang="scss" scoped>
.realtime-top {
  position: relative;
  padding: 9px 20px 9px 73px;
  background: rgba(24, 134, 254, 1);
  border-radius: 6px;
  color: #fff;
  margin-bottom: 30px;
  line-height: 32px;

  &::before {
    content: "";
    display: block;
    position: absolute;
    width: 59px;
    height: 49px;
    left: 13px;
    top: 0;
    background-image: url(../../../assets/bg-stars.png);
    background-size: 100% 100%;
  }

  > p {
    float: left;
    margin: 0;
  }
  > p:first-child {
    margin-right: 40px;
    min-width: 150px;
    max-width: 300px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .realtime-top-select {
    display: inline-block;
    position: relative;
    height: 32px;
    line-height: 32px;
    border-radius: 6px;
    min-width: 200px;
    background-color: #fff;
    color: #1886fe;
    padding: 0 26px 0 12px;
    cursor: pointer;

    > i {
      position: absolute;
      right: 8px;
      top: 9px;
    }
  }
  .text-clock {
    float: right;
    font-size: 12px;
    opacity: 0.8;
  }
}
.realtime-switch {
  font-size: 0;
  &-warning {
    .realtime-switch-item {
      &::before {
        content: "";
        display: block;
        position: absolute;
        width: 10px;
        height: 10px;
        top: -5px;
        right: -5px;
        border-radius: 50%;
        border: 2px solid #fff;
        background: #fe5863;
        z-index: 9;
      }
    }
  }

  &-item {
    display: inline-block;
    vertical-align: top;
    font-size: 12px;
    color: #8c94ac;
    background: #fff;
    line-height: 18px;
    padding: 5px 14px;
    position: relative;
    cursor: pointer;

    > i {
      margin-right: 5px;
    }
    &:first-child {
      border-radius: 6px 0px 0px 6px;
    }
    &:last-child {
      border-radius: 0px 6px 6px 0px;
    }

    &-act {
      color: #fff;
      background: #5fc9fa;
    }
  }
}
.invigilation-student-list {
  background: #ffffff;
  border-radius: 6px;
  padding: 10px 10px;
  font-size: 0;
  min-height: 200px;
  .invigilation-student-item {
    font-size: 14px;
    display: inline-block;
    vertical-align: top;
    padding: 10px;
    width: 25%;
  }
}
.warn-new-tips {
  position: relative;

  &::after {
    content: "";
    display: block;
    position: absolute;
    width: 32px;
    height: 16px;
    right: -32px;
    top: 0;
    background-image: url(../../../assets/icon-new-tips.png);
    background-size: 100% 100%;
  }
}
</style>
