import { toRefs, shallowReadonly, ShallowRef } from 'vue'
import { queryNewFuncmenu } from '@/services/api/common'
import { queryAccountMenu } from '@/services/api/account'
import { VueStore } from '../base'
import WebStorage from '@/utils/storage/base'

interface StoreState {
    loading: boolean;
    userMenus: ShallowRef<Ermcp.UserMenu[]>;
}

const store = new (class extends VueStore<StoreState>{
    constructor() {
        const storage = new WebStorage<Ermcp.UserMenu[]>(sessionStorage, 'userMenus', [])
        const state: StoreState = {
            loading: false,
            userMenus: storage.getRef()
        }
        super(state)
    }

    actions = {
        /** 获取用户菜单列表 */
        getUserMenuList: () => {
            this.state.loading = true
            return queryAccountMenu({
                success: (res) => {
                    this.state.userMenus = res.data
                },
                complete: () => {
                    this.state.loading = false
                }
            })
            return queryNewFuncmenu({
                data: {
                    menutype: '5',
                },
                success: (res) => {
                    // 扁平列表树形化
                    const arrayToTree = (list: Ermcp.NewFuncmenuRsp[], code: null | string = null) => {
                        const getChildren = (parent: null | string) => {
                            const result: Ermcp.UserMenu[] = []
                            list.forEach((e) => {
                                if (e.parentcode === parent) {
                                    const item = {
                                        id: 0,
                                        authType: e.authtype,
                                        title: e.resourcename,
                                        code: e.resourcecode,
                                        urlType: e.urltype,
                                        url: e.url || '',
                                        component: e.component,
                                        icon: e.iconame,
                                        buttonName: e.buttonname,
                                        buttonType: e.buttontype,
                                        sort: e.sort,
                                        hidden: Boolean(e.hidden),
                                        remark: e.remark,
                                        children: getChildren(e.resourcecode),
                                    }
                                    result.push(item)
                                }
                            })
                            return result
                        }
                        return getChildren(code)
                    }
                    this.state.userMenus = arrayToTree(res.data)
                },
                complete: () => {
                    this.state.loading = false
                }
            })
        },
        /** 重置数据 */
        reset: () => {
            this.state.userMenus = []
        }
    }
})

export function useMenuStore() {
    return shallowReadonly({
        ...toRefs(store.state),
        ...store.actions,
        ...store.methods,
    })
}