<template>
  <div
    id="app"
    onpaste="return false"
    oncopy="return false"
    oncut="return false"
  >
    <router-view></router-view>
    <video id="ssVideo" style="display: none"></video>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from "vuex";
import { nodeCheckProcess } from "./utils/nativeExe";
import { isElectron } from "./utils/util";
const { mapMutations } = createNamespacedHelpers("examingHomeModule");

export default {
  name: "APP",
  computed: {
    ...mapState(["user", "QECSConfig"]),
  },
  watch: {
    QECSConfig() {
      // console.log(this.$store.state.QECSConfig.PREVENT_CHEATING_CONFIG);
      if (this.QECSConfig.PREVENT_CHEATING_CONFIG.includes("FULL_SCREEN_TOP")) {
        if (typeof nodeRequire == "undefined") {
          console.log("非考生端，不检测置顶状态");
          this.logger({
            action: "非考生端，不检测置顶状态",
          });
          return;
        }
        console.log("考生端，设置检测置顶状态");
        this.logger({
          action: "考生端，设置检测置顶状态",
        });
        let remote = window.nodeRequire("electron").remote;
        // const { electron, dialog } = remote;
        // console.log(remote);
        // Module to control application life.
        // const app = remote.app;
        // const dialog = remote.dialog;
        const mainWindow = remote.getCurrentWindow();
        let lastBlurTimeStamp = 0;
        const blurHandler = () => {
          if (Date.now() - lastBlurTimeStamp < 1000) {
            // 小于1秒的切屏不计算在内，快捷键alt+tab容易连续触发两次
            return;
          }
          lastBlurTimeStamp = Date.now();
          // console.log("blur...");
          // dialog.showErrorBox("学生端不是置顶状态", "退出学生端!");
          // app.exit(0);

          this.logger({
            page: window.location.pathname,
            action: "学生端不是置顶状态",
          });
          window._hmt.push([
            "_trackEvent",
            window.location.pathname
              .replace(/=\w*/g, "=")
              .replace(/\d+/g, "{id}"),
            "学生端不是置顶状态",
          ]);
          if (this.$route.name === "OnlineExamingHome") {
            this.$Message.warning({
              content: "考试过程请勿尝试切屏，可能影响考试成绩",
              duration: 10,
              closable: true,
            });
            this.$http
              .post(
                "/api/ecs_oe_student/examControl/switchScreen?examRecordDataId=" +
                  this.$route.params.examRecordDataId
              )
              .then((res) => {
                if (
                  this.QECSConfig.PREVENT_CHEATING_CONFIG.includes(
                    "RECORD_SWITCH_SCREEN"
                  )
                ) {
                  if (res.data) {
                    // console.log(res.data);
                    const { switchScreenCount, maxSwitchScreenCount } =
                      res.data;
                    if (
                      typeof switchScreenCount === "number" &&
                      typeof maxSwitchScreenCount === "number"
                    ) {
                      if (switchScreenCount > maxSwitchScreenCount) {
                        this.updateExceedSwitchCount(true);
                      }
                    }
                  }
                }
              });
          }
          return;

          // const redirectUrl = sessionStorage.getItem("redirectUrl");
          // this.logger({
          //   action: "学生端不是置顶状态--退出",
          // });
          // console.log(`学生端不是置顶状态--退出`);
          // this.$Modal.error({
          //   title: "学生端不是置顶状态",
          //   content: "退出考试",
          //   onOk: () => {
          //     if (redirectUrl) {
          //       window.location = redirectUrl;
          //     } else {
          //       this.$router.push(
          //         "/login/" +
          //           localStorage.getItem("domain") +
          //           "?LogoutReason=学生端不是置顶状态--退出"
          //       );
          //     }
          //   },
          // });
        };
        mainWindow.removeListener("blur", blurHandler);
        mainWindow.on("blur", blurHandler);
      }
    },
  },
  async created() {
    this.interval = setInterval(() => {
      this.sendOnlineSignal();
    }, 3 * 60 * 1000);
    if (isElectron()) {
      nodeCheckProcess();
      this.processInterval = setInterval(
        () => nodeCheckProcess(),
        2 * 60 * 1000
      );
    }
  },
  beforeDestroy() {
    clearInterval(this.interval);
    clearInterval(this.processInterval);
  },
  methods: {
    ...mapMutations(["updateExceedSwitchCount"]),
    async sendOnlineSignal() {
      if (!window.sessionStorage.getItem("token")) {
        return;
      }
      try {
        await this.$http.get(
          "/api/ecs_core/student/online_signal/" + this.user.id
        );
      } catch (error) {
        this.logger({
          page: window.location.pathname,
          action: "发送在线信号",
          userId: this.user.id,
        });
      }
    },
  },
};
</script>

<style>
#app {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  font-size: 14px;
  font-weight: normal;
  font-stretch: normal;
  /* line-height: 20px; */
  color: #777777;
}
</style>
