<template>
  <div class="icon-demo">
    <div v-for="(item, index) in allIcons" :key="index" class="icon-container">
      <h5 class="common-title">{{ item.label }}</h5>
      <div class="icon-list">
        <template v-for="icon in item.icons" :key="icon">
          <div class="card" @click.stop="getSvgName(icon)">
            <common-icon class="icon" :name="icon" :size="30"></common-icon>
            <span style="width: 80px" class="text-line-3">{{ icon }}</span>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue';
import { ElMessage } from 'element-plus';

const { proxy } = getCurrentInstance();

const common = import.meta.globEager(
  '../../packages/assets/icons/common/*.svg'
);

const tool = import.meta.globEager('../../packages/assets/icons/tool/*.svg');
const flow = import.meta.globEager('../../packages/assets/icons/flow/*.svg');
const menu = import.meta.globEager('../../packages/assets/icons/menu/*.svg');
const commonKeys = Object.keys(common);
const toolKeys = Object.keys(tool);
const flowKeys = Object.keys(flow);
const menuKeys = Object.keys(menu);

const commons = ref([]);
const tools = ref([]);
const flows = ref([]);
const menus = ref([]);

function getName(keys, file, target) {
  keys.forEach((item) => {
    const name = item
      .split(`../../packages/assets/icons/${file}/`)[1]
      .split('.')[0];
    target.push(`${file}-${name}`);
  });
}
// commonKeys.forEach((item) => {
//   const name = item
//     .split('../../packages/assets/icons/common/')[1]
//     .split('.')[0];
//   commons.value.push(`common-${name}`);
// });

getName(commonKeys, 'common', commons.value);
getName(toolKeys, 'tool', tools.value);
getName(flowKeys, 'flow', flows.value);
getName(menuKeys, 'menu', menus.value);
const allIcons = ref([
  { label: '公共icon', icons: commons },
  { label: '工具类icon', icons: tools },
  { label: '流程图icon', icons: flows },
  { label: 'menu菜单', icons: menus }
]);

function getSvgName(name) {
  const text = `<common-icon name="${name}" :size="18"></common-icon>`;
  proxy.$copyText(text).then(() => {
    console.log(text);
    ElMessage.success('复制成功');
  });
}
</script>

<style scoped lang="scss">
.icon-demo {
  padding: 26px;
  .icon-container {
    margin-bottom: 40px;
  }
  .common-title {
    font-size: 16px;
    color: #333;
    line-height: 24px;
    padding-bottom: 10px;
  }
}
.icon-list {
  display: flex;
  flex-wrap: wrap;
  .card {
    display: flex;
    flex-direction: column;
    // justify-content: center;
    align-items: center;
    margin-right: 10px;
    border: 1px solid #f0f0f0;
    width: 80px;
    text-align: center;
    padding: 10px;
    margin-bottom: 10px;
    cursor: pointer;
    .icon {
      margin-bottom: 5px;
    }
  }
}
</style>
