<template>
  <div class="sidebar">
    <el-menu
      :default-active="activeIndex"
      class="sidebar-el-menu"
      mode="horizontal"
      router
    >
      <template v-for="item in items">
        <template v-if="item.subs">
          <el-submenu :index="item.index" :key="item.index">
            <template slot="title">
              <i :class="item.icon"></i>
              <span slot="title">{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-submenu
                v-if="subItem.subs"
                :index="subItem.index"
                :key="subItem.index"
              >
                <template slot="title">{{ subItem.title }}</template>
                <el-menu-item
                  v-for="(threeItem, i) in subItem.subs"
                  :key="i"
                  :index="threeItem.index"
                  >{{ threeItem.title }}</el-menu-item
                >
              </el-submenu>
              <el-menu-item
                v-else
                :index="subItem.index"
                :key="subItem.index"
                >{{ subItem.title }}</el-menu-item
              >
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index">
            <i :class="item.icon"></i>
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: "/stritl",
      items: [
        {
          icon: "el-icon-s-home",
          index: "/stritl",
          title: "首页",
        },
        {
          icon: "el-icon-camera",
          index: "/realTimeMonitor",
          title: "实时监控",
        },
        {
          icon: "el-icon-film",
          index: "/pictureRotation",
          title: "图片轮巡",
        },
        {
          icon: "el-icon-bell",
          index: "/photoAlarm",
          title: "告警处理",
        },
        {
          icon: "el-icon-search",
          index: "/realTimeSearch",
          title: "历史图片",
        },
        {
          icon: "iconfont icon-fubing",
          index: "/weatherIce",
          title: "气象覆冰",
          subs: [
            {
              index: "/weather",
              title: "气象监测",
            },
            {
              index: "/icing",
              title: "覆冰监测",
            },
          ],
        },
        {
          icon: "el-icon-files",
          index: "/property",
          title: "资产管理",
          subs: [
            {
              index: "/dyInformation",
              title: "电压信息管理",
            },
            {
              index: "/lineInformation",
              title: "线路信息管理",
            },
            {
              index: "/towerInformation",
              title: "杆塔信息管理",
            },

            {
              index: "/cameraChannel",
              title: "通道管理",
            },
            {
              index: "/photographicDevice",
              title: "装置管理",
            },
            {
              index: "/devicePhotoSchedule",
              title: "拍照时间表设置",
            },
            {
              index: "/reportData",
              title: "数据报表",
            },
            {
              index: "/statisticalReport",
              title: "统计报表",
            }
          ],
        },
        {
          icon: "el-icon-monitor",
          index: "/system",
          title: "系统管理",
          subs: [
            {
              index: "/userManagement",
              title: "用户管理",
            },
            {
              index: "/roleManagement",
              title: "角色管理",
            },
            // {
            //   index: "/menuManagement",
            //   title: "菜单功能管理",
            // },
            {
              index: "/deviceUpgrade",
              title: "升级管理",
            },

            {
              index: "/globalTools",
              title: "全局设置",
            },
            {
              index: "/waterMark",
              title: "水印下发",
            },
            {
              index: "/otherCommon",
              title: "批量命令",
            },
            {
              index: "/aiWork",
              title: "AI绘图",
            },
            {
              index: "/weatherIceConfig",
              title: "覆冰微气象转发",
            },
          ],
        },
      ],
      role: "",
      menuList: [],
    };
  },

  watch: {
    $route() {
      this.setCurrentRoute();
    },
  },
  methods: {
    setCurrentRoute() {
      console.log(this.$route.path);
      this.$nextTick(() => {
        this.activeIndex = this.$route.path; // 通过他就可以监听到当前路由状态并激活当前菜单
        console.log(this.activeIndex);
        localStorage.setItem("menuActive", this.activeIndex);
      });
    },

    getMenuArr(originalArray) {
      const icons = {
        stritl: "el-icon-s-home",
        realTimeMonitor: "el-icon-camera",
        pictureRotation: "el-icon-film",
        photoAlarm: "el-icon-bell",
        realTimeSearch: "el-icon-search",
        // ... 其他图标
        default: "el-icon-default", // 默认图标
      };
      const assetManagementSubs = []; // 用于存储资产管理子菜单的对象
      const systemManagementSubs = []; // 用于存储系统管理子菜单的对象
      const weatherManagementSubs = []; // 用于存储系统管理子菜单的对象
      const newArray = [];

      // 遍历原始数组，根据条件构建新数组
      originalArray.forEach((item) => {
        const { id, key, desc } = item;
        console.log(key, desc);
        const index = key.startsWith("/") ? key.substring(1) : key; // 移除开头的斜杠
        // 移除desc中"-"后面的内容
        const processedDesc = desc.split("-")[0].trim();
        console.log(index, processedDesc);
        const icon = icons[index] || icons["default"]; // 获取图标，如果未找到则使用默认图标

        if (
          [
            "dyInformation",
            "lineInformation",
            "towerInformation",
            "cameraChannel",
            "photographicDevice",
            "devicePhotoSchedule",
            "reportData",
          ].includes(index)
        ) {
          // 如果包含杆塔或线路信息，则添加到资产管理子菜单
          assetManagementSubs.push({
            index: "/" + index,
            title: processedDesc,
          });
        } else if (
          [
            "userManagement",
            "roleManagement",
            "menuManagement",
            "deviceUpgrade",
            "globalTools",
            "waterMark",
          ].includes(index)
        ) {
          // 如果包含用户列表等，则添加到系统管理子菜单
          systemManagementSubs.push({
            index: "/" + index,
            title: processedDesc,
          });
        } else if (["weather", "icing"].includes(index)) {
          // 如果包含用户列表等，则添加到系统管理子菜单
          weatherManagementSubs.push({
            index: "/" + index,
            title: processedDesc,
          });
        } else {
          // 其他菜单项直接添加到新数组中
          newArray.push({ icon, index: "/" + index, title: processedDesc });
        }
      });
      // 如果存在资产管理子菜单项，则添加资产管理菜单
      if (weatherManagementSubs.length > 0) {
        newArray.push({
          icon: "iconfont icon-fubing",
          index: "weatherIce",
          title: "气象覆冰",
          subs: weatherManagementSubs,
        });
      }
      // 如果存在资产管理子菜单项，则添加资产管理菜单
      if (assetManagementSubs.length > 0) {
        newArray.push({
          icon: "el-icon-files",
          index: "property",
          title: "资产管理",
          subs: assetManagementSubs,
        });
      }

      // 如果存在系统管理子菜单项，则添加系统管理菜单
      if (systemManagementSubs.length > 0) {
        newArray.push({
          icon: "el-icon-setting", // 假设的系统管理图标
          index: "system",
          title: "系统管理",
          subs: systemManagementSubs,
        });
      }
      console.log("我是最后的", newArray);

      //return newArray;
      this.items = newArray;
    },
  },
  created() {
    console.log("我是被选中的", this.activeIndex);
    this.setCurrentRoute();
    this.role = localStorage.getItem("role");
    console.log("用户管理");
    console.log(this.role);
    if (this.role !== "0") {
      this.menuList = JSON.parse(localStorage.getItem("menuPermission"));
      console.log(this.menuList);
      this.menuList.push({ id: 8, key: "/stritl", desc: "首页-菜单" });
      this.menuList = this.menuList.sort((a, b) => a.id - b.id);
      this.getMenuArr(this.menuList);
    } else {
      console.log("我是超管");
      this.items = this.items;
    }
  },
};
</script>

