<template>
  <div class="common-layout">
    <el-container>
      <!-- 头部 -->
      <!-- <el-header class="el-header">
        <h2>220kV南河变电站辅助设备监控系统(全站总览)</h2>
      </el-header> -->

      <el-container>
        <!-- 左侧菜单 -->
        <el-aside v-if="true" class="el-aside" :style="{ width: asideWidth }">
          <div class="toggle-button" @click="toggleCollapse">
            <el-icon v-if="!isCollapse"><Fold /></el-icon>
            <el-icon v-else><Expand /></el-icon>
          </div>
          <el-scrollbar class="el-scrollbar">
            <el-menu
              :collapse="isCollapse"
              :collapse-transition="false"
              :default-active="$route.name"
            >
              <menuIndex :is-collapse="isCollapse" @getMenuList="getMenuList" />
            </el-menu>
          </el-scrollbar>
        </el-aside>

        <!-- 主体 -->
        <el-main class="el-main" v-loading="false">
          <el-scrollbar>
            <div class="el-main-core">
              <RouterView />
            </div>
          </el-scrollbar>
        </el-main>
      </el-container>

      <el-footer class="el-footer">
        <div class="footer-navigation" v-if="targetArr && targetArr.length > 0">
          <el-button
            v-for="item in targetArr"
            @click="lineUrl(item.id)"
            :key="item.id"
            plain
            class="nav-item flex-1"
          >
            {{ item.label }}
          </el-button>
        </div>

        <!-- <div class="footer-navigation">
          <el-button plain @click="lineUrl" class="nav-item flex-1">全站报文</el-button>
          <el-button plain class="nav-item flex-1">越限信息</el-button>
          <el-button plain class="nav-item flex-1">异常设备</el-button>
          <el-button plain class="nav-item flex-1">消防系统</el-button>
          <el-button plain class="nav-item flex-1">安全防范</el-button>
          <el-button plain class="nav-item flex-1">在线监测</el-button>
          <el-button plain class="nav-item flex-1">给排水系统</el-button>
          <el-button plain class="nav-item flex-1">环境监控</el-button>
          <el-button plain class="nav-item flex-1">照明控制</el-button>
          <el-button plain class="nav-item flex-1">智能锁控</el-button>
          <el-button plain class="nav-item flex-1">智能联动</el-button>
        </div> -->
      </el-footer>
    </el-container>
  </div>
</template>

<script lang="ts" setup>
import { createRouter, RouterView, useRouter } from 'vue-router';
import { Menu, Fold, Expand } from '@element-plus/icons-vue';
import { ref, reactive, onUnmounted, watch } from 'vue';
import bottomMenuIndex from '@/components/menu/bottomMenuIndex.vue';
import menuIndex from '@/components/menu/menuIndex.vue';
import emitter from '@/utils/emitter';

let initArr: any[] = [
  { id: 'homeGlobalMessage', label: '全站报文' },
  { id: 'homeExceedMessage', label: '越限信息' },
  { id: 'homeExceptionFacility', label: '异常设备' },
  { id: 'fireControlIndex', label: '消防系统' },
  { id: 'securityIndex', label: '安全防范' },
  { id: 'monitoringIndex', label: '在线监测' },
  { id: 'waterSupply', label: '给排水系统' },
  { id: 'environmentIndex', label: '环境监控' },
  { id: 'lightingIndex', label: '照明控制' },
  { id: 'testOverview', label: '智能锁控' },
  { id: 'testOverview', label: '智能联动' }
];

const isCollapse = ref(false);
const asideWidth = ref('20%');

function toggleCollapse() {
  isCollapse.value = !isCollapse.value;
  asideWidth.value = isCollapse.value ? '4%' : '20%';
}

const router = useRouter();
function lineUrl(url: string) {
  isCollapse.value = true;
  asideWidth.value = '4%';
  console.log('url:', url);
  router.push({
    name: url,
    query: { screen: url }
  });
}

