import { defineStore } from 'pinia'
import { addLog } from '@/api'
import { loadShopInfo } from '@/api/user'
import { addDynamicRoutes } from '@/router/vue-router'
import { adminRoutes, managerRoutes, userRoutes, adminMenus } from '@/utils/routeMenu'
import dayjs from 'dayjs'

export const useUserStore = defineStore('user-info',{
    state: () => {
        return {
            userInfo: {},
            shopInfo: {},
            routerList: [],
            menuList: [],
            user_groups: [],
            partner: [],
            routeSetted: false,
        }
    },
    actions: {
        async setInfo(userinfo) {
            this.userInfo = userinfo
            switch (userinfo.role) {
                case '0':
                    this.routerList = adminRoutes
                    break
                case '1':
                    this.routerList = managerRoutes
                    break
                case '2':
                    this.routerList = userRoutes
                    break
            }
            await addDynamicRoutes(this.routerList)
            this.routeSetted = true
            this.menuList = adminMenus
            const {data: res} = await loadShopInfo()
            if (res.error === 0) {
                this.shopInfo = res.data
            } else {
                this.shopInfo = {}
            }
            if(userinfo.role === '0') {
                this.userInfo.team = this.shopInfo.account.map(item => item.id)
            } else {
                this.userInfo.team = [userinfo.id]
                if(userinfo.authority && Object.keys(userinfo.authority).length > 0) {
                    let authority = JSON.parse(userinfo.authority)
                    if(authority.team && authority.team.length > 0) {
                        this.userInfo.team = this.userInfo.team.concat(authority.team)
                    }
                }
            }
            await addLog(0,this.userId,'login',dayjs().format('YYYY-MM-DD HH:mm:ss'))
        },
        async addMainRoute() {
            await addDynamicRoutes(this.routerList)
        },
        clearInfo() {
            this.userInfo = {}
            this.user_groups = []
            this.shopInfo = {}
            this.routerList = []
            this.menuList = []
            this.routeSetted = false
        },
        async newUserGroup(name) {
            if (!name || name.trim() === '') {
                throw new Error('工作组名称不能为空或重复！')
            }
            this.user_groups.push({name,member: [this.userId]})
        },
        async editUserGroup(idx,name, member=[]) {
            if (!name || name.trim() === '') {
                throw new Error('工作组名称不能为空')
            }
            if(member.length === 0) {
                member = [this.userId]
            }
            this.user_groups[idx] = {name,member}
        },
        logout() {
            this.clearInfo()
        }
    },
    getters: {
        userId: (state) => {
            return state.userInfo.id || ''
        },
        userName: (state) => {
            return state.userInfo.name || ''
        },
        isAdmin: (state) => {
            return state.userInfo.role === '0' || state.userInfo.role === '1'
        },
        isSalesman: (state) => {
            return state.userInfo.role !== '0' && state.userInfo.role !== '1'
        },
        isLoggedIn: (state) => {
            return Object.keys(state.userInfo).length > 0
        },
        hasRouterList: (state) => {
            return state.routerList.length > 0
        },
        team: (state) => {
            return state.userInfo.team
        },
        teamInfo: (state) => {
            let groups_info = {}
            state.userInfo.team.forEach(id => {
                let item = state.shopInfo.account.find(el => el.id === id)
                if(item) {
                    groups_info[id] = {
                        id: item.id,
                        name: item.nickname || item.name,
                        role: item.role
                    }
                } else {
                    groups_info[id] = {
                        id: id,
                        name: '未知用户',
                        role: '2'
                    }
                }
            })
            return groups_info
        }
    },
    persist: true
})

export const useInquiryListStore = defineStore('inquiry-list',{
    state: () => {
        return {
            currentPage: 1,
            pageSize: 20,
            keys: {}
        }
    },
    actions: {
        reset() {
            this.currentPage = 1
            this.pageSize = 20
            this.keys = {}
        }
    },
    getters: {}
})

export const useOrderListStore = defineStore('order-list',{
    state: () => {
        return {
            currentPage: 1,
            pageSize: 20,
            keys: {}
        }
    },
    actions: {
        reset() {
            this.currentPage = 1
            this.pageSize = 20
            this.keys = {}
        }
    },
    getters: {}
})