<style lang="less">
.sidebar {
  .el-menu--horizontal {
    background: #169e8c;

    .el-menu-item {
      height: 56px;
      line-height: 56px;
    }
    .el-submenu .el-submenu__title {
      height: 56px;
      line-height: 56px;
    }
    .el-submenu__title {
      padding: 0 10px;
      .iconfont {
        margin-right: 5px;
      }
    }
    .el-dropdown-menu__item,
    .el-menu-item,
    .el-submenu__title {
      font-size: 16px;
    }
    .el-menu-item i {
      color: #666;
    }
    .el-menu-item.is-active {
      i {
        color: #169e8c;
      }
    }
    .el-submenu.is-active .el-submenu__title > i {
      &:first-child {
        color: #333;
      }
    }
  }
  .el-menu {
    background: #169e8c;

    i {
      color: #fff !important;
    }
    .el-menu-item {
      color: #fff;
    }
    .el-submenu .el-submenu__title {
      color: #fff;
    }
    .el-menu-item:not(.is-disabled):focus,
    .el-menu-item:not(.is-disabled):hover {
      color: #fff;
      background-color: #128071;
      border-bottom: 2px solid #128071;
      i {
        color: #fff !important;
      }
    }
    .el-submenu:focus .el-submenu__title,
    .el-submenu:hover .el-submenu__title {
      color: #fff !important;
      background-color: #128071;
      border-bottom: 2px solid #128071;
      i {
        color: #fff !important;
      }
    }
  }
  .el-menu--horizontal > .el-menu-item.is-active {
    border-bottom: 2px solid #128071 !important;
    color: #fff !important;
    background-color: #128071 !important;
    i {
      color: #fff !important;
    }
  }
  .el-submenu.is-active {
    .el-submenu__title {
      border-bottom: 2px solid #128071 !important;
      color: #fff !important;
      background-color: #128071;
      i {
        color: #fff !important;
      }
    }
  }
  .gallery-thumbs .swiper-slide-active {
    border: 3px solid #169e8c !important;
  }
  .tags-li.active {
    border: 1px solid #169e8c;
    background-color: #169e8c;
  }
  .el-menu-item [class^="iconfont"] {
    margin-right: 5px;
    width: 24px;
    text-align: center;
    font-size: 18px;
    vertical-align: middle;
  }
}
</style>
