import { Module } from 'vuex';
import { useAsRouter } from '@/hooks/useAsRouter';
import { routesModuleList } from '@/router/dynamic';
import { routesManageModuleList } from '@/router/manageDynamic';
import { routesJobseekerModuleList } from '@/router/jobseekerDynamic';

import { LayoutRoute } from '@/router/routes';

const { routerDynamic, routerAdd, routerInit } = useAsRouter();

const NotFound = {
	path: '/:pathMatch(.*)*',
	name: 'NotFound',
	component: () => import('@/views/404.vue'),
	meta: {
		title: '系统找不到页面',
	}
}
export interface PermissionState {
	menuList : string[];
	permissionList : string[] | null;
	breadcrumbList : string[];
}

const state : PermissionState = {
	menuList: [],
	permissionList: null,
	breadcrumbList: []
};

const mutations = {
	SET_PERMISSION(state, routes) {
		state.permissionList = routes
	},
	SET_MENU(state, menu) {
		state.menuList = menu
	}
};

const actions = {
	async FETCH_PERMISSION({
		commit,rootState 
	}) {
		try {			
			// let res = await LOGIN.permissions();
			// let arr = res.data.data;
			console.log(rootState)
			if(rootState.token && rootState.role === 'company' && rootState.pageType == 'company' ) {
				LayoutRoute.children = [];
				commit('SET_MENU', []);
				commit('SET_PERMISSION', null);
				LayoutRoute.children = routesManageModuleList;
				commit('SET_MENU', LayoutRoute.children);
				routerAdd(LayoutRoute);
				routerAdd(NotFound);
				let initialRoutes = routerInit();
				commit('SET_PERMISSION', [...initialRoutes]);
			} else if(rootState.token && rootState.role === 'personal'  && rootState.pageType == 'personal'){
				LayoutRoute.children = [];
				commit('SET_MENU', []);
				commit('SET_PERMISSION', null);
				LayoutRoute.children = routesJobseekerModuleList;
				commit('SET_MENU', LayoutRoute.children);
				routerAdd(LayoutRoute);
				routerAdd(NotFound);
				let initialRoutes = routerInit();
				commit('SET_PERMISSION', [...initialRoutes]);
			} else {
				LayoutRoute.children = [];
				commit('SET_MENU', []);
				commit('SET_PERMISSION', null);
				LayoutRoute.children = routesModuleList;
				commit('SET_MENU', LayoutRoute.children);
				routerAdd(LayoutRoute);
				routerAdd(NotFound);
				let initialRoutes = routerInit();
				commit('SET_PERMISSION', [...initialRoutes]);
			}
			
		} catch {

		}
	}
};

export const permissionsModule : Module<PermissionState> = {
	namespaced: true,
	state,
	mutations,
	actions
};