<template>
  <div class="tag-select">
    <el-tabs v-model="activeName">
      <el-tab-pane v-for="item in groupTags" :label="item.name" :key="item.id" :name="item.id">
        <div v-if="item.children?.length === 0" class="empty-text">暂无数据</div>
        <el-tag
          size="large"
          v-for="tag in item.children"
          :key="tag.id"
          :class="{ active: modelValue.includes(tag.id) }"
          @click="selectAction(tag.id)"
        >
          {{ tag.name }}
        </el-tag>
      </el-tab-pane>
    </el-tabs>
    <div class="mt20" v-if="showDetails && selectTags.length > 0">
      <el-tag class="mr20 mb10" size="large" v-for="tag in selectTags" :key="tag.id" closable @close="removeAction(tag.id)">
        {{ tag.name }}
      </el-tag>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineProps, defineEmits, withDefaults, computed, onBeforeMount, defineExpose } from "vue";
import { Common } from "@/api/interface";

interface Props {
  showDetails?: Boolean;
  modelValue: number[];
  api: () => Promise<any>;
}
type TagMapValue = { patentId: number; item: { id: number; name: string } };
const props = withDefaults(defineProps<Props>(), { modelValue: () => [], showDetails: () => false });
const groupTags = ref<Common.ResTreeOptions[]>([]);
const activeName = ref<number | undefined>();

const emit = defineEmits(["update:modelValue"]);

const tagMap = computed(() => {
  const map: Map<number, TagMapValue> = new Map();
  groupTags.value.forEach(item => {
    item.children?.forEach(tag => {
      map.set(tag.id, { patentId: item.id, item: tag });
    });
  });
  return map;
});
const selectTags = computed(() => {
  const result: TagMapValue["item"][] = [];
  props.modelValue.forEach(id => {
    const tag = tagMap.value.get(id);
    if (tag) {
      result.push(tag.item);
    }
  });
  return result;
});

// 选择
function selectAction(id: number) {
  const defaultValue = [...props.modelValue];
  if (!defaultValue.includes(id)) {
    defaultValue.push(id);
    emit("update:modelValue", defaultValue);
  }
}

// 移除
function removeAction(id: number) {
  const defaultValue = [...props.modelValue];
  const index = defaultValue.indexOf(id);
  if (index > -1) {
    defaultValue.splice(index, 1);
    emit("update:modelValue", defaultValue);
  }
}

// 初始化
async function initData() {
  const res = await props.api();
  groupTags.value = res.data.filter((item: Common.ResTreeOptions) => item.children?.length);
  setFirstActived();
}

// 设置第一个激活
async function setFirstActived() {
  activeName.value = groupTags.value[0]?.id;
}

onBeforeMount(() => {
  initData();
});

defineExpose({
  setFirstActived
});
</script>

<style lang="scss" scoped>
.tag-select {
  width: 100%;
  .empty-text {
    width: 100%;
    font-size: 12px;
    color: #999999;
    text-align: center;
  }
  :deep(.el-tabs) {
    font-size: 16px;
    border: 1px solid #e4e7ed;
    .el-tag {
      height: 28px;
    }
    .el-tabs__item {
      color: #333333;
      &.is-top:nth-child(2) {
        padding-left: 20px;
      }
    }
    .el-tabs__nav-wrap::after {
      display: none;
    }
    .el-tabs__active-bar {
      height: 3px;
    }
    .el-tabs__header {
      margin: 0;
      background: #f5f7fa;
      border-bottom: 1px solid #e4e7ed;
    }
    .el-tab-pane {
      display: flex;
      align-items: center;
      height: 68px;
      padding: 0 10px;
      overflow-x: auto;
      .el-tag {
        margin-right: 20px;
        font-size: 16px;
        color: #333333;
        cursor: pointer;
        background: #f5f7fa;
        border: 1px solid #e4e7ed;
        border-radius: 4px;
        &.active {
          background: #d8dde4;
          border: 1px solid #e4e7ed;
        }
        &:last-of-type {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
