<template>
  <div :class="{ 'has-logo': showLogo }" class="sidebar-container">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="getMenuBackground"
        :text-color="getMenuTextColor"
        :unique-opened="true"
        active-text-color="#25bf82"
        :collapse-transition="false"
        mode="vertical"
        :class="sideTheme"
      >
        <sidebar-item
          v-for="(route, index) in baseRoutes"
          :key="route.path + index"
          :item="route"
          :base-path="route.path"
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script setup>
import { onMounted, nextTick } from "vue";
import Logo from "./Logo";
import SidebarItem from "./SidebarItem";
import variables from "@/assets/styles/variables.module.scss";
import useAppStore from "@/store/modules/app";
import useSettingsStore from "@/store/modules/settings";
import usePermissionStore from "@/store/modules/permission";

const route = useRoute();
const appStore = useAppStore();
const settingsStore = useSettingsStore();
const permissionStore = usePermissionStore();

const baseRoutes = ref([]);
const sidebarRouters = computed(() => permissionStore.sidebarRouters);
const showLogo = computed(() => settingsStore.sidebarLogo);
const sideTheme = computed(() => settingsStore.sideTheme);
const theme = computed(() => settingsStore.theme);
const isCollapse = computed(() => !appStore.sidebar.opened);

import useUserStore from "@/store/modules/user"; //测试用
import { ref } from "vue";
import { reactive } from "vue";
const userStore = useUserStore(); //测试用
// 下面测试用
const yunying = reactive([
  {
    component: "Layout",
    hidden: false,
    path: "/plant",
    alwaysShow: true,
    name: "Plant",
    redirect: "noRedirect",
    meta: {
      icon: "trace",
      link: null,
      noCache: false,
      title: "智慧种植",
    },
    children: [
      {
        component: "plant/ambient",
        hidden: false,
        meta: {
          title: "农业环境监测",
          icon: "",
          noCache: false,
          link: null,
        },
        name: "Ambient",
        path: "ambient",
      },
      {
        component: "plant/control",
        hidden: false,
        meta: {
          title: "生产管理控制",
          icon: "",
          noCache: false,
          link: null,
        },
        name: "Control",
        path: "control",
      },
    ],
  },
  {
    component: "Layout",
    hidden: false,
    path: "/trade",
    alwaysShow: true,
    name: "Trade",
    redirect: "noRedirect",
    meta: {
      icon: "trace",
      link: null,
      noCache: false,
      title: "电商交易",
    },
    children: [
      {
        component: "trade/agriculturalinputstrading",
        hidden: false,
        meta: {
          title: "农资交易",
          icon: "",
          noCache: false,
          link: null,
        },
        name: "Agriculturalinputstrading",
        path: "agriculturalinputstrading",
      },
      {
        component: "trade/supplierServices",
        hidden: false,
        meta: {
          title: "供应商服务",
          icon: "",
          noCache: false,
          link: null,
        },
        name: "SupplierServices",
        path: "supplierServices",
      },
      {
        component: "trade/procurementServices",
        hidden: false,
        meta: {
          title: "采购商服务",
          icon: "",
          noCache: false,
          link: null,
        },
        name: "ProcurementServices",
        path: "procurementServices",
      },
      {
        component: "trade/landTransaction",
        hidden: false,
        meta: {
          title: "土地交易",
          icon: "",
          noCache: false,
          link: null,
        },
        name: "LandTransaction",
        path: "landTransaction",
      },
    ],
  },
  {
    component: "Layout",
    hidden: false,
    path: "/",
    children: [
      {
        component: "brand/index",
        hidden: false,
        meta: {
          title: "投入品监管平台",
          icon: "",
          noCache: false,
          link: null,
        },
        name: "Brand",
        path: "brand",
      },
    ],
  },
]);
// 下面测试用
const zhengwu = reactive([
  {
    component: "Layout",
    hidden: false,
    path: "/",
    children: [
      {
        component: "mainbody/index",
        hidden: false,
        meta: {
          title: "生产经营主体",
          icon: "icon-entities",
          noCache: false,
          link: null,
        },
        name: "Mainbody",
        path: "mainbody",
      },
    ],
  },
  {
    component: "Layout",
    hidden: false,
    path: "/",
    children: [
      {
        component: "inputs/index",
        hidden: false,
        meta: {
          title: "投入品监管平台",
          icon: "icon-input",
          noCache: false,
          link: null,
        },
        name: "Inputs",
        path: "inputs",
      },
    ],
  },
  {
    component: "Layout",
    hidden: false,
    path: "/",
    alwaysShow: true,
    name: "Trace",
    redirect: "noRedirect",
    meta: {
      icon: "trace",
      link: null,
      noCache: false,
      title: "溯源管理",
    },
    children: [
      {
        component: "traceability/query",
        hidden: false,
        meta: {
          title: "溯源查询",
          icon: "icon-search",
          noCache: false,
          link: null,
        },
        name: "Query",
        path: "query",
      },
      {
        component: "traceability/statistics",
        hidden: false,
        meta: {
          title: "溯源统计",
          icon: "icon-data",
          noCache: false,
          link: null,
        },
        name: "Statistics",
        path: "statistics",
      },
    ],
  },
]);

// 获取菜单背景色
const getMenuBackground = computed(() => {
  if (settingsStore.isDark) {
    return "var(--sidebar-bg)";
  }
  return sideTheme.value === "theme-dark"
    ? variables.menuBg
    : variables.menuLightBg;
});

// 获取菜单文字颜色
const getMenuTextColor = computed(() => {
  if (settingsStore.isDark) {
    return "var(--sidebar-text)";
  }
  return sideTheme.value === "theme-dark"
    ? variables.menuText
    : variables.menuLightText;
});

const activeMenu = computed(() => {
  const { meta, path } = route;
  if (meta.activeMenu) {
    return meta.activeMenu;
  }
  return path;
});
onMounted(async () => {
  baseRoutes.value = [...sidebarRouters.value];
  await nextTick();
  console.log("运营云菜单");
  console.log(baseRoutes.value);
});
</script>

<style lang="scss" scoped>
.sidebar-container {
  background-color: v-bind(getMenuBackground);

  .scrollbar-wrapper {
    background-color: v-bind(getMenuBackground);
  }

  .el-menu {
    border: none;
    height: 100%;
    width: 100% !important;

    .el-menu-item,
    .el-sub-menu__title {
      &:hover {
        background-color: var(--menu-hover, rgba(0, 0, 0, 0.06)) !important;
      }
    }

    .el-menu-item {
      color: v-bind(getMenuTextColor);

      &.is-active {
        color: var(--menu-active-text, #409eff);
        background-color: var(--menu-hover, rgba(0, 0, 0, 0.06)) !important;
      }
    }

    .el-sub-menu__title {
      color: v-bind(getMenuTextColor);
    }
  }
}
</style>
