<template>
  <div class="login-ways h-full">
    <div class="flex flex-col h-full justify-between">
      <div class="level1">
        <div class="title">登录</div>
        <div class="tip">选择登录身份</div>
        <div class="flex items-center justify-between">
          <div
            class="card"
            @click="activeName = 'scan'"
            :class="{ active: activeName === 'scan' }"
          >
            <img src="../../assets/imgs/scan_login_icon.png" />
            <p class="way-name">扫描员登录</p>
            <p class="way-desc">Scanner login</p>
          </div>
          <div
            class="card"
            @click="activeName = 'admin'"
            :class="{ active: activeName === 'admin' }"
          >
            <img src="../../assets/imgs/admin_login_icon.png" />
            <p class="way-name">管理员登录</p>
            <p class="way-desc">Administrator login</p>
          </div>
        </div>
      </div>
      <div class="level2">
        <div class="text-right">
          <qm-button type="primary" @click="nextStep"
            >下一步 <template #icon> <SwapRightOutlined /> </template
          ></qm-button>
        </div>
        <footer-info></footer-info>
      </div>
    </div>
    <qm-button
      :icon="h(GlobalOutlined)"
      class="reset-btn"
      @click="emit('toIndex', 0)"
    >
      连接设置</qm-button
    >
  </div>
</template>
<script name="LoginWays" lang="ts" setup>
import { ref, h } from "vue";
import {
  CheckCircleFilled,
  CloseCircleFilled,
  SwapRightOutlined,
  GlobalOutlined,
} from "@ant-design/icons-vue";
import FooterInfo from "@/components/FooterInfo/index.vue";
import { local } from "@/utils/tool";
import { scanLogin } from "@/ap/system";
import { useAppStore } from "@/store";

const emit = defineEmits(["next", "toIndex"]);
const activeName = ref("scan");

const nextStep = async () => {
  if (activeName.value === "scan") {
    let deviceName = window.electronApi?.getComputerName?.();
    let device = await window.electronApi?.getUuid?.();
    let res = await scanLogin({ device, deviceName });
    console.log("扫描员登录成功", res);
    let baseUrl = local.get("baseUrl");
    let ipArr = baseUrl.split("//")[1].split(":");
    window.electronApi.startScanExe([
      ...ipArr,
      res.sessionId,
      res.accessToken,
      window.devicePixelRatio,
    ]);
    const appStore = useAppStore();
    appStore.loadingStatus = {
      spinning: true,
      tip: "扫描端启动中...",
    };
    setTimeout(() => {
      //如果10秒了扫描端还没启动起来，估计有异常情况，于是关掉全屏loading
      appStore.loadingStatus = {
        spinning: false,
        tip: "",
      };
    }, 10000);
    //同时监测进程，如果发现扫描端启动了，立刻关掉全屏loading，且关掉本项目
    try {
      let result = await window.electronApi?.watchScanProcess();
      if (result) {
        window.close();
      }
    } catch (err: any) {
      appStore.loadingStatus = {
        spinning: false,
        tip: "",
      };
      window.$message.error("扫描端启动异常！请检查扫描端文件！");
    }
  } else {
    emit("next");
  }
};
</script>
<style lang="less" scoped>
.login-ways {
  padding-top: 120px;
  position: relative;
  :deep(.reset-btn) {
    position: absolute;
    top: 25px;
    right: 65px;
    z-index: 1;
  }
  .level2 {
    .text-right {
      padding-right: 24px;
      padding-bottom: 24px;
    }
  }
  .level1 {
    padding: 0 24px;
    .card {
      border-radius: 6px;
      border: 1px solid @border-color1;
      height: 140px;
      width: calc(50% - 12px);
      text-align: center;
      cursor: pointer;
      transition: all 0.3s;
      &.active {
        background: #f3f4f6;
      }
      img {
        height: 35px;
        margin-top: 26px;
      }
      .way-name {
        font-size: 16px;
        font-weight: bold;
        color: #14161a;
        margin-top: 12px;
      }
      .way-desc {
        font-size: 12px;
        color: @text-color3;
        margin-top: 10px;
      }
    }
    .title {
      color: @text-color1;
      font-weight: bold;
      line-height: 32px;
      font-size: 24px;
    }
    .tip {
      color: @text-color3;
      line-height: 26px;
      font-size: 18px;
      margin-top: 4px;
      margin-bottom: 24px;
    }
  }
}
</style>
