<template>
    <div v-loading="loading" class="hc-body-loading" element-loading-text="授权登录中...">
        <div v-if="isErrorShow" class="error-page">
            <div :style="`background-image: url(${svg403});`" class="img" />
            <div class="content">
                <h1>403</h1>
                <div class="desc">抱歉，token授权登录异常，请重新进入</div>
                <div class="actions">
                    <el-button block size="large" type="primary" @click="toLoginTap">手动登录</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useAppStore } from '~src/store'
import { getTenantID, loginByToken5 } from '~api/user'
import { setUserAppInfo, setUserTenantInfo } from '~sto/user'
import { useRoute, useRouter } from 'vue-router'
import { getObjVal, getObjValue, isNullES } from 'js-fast-way'
import svg403 from '~src/assets/view/403.svg'
import { getTopUrl } from '~uti/tools'
import website from '~src/config'
import { getStoreValue } from '~uti/storage'

//初始变量
const router = useRouter()
const useRoutes = useRoute()
const store = useAppStore()

//先清理下缓存
store.clearStoreData()

//变量
const loading = ref(true)
const isErrorShow = ref(false)
const toUrl = ref('')

//渲染完成
onMounted(() => {
    // http://档案的域名/#/auth-token?token=xxx&tid=xxx&pid=xxx&cid=xxx&bg=no&layout=no&url=xxx
    const { tid, pid, cid, bg, layout, url } = getObjValue(useRoutes.query)
    const urlQuery = getObjValue(useRoutes.query)
    const gfzxtoken = urlQuery['gfzx-token'] || ''
    if (!isNullES(gfzxtoken)) {
        isErrorShow.value = false
        toUrl.value = url 
        //缓存数据
        store.setTokenVal(gfzxtoken)
        store.setProjectId(pid)
        store.setContractId(cid)
        store.setIsDatavBg(bg !== 'no')
        store.setIsLayout(layout)
        //处理授权登录
        setLoginByTokenData(gfzxtoken, tid)
    } else {
        loading.value = false
        isErrorShow.value = true
    }
})

//获取租户id
const getTenantIdApi = async () => {
    const { data } = await getTenantID(getTopUrl())
    const { id } = await setUserTenantInfo(getObjVal(data))
    return id
}

//设置租户信息
const setLoginByTokenData = async (token, tenant_id) => {
    const tenantIds = await getTenantIdApi()
    const tenantId = tenant_id ? tenant_id : tenantIds
    await loginByTokenApi({ token, tenantId })
}

//请求授权登录
const loginByTokenApi = async (form) => {
    await setUserTenantInfo()
    const { error, code, data } = await loginByToken5(form)
    const res = getObjVal(data)
    if (!error && code === 200 && res) {
        await setUserAppInfo(res)
        setTimeout(() => {
            loading.value = false
            isErrorShow.value = false
         
            window?.$message?.success('授权登录成功')
            console.log(toUrl.value, 'toUrl.value')
            if (!website.localModel) {
            //210跳转文件收集
                // router.push({ name: 'file' })  
            const role_id = getStoreValue('role_id')
            if (website.ex_role_id.includes(role_id)) {
                console.log('专家跳转')
                router.push({ name: 'transfer-initial-expert' })
            } else {
              
                if (toUrl.value) {
                    router.push({ path: toUrl.value })
                } else {
                    router.push({ name: 'statistics-stats' })
                }
              
            }
           
            } else {
                // window?.$message?.success('授权登录成功')
                //甬台温，跳转大屏
                router.push({ path: toUrl.value })
            }
            
        }, 500)
    } else {
        window.$message?.error('授权登录失败')
        isErrorShow.value = true
        loading.value = false
        store.setIsDatavBg(true)
    }
}

//跳转登陆
const toLoginTap = () => {
    // router.push({ path: '/login' })
    const { pid } = getObjValue(useRoutes.query)
        if (pid === '1891312830718746625') {
            // router.push({ path: '/login1' })
            router.push({ path: '/login-main' })
        } else {
            router.push({ path: '/login-main' })
        }
}
</script>

<style lang="scss" scoped>
@import "../../styles/error/style.scss";
.hc-body-loading {
    position: relative;
    height: 100%;
    width: 100%;
}
</style>
