<template>
  <el-card shadow="never">
    <el-input v-model="sideTreeName" placeholder="名称" clearable>
      <template #prefix>
        <i-ep-search />
      </template>
    </el-input>

    <el-tree
      ref="sideTreeTreeRef"
      class="mt-2"
      :data="sideTreeList"
      :props="{ children: 'children', label: 'label', disabled: '' }"
      :expand-on-click-node="false"
      :filter-node-method="handleFilter"
      default-expand-all
      @node-click="handleNodeClick"
    />
  </el-card>
</template>

<script setup lang="ts">
import sideTreeAPI from "@/api/sLongDemo/sideTree";
const props = defineProps({
  modelValue: {
    type: [Number],
    default: undefined,
  },
});

const sideTreeList = ref<OptionType[]>();
const sideTreeTreeRef = ref(ElTree);
const sideTreeName = ref();

const emits = defineEmits(["node-click"]);

const sideTreeId = useVModel(props, "modelValue", emits);

watchEffect(
  () => {
    sideTreeTreeRef.value.filter(sideTreeName.value);
  },
  {
    flush: "post", // watchEffect会在DOM挂载或者更新之前就会触发，此属性控制在DOM元素更新后运行
  }
);

/** 部门筛选 */
function handleFilter(value: string, data: any) {
  if (!value) {
    return true;
  }
  return data.label.indexOf(value) !== -1;
}

/** 部门树节点 Click */
function handleNodeClick(data: { [key: string]: any }) {
  sideTreeId.value = data.value;
  emits("node-click");
}

onBeforeMount(() => {
  sideTreeAPI.getOptions().then((data) => {
    sideTreeList.value = data;
  });
});
</script>
