<!--
* @FileDescription: 登录
* @Author: 黄琪勇
* @Date:2024-03-19 17:13:28
-->
<template>
   <div class="login">
      <div class="loginCon">
         <div class="decoration">
            <img class="xiaoren" src="@/img/loginErr/xiaoren.png" />
            <img class="star" src="@/img/loginErr/star.png" />
            <img class="bStar" src="@/img/loginErr/bStar.png" />
            <img class="music" src="@/img/loginErr/music.png" />
            <img class="yun" src="@/img/loginErr/yun.png" />
         </div>
         <div class="loginBox">
            <img v-show="qrcodeStatus === 2" class="scanned" src="@/img/loginErr/scanned.png" />
            <div v-show="qrcodeStatus !== 2" v-loading="!uuid" class="qrcodeBox">
               <div class="qrCodeCon" :class="{ refresh: qrcodeStatus === 3 }">
                  <qrcode-vue class="qrCode" v-if="uuid" :value="qrcode" />
                  <img @click="handleRefresh" src="@/img/loginErr/refresh.png" />
               </div>
            </div>
            <div class="dsc">
               <img src="@/img/loginErr/jiantou1.png" />
               <div>{{ qrcodeStatus === 2 ? "扫码完成，请在手机上操作" : "使用客户端扫描二维码登录乐教通" }}</div>
               <img src="@/img/loginErr/jiantou2.png" />
            </div>
            <div class="refreshBtn" @click="handleRefresh">{{ qrcodeStatus === 2 ? "重新扫码" : "刷新二维码" }}</div>
         </div>
      </div>
   </div>
</template>

<script setup lang="ts">
import { createQrcodeApi, queryQrcodeStatusApi } from "@/api/user.api"
import { httpAjax } from "@/plugin/httpAjax"
import QrcodeVue from "qrcode.vue"
import { ref, computed } from "vue"
import userStore from "@/store/modules/user"
import { useRouter } from "vue-router"
import useSecureAnth from "@/hooks/useSecureAnth"
import { mutualTLSQuery_gym, mutualTLSQuery_gyt, mutualTLSQuery_klx } from "@/api/user.api"
import { httpAjaxLoading } from "@/plugin/httpAjax"

const userStoreHook = userStore()
const router = useRouter()

const uuid = ref<string>("")
const qrcode = computed(() => {
   return JSON.stringify({ action: "login", uuid: uuid.value })
})
const qrcodeStatus = ref<1 | 2 | 3>(1) // 1 未扫码  2 扫码完成  3 二维码过期
let _time: number

handleCreateQrcode()
function handleCreateQrcode() {
   httpAjax(createQrcodeApi, uuid.value).then(res => {
      if (res.code === 200) {
         uuid.value = res.data.uuid
         handleQrcodeStatus()
      }
   })
}
function handleQrcodeStatus() {
   httpAjax(queryQrcodeStatusApi, uuid.value as string).then(res => {
      if (res.code === 200) {
         const { status } = res.data
         if (status === "FINISH") {
            // 登录成功
            userStoreHook.login(res.data).then(() => {
               const appKey = res.data.appKey as rolesType
               // GYM,GYT,KLX 区分  安全证书
               const mutualTLSQueryApi = {
                  GYT: mutualTLSQuery_gyt,
                  GYM: mutualTLSQuery_gym,
                  KLX: mutualTLSQuery_klx
               }
               httpAjaxLoading(mutualTLSQueryApi[appKey]).then(res => {
                  if (res.code === 511) {
                     useSecureAnth({
                        onCancel() {
                           router.push({ path: "/" })
                        }
                     })
                  } else {
                     router.push({ path: "/" })
                  }
               })
            })
            return
         }
         if (status === "SCANNED") {
            // 扫码完成
            qrcodeStatus.value = 2
         } else if (status === "VERIFIED") {
            // 已验证
            qrcodeStatus.value = 2
         } else if (status === "EXPIRED") {
            // 二维码过期
            qrcodeStatus.value = 3
            return
         } else if (status === "NOT_SCAN") {
            // 未扫码
            qrcodeStatus.value = 1
         }
         _time = setTimeout(() => {
            clearTimeout(_time)
            handleQrcodeStatus()
         }, 3000)
      } else if (res.code === 5440) {
         // 二维码过期
         qrcodeStatus.value = 3
      }
   })
}
function handleRefresh() {
   _time && clearTimeout(_time)
   handleCreateQrcode()
}
</script>

<style lang="scss" scoped>
.login {
   width: 100%;
   height: 100%;
   min-height: 640px;
   background: url("@/img/loginErr/bg.png") no-repeat;
   background-size: cover;
   display: flex;
   align-items: center;
   justify-content: center;
   .loginCon {
      width: 74%;
      display: flex;
      justify-content: space-between;
   }
   .loginBox {
      width: 440px;
      height: 498px;
      background: url("@/img/loginErr/loginBox.png") no-repeat;
      background-size: cover;
      z-index: 2;
      display: flex;
      flex-direction: column;
      align-items: center;
      .scanned {
         margin-top: 132px;
         width: 114px;
         height: 189px;
      }
      .qrcodeBox {
         margin-top: 154px;
         width: 188px;
         height: 188px;
         display: flex;
         justify-content: center;
         align-items: center;
         background: url("@/img/loginErr/border.png") no-repeat;
         background-size: cover;
         .qrCodeCon {
            position: relative;
            &.refresh {
               background-color: rgba(255, 255, 255, 0.8);
               > .qrCode {
                  opacity: 0.3;
               }
               > img {
                  cursor: pointer;
                  position: absolute;
                  left: 50%;
                  top: 50%;
                  transform: translate(-50%, -50%);
                  display: block;
               }
            }
            .qrCode {
               width: 164px !important;
               height: 164px !important;
            }
            > img {
               width: 68px;
               height: 68px;
               display: none;
            }
         }
      }
      .dsc {
         margin-top: 28px;
         display: flex;
         justify-content: center;
         align-items: center;
         > div {
            width: 240px;
            text-align: center;
            font-size: 16px;
            color: #777;
            margin: 0 14px;
         }
         > img {
            width: 33px;
            height: 18px;
         }
      }
      .refreshBtn {
         margin-top: 14px;
         line-height: 22px;
         font-size: 16px;
         color: #f67146;
         display: inline;
         font-weight: bold;
         cursor: pointer;
         &:hover {
            opacity: $opacity-hover;
         }
      }
   }
   .decoration {
      margin-top: -8px;
      position: relative;
      z-index: 1;
      width: 523px;
      height: 412px;
      .xiaoren {
         position: absolute;
         top: 0;
         left: 0;
         width: 100%;
         height: 100%;
         z-index: 1;
      }
      .star {
         position: absolute;
         right: 0;
         top: 0;
         width: 26px;
         height: 38px;
      }
      .bStar {
         position: absolute;
         left: -20px;
         top: 178px;
         width: 60px;
         height: 58px;
      }
      .music {
         position: absolute;
         right: -26px;
         top: 24px;
         width: 28px;
         height: 56px;
      }
      .yun {
         position: absolute;
         width: 346px;
         height: 240px;
         bottom: -160px;
         left: 40px;
         z-index: 0;
      }
   }
}
</style>
