import {defineStore} from 'pinia'
import cache from '@/utils/cache'
import type {RouteRecordRaw} from 'vue-router'
import {getMenu, getUserInfo, login} from '@/api/user'
import router, {filterAsyncRoutes} from '@/router'
import {TOKEN_KEY} from '@/enums/cacheEnums'
import {PageEnum} from '@/enums/pageEnum'
import {clearAuthInfo, getBrandId, getHouseId, getToken, getUserId} from '@/utils/auth'

export interface UserState {
    token: string
    houseId: string
    brandId: string
    userId: string
    userInfo: Record<string, any>
    routes: RouteRecordRaw[]
    menu: any[]
    perms: string[]
}

const useUserStore = defineStore({
    id: 'user',
    state: (): UserState => ({
        token: getToken() || '',
        houseId: getHouseId() || '',
        brandId: getBrandId() || '',
        userId: getUserId() || '',
        // 用户信息
        userInfo: {},
        // 路由
        routes: [],
        menu: [],
        // 权限
        perms: []
    }),
    getters: {},
    actions: {
        resetState() {
            this.token = ''
            this.houseId = ''
            this.brandId = ''
            this.userId = ''
            this.userInfo = {}
            this.perms = []
        },
        login(playload: any) {
            const { mobile, passWord, code, uuid } = playload
            return new Promise((resolve, reject) => {
                login({
                    mobile: mobile,
                    passWord,
                    code,
                    uuid
                })
                    .then((data) => {
                        this.token = data.single.token
                        cache.set(TOKEN_KEY, data.single.token)
                        window.localStorage.setItem("like_admin_token",data.single.token)
                        window.localStorage.setItem("like_admin_brandId",data.single.brandId)
                        window.localStorage.setItem("like_admin_userId",data.single.dataUserId)
                        if(data.single.type === 2 && data.single.houseList.length == 1){
                            window.localStorage.setItem("like_admin_houseId",data.single.houseList[0].houseId)
                        }
                        resolve(data.single)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        logout() {
            /*return new Promise((resolve, reject) => {
                logout()
                    .then(async (data) => {
                        this.token = ''
                        await router.push(PageEnum.LOGIN)
                        clearAuthInfo()
                        resolve(data)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })*/
            clearAuthInfo()
            router.push(PageEnum.LOGIN)
        },
        getUserInfo() {
            return new Promise((resolve, reject) => {
                getUserInfo()
                    .then((data) => {
                        this.userInfo = data.user
                        this.perms = data.permissions
                        resolve(data)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        getMenu() {
            return new Promise((resolve, reject) => {
                getMenu()
                    .then((data) => {
                        this.menu = data
                        this.routes = filterAsyncRoutes(data)
                        resolve(data)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        }
    }
})

export default useUserStore