let treeData: any[] = reactive([]);
function getMenuList(val: any[]) {
  treeData = val;
  console.log('父类拿到treeData:', treeData);
}

watch(
  () => treeData,
  (newVal) => {
    console.log('treeData:', newVal);
  }
);

let buttonNames: any[] = [];
let routeName = ref<string>();
emitter.on('getStairRoute', (val: string) => {
  debugger;
  console.log('楼梯路由11：', val, treeData);
  routeName.value = val;
  if (treeData && treeData.length > 0 && val) {
    treeData.forEach((item: any) => {
      if (item.id === val) {
        if (item.children && item.children.length > 0 && val != 'homeOverview') {
          buttonNames = [];
          item.children.forEach((child: any) => {
            buttonNames.push(child);
          });
          console.log('buttonNames:', buttonNames);
          targetArr.length = 0;
          processorList(buttonNames, targetArr);
          console.log('targetArr:', targetArr);
          return;
        } else if (item.children && item.children.length > 0 && val == 'homeOverview') {
          targetArr.length = 0;
          Object.assign(targetArr, initArr);
          console.log('targetArr:', targetArr);
          return;
        }
        if (!item.children && item.id == 'homeOverview') {
          targetArr.length = 0;
          Object.assign(targetArr, initArr);
          console.log('targetArr:', targetArr);
          return;
        }
      }
    });
  }
});

let targetArr: any[] = reactive([]);
Object.assign(targetArr, initArr);
function processorList(arr: any[], targetArr: any[]) {
  arr.forEach((item: any) => {
    if (item.children && item.children.length > 0) {
      processorList(item.children, targetArr);
    } else {
      targetArr.push(item);
    }
  });
}

onUnmounted(() => {
  emitter.off('getStairRoute');
});
</script>

<style lang="less" scoped>
.toggle-button {
  // 添加背景颜色
  background-color: var(--base-container-flex-color);
  // 设置文本大小
  font-size: 20px;
  // 设置文本行高
  // line-height: 24px;
  // 设置文本颜色
  color: rgb(121, 187, 255);
  // 设置文本居中
  text-align: center;
  // 设置文本间距
  letter-spacing: 0.2em;
  // 设置鼠标悬浮变小手效果
  cursor: pointer;

  width: 100%;
}
.common-layout {
  width: 100%;
  height: 100vh;

  .el-container {
    width: 100%;
    height: 100vh;
    .el-header {
      width: 100%;
      height: 8vh;
      border-bottom: 2px solid #e0e0e0;
      //   background: #25af91;
      display: flex;
      align-items: center;
      justify-content: center;
      h2 {
        margin: 0px;
        padding: 0;
        text-align: center;
        // display: inline;
      }
    }
    .el-aside {
      width: 20%;
      height: 100%;
      border-right: 2px solid var(--base-container-frame-color);
      // background: #30b8b1;
      .el-scrollbar {
        height: 89vh;
        // background: #62db25;
        // border-right: 2px solid var(--base-container-frame-color); /* 添加右边框 */
      }
    }
    .el-main {
      margin: 0px;
      padding: 0px;
      width: 100%;
      flex: 1;
      min-height: 0;
      height: 94vh;
      flex: 1;
      min-height: 0;
      background: rgb(233, 233, 235);
      .el-main-core {
        // width: 100%;
        // height: 100%;
        margin-top: 0.5%;
        margin-left: 0.5%;

        background: rgb(255, 255, 255);
      }
    }
    .el-footer {
      width: 100%;
      height: 6vh;
      //   background: #4a6cde;
      padding: 0;
      display: flex;
      .footer-navigation {
        width: 100%;
        display: flex;
        .nav-item {
          margin: 0;
          border-radius: var(--el-border-radius-base);
          flex: 1;
          height: 100%;
        }
      }
    }
  }
}
</style>
