import { defineStore } from "pinia";
// import router from "@/routers";
import { getAuthMenuListApi } from "@/api/modules/login";
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList, setKeeAliveRoute, getBtnsAuthList } from "@/utils";
//setKeeAliveRoute
export const useAuthStore = defineStore({
    id: "wms-auth",
    state: (): Record<string, any> => ({
        // 菜单权限列表
        authMenuList: [],
        // 当前页面的 router name，用来做按钮权限筛选
        routeName: ""
    }),
    getters: {
        // 菜单权限列表 ==> 这里的菜单没有经过任何处理(单点没有设置isKeepAlive的地方所有自己动手)
        authMenuListGet: state => setKeeAliveRoute(state.authMenuList),
        // 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
        showMenuListGet: state => getShowMenuList(state.authMenuList),
        // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
        flatMenuListGet: state => getFlatMenuList(state.authMenuList),
        // 递归处理后的所有面包屑导航列表
        breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList),
        //设置按钮权限
        btnsAuthList: state => getBtnsAuthList(state.authMenuList)
    },
    actions: {
        // Get AuthMenuList
        async getAuthMenuList() {
            const { data } = await getAuthMenuListApi();
            let dataClone = data;
            //添加新增箱嘜！無法做到通過按鈕控制路由
            if (dataClone.length > 0) {
                dataClone[0]?.children[0]?.children.push({
                    path: "/supplier/code/boxMark/details",
                    name: "boxMarkDetails",
                    component: "/supplier/code/boxMark/details",
                    hidden: true,
                    children: [],
                    meta: {
                        icon: "",
                        title: "新增箱唛",
                        isKeepAlive: true
                    }
                });
            }

            //如果有路由就使用路由，如果沒有就添加一個首頁，讓用戶條到首頁去
            this.authMenuList = dataClone || [
                {
                    path: "/index",
                    name: "home",
                    component: "/home/index",
                    hidden: true,
                    children: [],
                    meta: {
                        icon: "",
                        title: "首页",
                        isKeepAlive: true
                    }
                }
            ];
        },

        // Set RouteName
        async setRouteName(name: string) {
            this.routeName = name;
        }
    }
});
