import { defineStore } from 'pinia'
//import { menu, permission } from "../api/module/user";

import  menu  from "../mockjs/user";
import {getMenu} from "@/api/module/home";

interface Tab {
  id: string
  title: string
  closable: boolean
}

export const useUserStore = defineStore({
  id: 'user',
  state: () => {
    return {
      token: '',
      userInfo: {} as any,
      permissions: [] as string[],
      menus: [] as any[],
      visitedRoutes: [] as Tab[]  // 新增访问记录
    }
  },
  actions: {
    async loadMenus(){
      const data = await getMenu({});
      this.menus =  data.data;
      console.log(data);
    },
    async loadPermissions(){
      // const { data, code } = await permission();
      // if(code == 200) {
      //
      // }
      this.permissions = ['sys:user:add', 'sys:user:edit', 'sys:user:delete', 'sys:user:import', 'sys:user:export'];
    },
    // 添加访问过的路由
    addVisitedRoute(tab: Tab) {
      if (!this.visitedRoutes.some(item => item.id === tab.id)) {
        this.visitedRoutes.push(tab);
      }
    },
    
    // 移除访问记录
    removeVisitedRoute(tabId: string) {
      const index = this.visitedRoutes.findIndex(item => item.id === tabId);
      if (index > -1) {
        this.visitedRoutes.splice(index, 1);
      }
    },
    
    // 清空访问记录
    clearVisitedRoutes() {
      // 保留首页
      this.visitedRoutes = this.visitedRoutes.filter(tab => tab.id === '/home');
    }
  },
  persist: {
    key: 'user',
    storage: localStorage,
    paths: ['token', 'userInfo', 'permissions', 'menus', 'visitedRoutes'],
  }
})
