<template>
    <div></div>
</template>

<script setup>
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { SET_TOKEN,SET_USER_ROLES,SET_USER_CUR_ROLE } from "@/store/modules/app/type";
import { onMounted } from "vue";

const router = useRouter();
const route = useRoute();
const store = useStore();

let userId = route.query.userId;

onMounted(async () => {
    const token = route.query.token
    const targetUrl = route.query.targetUrl ? route.query.targetUrl : '/home';
    
    // 先从 session 获取保存的角色
    let savedRole = null;
    
    if (!token) {
        const { data } = await VE_API.system.devLogin({ userId: userId });
        store.dispatch(`app/${SET_TOKEN}`, data.token);
        store.dispatch(`app/${SET_USER_ROLES}`, data.roles);
        
        const sessionRes = await VE_API.mine.getSessionStore({ key: "cur_role" });
        if (sessionRes && sessionRes.data) {
            savedRole = sessionRes.data.val;
        }
        // 优先使用保存的角色，如果保存的角色在 roles 中，则使用保存的角色，否则如果 roles 中包含 2，赋值 2，否则赋值 0
        let curRole = 0;
        if (savedRole !== null && Array.isArray(data.roles) && data.roles.includes(savedRole)) {
            curRole = savedRole;
        }
        store.dispatch(`app/${SET_USER_CUR_ROLE}`, curRole);
        localStorage.setItem("localUserInfo", JSON.stringify(data));
    }
    // 存userId
    let pointXy = route.query.point.split(",")
    // 刷新后仍保留id和point
    localStorage.setItem("MINI_USER_ID", userId)
    route.query?.userInfo && localStorage.setItem("localUserInfo", route.query.userInfo);
    if(route.query.roles){
        const roles = JSON.parse(route.query.roles);
        store.dispatch(`app/${SET_USER_ROLES}`, roles);
        
        const sessionRes = await VE_API.mine.getSessionStore({ key: "cur_role" });
        if (sessionRes && sessionRes.data) {
            savedRole = sessionRes.data.val;
        }
        // 优先使用保存的角色，如果保存的角色在 roles 中，则使用保存的角色，否则如果 roles 中包含 2，赋值 2，否则赋值 0
        let curRole = 0;
        if (savedRole !== null && Array.isArray(roles) && roles.includes(savedRole)) {
            curRole = savedRole;
        }
        store.dispatch(`app/${SET_USER_CUR_ROLE}`, curRole);
    }
    await getFarmList();
    localStorage.setItem("MINI_USER_LOCATION", route.query.point)
    localStorage.setItem("MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`)
    store.commit("home/SET_MINI_USER_LOCATION", route.query.point);
    store.commit("home/SET_MINI_USER_ID", userId);
    store.commit("home/SET_MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`);
    router.push(`${targetUrl}?miniJson=${JSON.stringify(route.query)}`);
})

const getFarmList = async () => {
    localStorage.removeItem('selectedFarmId');
    localStorage.removeItem('selectedFarmName');
    const { data } = await VE_API.farm.userFarmSelectOption();
    if(data && data.length > 0) {
        localStorage.setItem('isGarden', true);
        const defalutFarm = data[0]
        localStorage.setItem('selectedFarmId', defalutFarm.id);
        localStorage.setItem('selectedFarmName', defalutFarm.name);
    }
}

</script>

<style scoped></style>
