import { defineStore } from "pinia";
// import router from "@/routers";
import { getAuthMenuListApi } from "@/api/modules/login";
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList, setKeeAliveRoute, getBtnsAuthList } from "@/utils";
// import { setRouterControl } from "@/utils/routerControl";
import { boxMarkIndexAdd, subscriptionIndexAdd } from "@/utils/routerControl/constant/index";

// const TYPES_NUMBER: any = {
//     boxMarkIndexAdd: 6
// };

//setKeeAliveRoute
export const useAuthStore: any = defineStore({
    id: "wms-auth",
    state: (): 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();
            //如果有路由就使用路由，如果沒有就添加一個首頁，讓用戶條到首頁去
            this.authMenuList = data || [
                {
                    path: "/index",
                    name: "home",
                    component: "/home/index",
                    hidden: true,
                    children: [],
                    meta: {
                        icon: "",
                        title: "首页",
                        isKeepAlive: true
                    }
                }
            ];
            // //判断箱唛列表是否有新增按钮
            let btnName: any = this.authMenuList[6]?.children[0]?.children[0]?.children[0]?.name;
            let btnName1: any = this.authMenuList[6]?.children[1]?.children[0]?.children[0]?.name;
            //如果箱唛列表页面有新增按钮就增加该路由!
            if (btnName === "boxMarkIndexAdd") {
                this.authMenuList[6]?.children[0]?.children.push(boxMarkIndexAdd);
            }
            if (btnName1 === "subscriptionIndexAdd") {
                this.authMenuList[6]?.children[1]?.children.push(subscriptionIndexAdd);
            }
        },
        // Set RouteName
        async setRouteName(name: string) {
            this.routeName = name;
        }
    }
});
