import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import {
  deleteSessionVar,
  getSessionVar,
  saveSessionVar,
} from "@/utils/session";

export const useMenuStore = defineStore('menu', () => {
  function initRouteList() {
    return getSessionVar("routeList") ? JSON.parse(getSessionVar("routeList")) : [{
      path: '/kmplatform/openplatform',
      name: 'openplatform',
      title: "开放平台",
      children: []
    }]
  }
  let routeList = ref(initRouteList())

  // const routeList = ref([{
  //   path: '/kmplatform/home',
  //   name: 'kmplatform-home',
  //   title: "主页",
  //   children: []
  // },
  // {
  //   path: '/kmplatform/knowledgebase',
  //   name: 'knowledgebase',
  //   title: "知识库",
  //   children: []
  // },
  // {
  //   path: '/kmplatform/kgbuilder',
  //   name: 'kgbuilder',
  //   title: "知识图谱构建",
  //   children: []
  // },
  // {
  //   path: '/kmplatform/openplatform',
  //   name: 'openplatform',
  //   title: "开放平台",
  //   children: []
  // },
  // {
  //   path: '/kmplatform/kgpermission',
  //   name: 'kgpermission',
  //   title: "系统权限",
  //   children: []
  // }
  // ])
  const operationPermissions = computed(() => {
    let permissions = {}
    function traverseTree(node) {
      // 如果当前节点有子节点，递归遍历每个子节点
      if (node.children && node.children.length > 0) {
        node.children.forEach(child => {
          permissions[child.menu_route] = true
          // console.log(child.name); // 处理子节点
          traverseTree(child); // 递归遍历子节点的子节点
        });
      }
    }

    routeList.value.forEach(node => {
      traverseTree(node);
    });
    return permissions
  })

  const updateRouteList = (newRoutes) => {
    routeList.value = newRoutes; // 更新路由列表
  };
  function isOP(code) {
    return operationPermissions.value[code] ? true : false;
  }
  return { routeList, updateRouteList, operationPermissions, isOP }
})
