<template>
  <div class="realtime-monitoring">
    <div class="realtime-top clear-float">
      <p>考场名称：{{ curExamBatch.name }}</p>
      <div
        class="el-select el-select--small"
        @click="$refs.ExamBatchDialog.open()"
      >
        <div class="el-input el-input--small el-input--suffix">
          <div class="el-input__inner">{{ curExamBatch.label }}</div>
          <span class="el-input__suffix">
            <span class="el-input__suffix-inner"
              ><i
                class="el-select__caret el-input__icon el-icon-arrow-up"
              ></i></span
          ></span>
        </div>
      </div>
      <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"
        ></summary-line>
        <div class="part-filter-info-sub">
          <el-button
            icon="el-icon-phone-outline"
            type="success"
            @click="toCommunication"
            >通话待办</el-button
          >
        </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="item in batchs"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="filter.status" placeholder="考试状态" clearable>
              <el-option
                v-for="item in exams"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.monitorStatusSource"
              placeholder="通讯故障"
              clearable
            >
              <el-option
                v-for="item in exams"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></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.identity"
              placeholder="姓名/证件号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toPage(1)">查询</el-button>
          </el-form-item>
        </el-form>

        <div class="part-filter-form-action">
          <el-dropdown
            @command="viewingAngleChange"
            style="margin-right: 10px;"
            v-if="pageType === '1'"
          >
            <el-button type="primary"
              >{{ curViewingAngle.name || "切换视角"
              }}<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"
                >切换{{ item.name }}</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="subjectCode" label="剩余时间"></el-table-column>
      <el-table-column prop="paperDownload" label="试题下载"></el-table-column>
      <el-table-column prop="status" label="考试状态"></el-table-column>
      <el-table-column prop="progress" label="进度"></el-table-column>
      <el-table-column
        prop="clientCommunicationStatus"
        label="通讯"
      ></el-table-column>
      <el-table-column prop="subjectCode" label="推流通讯"></el-table-column>
      <el-table-column prop="clientCurrentIp" label="IP"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间"></el-table-column>
      <el-table-column prop="warningCount" label="预警数"></el-table-column>
      <el-table-column prop="breachStatus" label="违纪"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            class="btn-table-icon"
            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 videoList"
        :key="item.examStudentId"
      >
        <invigilation-student :data="item"></invigilation-student>
      </div>
    </div>

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

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

<script>
import {
  invigilateList,
  invigilateVideoList,
  invigilateExamFinish,
} from "@/api/invigilation";
import ExamBatchDialog from "./ExamBatchDialog";
import InvigilationStudent from "../common/InvigilationStudent";
import SummaryLine from "../common/SummaryLine";
import handleRollupDialog from "./handleRollupDialog";
import TextClock from "../common/TextClock";

export default {
  name: "realtime-monitoring",
  components: {
    ExamBatchDialog,
    InvigilationStudent,
    SummaryLine,
    handleRollupDialog,
    TextClock,
  },
  data() {
    return {
      filter: {
        examId: "",
        paperDownload: null,
        status: null,
        monitorStatusSource: null,
        identity: null,
        maxWarningCount: undefined,
        minWarningCount: undefined,
      },
      hasNewWarning: false,
      curExamBatch: {},
      curViewingAngle: {},
      current: 1,
      total: 0,
      size: 10,
      multipleSelection: [],
      batchId: "",
      batchs: [],
      exams: [],
      subjects: [],
      pageType: "0",
      dataList: [
        {
          breachStatus: 0,
          clientCommunicationStatus: "12",
          clientCurrentIp: "192.168.10.12",
          courseCode: "F001",
          courseName: "数学",
          examActivityId: 0,
          examId: 111,
          examRecordId: 222,
          examStudentId: 333,
          identity: "000000000000008",
          monitorStatusSource: "",
          name: "楚一一",
          paperDownload: 0,
          progress: 0,
          roomCode: "123",
          roomName: "第一教师",
          status: "1",
          statusCode: "1",
          updateTime: "2020-12-12",
          warningCount: 0,
        },
      ],
      videoList: [],
      viewingAngles: [
        {
          code: "1",
          name: "第一视角",
        },
        {
          code: "2",
          name: "第二视角",
        },
        {
          code: "3",
          name: "第三视角",
        },
      ],
    };
  },
  mounted() {},
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current - 1,
        pageSize: this.size,
      };

      let res = null;
      if (this.pageType === "0") {
        res = await invigilateList(datas);
        this.dataList = res.data.data.records.map((item) => {
          item.label = `${item.identity} ${item.courseName}(${item.courseCode}) ${item.name}`;
          return item;
        });
      } else {
        res = await invigilateVideoList(datas);
        this.videoList = res.data.data.records;
      }

      this.total = res.data.data.records.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    examChange(examBatch) {
      if (!examBatch) return;
      this.filter.examId = examBatch.id;
      this.curExamBatch = examBatch;
      // this.toPage(1);
    },
    pageTypeChange(pageType) {
      this.pageType = pageType;
      this.toPage(1);
      this.multipleSelection = [];
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
    viewingAngleChange(data) {
      this.curViewingAngle = data;
    },
    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: "取消",
        type: "confirm",
      }).catch(() => {});

      if (!result) return;

      await invigilateExamFinish();
      this.toPage(1);
      this.$message({
        type: "success",
        message: "操作成功!",
      });
    },
    toCommunication() {
      this.$router.push({ name: "VideoCommunication" });
    },
    toDetail(row) {
      console.log(row);
      this.$router.push({
        name: "WainingDetail",
        params: { recordId: row.examRecordId },
      });
    },
  },
};
</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;

  &::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%;
  }

  > * {
    float: left;
    line-height: 32px;
    margin: 0;
  }
  .el-select {
    min-width: 200px;
  }
  > p:first-child {
    margin-right: 40px;
    min-width: 150px;
  }
  > p:last-child {
    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%;
  }
}
</style>
