<template>
  <div class="folder-structure">
    <div v-for="(folder, key) in treeData" :key="key" class="folder-item">
      <div class="folder-main" @click="toggleFolder(key)">
        <span class="folder-toggle">
          <icon-down v-if="folder.expanded" />
          <icon-right v-else />
        </span>
        <span class="folder-name">{{ folder.abbreviation }}</span>
      </div>

      <div
        v-if="folder.expanded && folder.child.length > 0"
        class="folder-children"
      >
        <div
          v-for="child in folder.child"
          :key="child.name"
          class="child-folder"
          @click="handleChild(child)"
        >
          <FolderIcon
            class="folder-icon"
            :style="{
              fill: child.high
                ? '#ff4d4f'
                : child.medium
                  ? '#ffa940'
                  : child.low
                    ? '#52c41a'
                    : '#ffd700',
            }"
          />
          <span class="child-name">{{ child.abbreviation }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup name="TreePage">
import type { AlarmTotalRes, Tree } from '@/api/home';
import { watch, ref } from 'vue';
import useLoading from '@/hooks/loading';
// 导入 SVG 作为 Vue 组件
import FolderIcon from '@/assets/icon/folder.svg';
import router from '@/router';
const { loading, setLoading } = useLoading(true);
interface ListPageProps {
  data: AlarmTotalRes;
}
const props = withDefaults(defineProps<ListPageProps>(), {
  data: () => ({}) as AlarmTotalRes,
});
const treeData = ref<Tree[]>(props.data.tree);
watch(
  () => props.data,
  () => {
    treeData.value = props.data.tree;
    treeData.value.forEach(item => {
      item.expanded = true;
    });
    setLoading(false);
  }
);
// 切换文件夹展开/折叠状态
const toggleFolder = (folderName: number) => {
  if (treeData.value[folderName]) {
    treeData.value[folderName].expanded = !treeData.value[folderName].expanded;
  }
};

const handleChild = (folder: Tree) => {
  router.push({ name: 'Workplace', params: { station: folder.name } });
};
</script>
<style lang="less" scoped>
.folder-structure {
  margin: 0;
}

.folder-item {
  margin: 2px 0;
  cursor: pointer;
  transition: all 0.3s ease;
}

.folder-item:hover {
  background-color: var(--color-theme-2);
  border-radius: 5px;
}

.folder-main {
  display: flex;
  align-items: center;
  padding: 8px 0;
}

.folder-name {
  margin-top: 3px;
  color: var(--color-white);
  font-weight: 500;
  font-size: 16px;
}

.child-name {
  margin-top: 5px;
  color: var(--color-white);
  font-weight: 500;
  font-size: 14px;
}

.folder-toggle {
  width: 20px;
  margin-right: 8px;
  color: #4facfe;
  font-size: 14px;
  text-align: center;
}

.folder-children {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  margin-left: 10px;
  padding-left: 15px;
  border-left: 1px solid var(--color-border);
}

.child-folder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-left: 10px;
  padding: 5px 0;
  text-align: center;
}

.folder-icon {
  width: 40px;
  height: 25px;
}

.child-folder:hover {
  background: var(--color-border);
  border-radius: 5px;
}
</style>
