import { defineStore } from 'pinia'
import { ADMIN_INFO } from '@/stores/constant/cacheKey'
import type { AdminInfo } from '@/stores/interface'

export const useAdminInfo = defineStore('adminInfo', {
    state: (): AdminInfo => {
        return {
            access_token: '',
            token_type: '',
            refresh_token: '',
            expires_in: 0,
            scope: '',
            nickname: '',
            userType: 0,
            deptIndex: '',
            userIndex: '',
            client_id: '',
            headSculpture: '',
            jti: '',
            name: '',
            deptId: '',
            phone: '',
            email: '',
            limitIpStart: '',
            limitIpEnd: '',
            limitTime: '',
            casualUser: 0,
            type: 0,
            smsNotice: 0,
            emailNotice: 0,
            role: [],
            devCode: '',
            id: '',
            loginName: '',
            state: 0,
            registerTime: '',
            loginTime: '',
            deptName: '',
            areaId: '',
            areaName: '',
            deptLevel: 0,
            roleList: [],
            roleCode: []
        }
    },
    actions: {
        dataFill(state: AdminInfo) {
            this.$state = { ...this.$state, ...state }
        },
        reset() {
            this.$state = {
                access_token: '',
                token_type: '',
                refresh_token: '',
                expires_in: 0,
                scope: '',
                nickname: '',
                userType: 0,
                deptIndex: '',
                userIndex: '',
                client_id: '',
                headSculpture: '',
                jti: '',
                name: '',
                deptId: '',
                phone: '',
                email: '',
                limitIpStart: '',
                limitIpEnd: '',
                limitTime: '',
                casualUser: 0,
                type: 0,
                smsNotice: 0,
                emailNotice: 0,
                role: [],
                devCode: '',
                id: '',
                loginName: '',
                state: 0,
                registerTime: '',
                loginTime: '',
                deptName: '',
                areaId: '',
                areaName: '',
                deptLevel: 0,
                roleList: [],
                roleCode: []
            }
        },
        removeToken() {
            this.access_token = ''
            this.refresh_token = ''
        },
        setToken(token: string, type: 'auth' | 'refresh') {
            const field = type == 'auth' ? 'access_token' : 'refresh_token'
            this[field] = token
        },
        getToken(type: 'auth' | 'refresh' = 'auth') {
            if (type === 'auth') {
                if (this.token_type && this.access_token) {
                    return this.token_type + ' ' + this.access_token
                } else {
                    return ''
                }
            } else {
                return this.refresh_token
            }
        }
    },
    persist: {
        key: ADMIN_INFO
    }
})
