<template>
  <v-app>
    <v-overlay style="width: 100%;" class="loading_bg text-center" :model-value="true" opacity="0" persistent>
      <v-progress-circular v-show="valid == null" indeterminate size="64" color="blue"></v-progress-circular>
      <v-btn v-if="valid == false" class="pa-12" variant="outlined" size="large" icon color="red" elevation="0">
        <v-icon size="72" style="padding-bottom: 70px;padding-right: 6px;">mdi-account-multiple-remove-outline</v-icon>
      </v-btn>
      <v-btn v-if="valid == true" class="pa-12" variant="outlined" size="large" icon color="green" elevation="0">
        <v-icon size="72" style="padding-bottom: 70px;padding-right: 6px;">mdi-account-multiple-check-outline</v-icon>
      </v-btn>
      <div class="mt-6 font-weight-black text-h6 text-black" :class="valid == false ? 'text-red' : ''">{{ note }}</div>
    </v-overlay>
  </v-app>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'

const { proxy } = getCurrentInstance()
const router = useRouter()

const valid = ref(null)
const note = ref("授权登录中...")

onMounted(() => {
  alert('11111')
  const stoken = GetQueryString("stoken")
  if (stoken) {
    UserLoginByData("mis", { stoken: stoken })
    return
  }
  const uid = GetQueryString("uid")
  if (uid) {
    UserLoginByData("frdb", { uid: uid })
    return
  }

  if (!stoken && !uid) {
    valid.value = false
    note.value = "授权登录错误，用户令牌不能为空，请关闭页面后重试！"
  }
})

const GetQueryString = (name) => {
  const reg = new RegExp("[?&]" + name + "=([^&^#^/]*)", "i")
  const res = window.location.href.match(reg)
  return res != null ? decodeURIComponent(res[1]) : ""
}

const UserLoginByData = (type, data) => {
  proxy.$vtk.request.postJson(`/bizp/admin/login/${type}`, data).then((res) => {
    if (res.meta.success && res.data) {
      valid.value = true
      note.value = "授权登录成功，正在跳转请稍后..."
      const users = res.data.users
      proxy.$vtk.storage.set("_mis_acis_users", users)
      proxy.$vtk.storage.set("_mis_acis_token", res.data.token)
      setTimeout(() => {
        router.push("/example/home")
      }, 500)
    } else {
      valid.value = false
      note.value = "授权登录失败，请关闭页面后重试！"
    }
  })
}
</script>

<style scoped>
.loading_bg {
  background-image: url("../../assets/img/jz.jpg") !important;
  background-size: cover;
}

:deep(.v-overlay__content) {
  position: absolute !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
}
</style>
