<template>
  <div class="filter" :class="wrapperCls">
    <h4 class="title sle" v-if="title">{{ title }}</h4>
    <el-input v-if="searchable" v-model="filterText" placeholder="输入关键字进行过滤" clearable :suffix-icon="Search" />
    <el-scrollbar :style="{ height: title ? `calc(100% - 95px)` : `calc(100% - 56px)` }">
      <el-tree
        ref="treeRef"
        :default-expand-all="defaultExpandAll"
        :node-key="id"
        :data="multiple ? treeData : treeAllData"
        :show-checkbox="multiple"
        :check-strictly="false"
        :current-node-key="!multiple ? selected : ''"
        :highlight-current="!multiple"
        :expand-on-click-node="expandOnClickNode"
        :check-on-click-node="multiple"
        :props="defaultProps"
        :filter-node-method="filterNode"
        :default-checked-keys="multiple ? selected : []"
        @node-click="handleNodeClick"
        @check="handleCheckChange"
      >
        <template #default="scope">
          <span class="el-tree-node__label">
            <slot :row="scope">
              {{ scope.node.label }}
            </slot>
          </span>
        </template>
      </el-tree>
    </el-scrollbar>
  </div>
</template>

<script setup lang="ts" name="TreeFilter">
import { ref, watch, onBeforeMount, nextTick } from "vue";
import { ElTree } from "element-plus";
import { Search } from "@element-plus/icons-vue";
import { pinyinFilter } from "@/utils/pinyin";

// 接收父组件参数并设置默认值
interface TreeFilterProps {
  requestApi?: (data?: any) => Promise<any>; // 请求分类数据的 api ==> 非必传
  dataCallback?: (data?: any) => any; // 返回的数据进行处理的方法 ==> 非必传
  data?: { [key: string]: any }[]; // 分类数据，如果有分类数据，则不会执行 api 请求 ==> 非必传
  title?: string; // treeFilter 标题 ==> 非必传
  id?: string; // 选择的id ==> 非必传，默认为 “id”
  label?: string; // 显示的label ==> 非必传，默认为 “label”
  multiple?: boolean; // 是否为多选 ==> 非必传，默认为 false
  defaultValue?: any; // 默认选中的值 ==> 非必传
  expandOnClickNode?: boolean; // 是否在点击节点的时候展开或者收缩节点， 默认值为 false false，则只有点箭头图标的时候才会展开或者收缩节点。
  wrapperCls?: string; // 外层类名 ==> 非必传
  searchable?: boolean; // 是否可搜索 ==> 非必传，默认为 true
  searchText?: string;
  defaultExpandAll?: boolean; // 是否默认展开所有节点 ==> 非必传，默认为 false
}
const props = withDefaults(defineProps<TreeFilterProps>(), {
  id: "id",
  label: "label",
  multiple: false,
  expandOnClickNode: false,
  wrapperCls: "card",
  searchable: true,
  searchText: "",
  defaultExpandAll: true
});

const defaultProps = {
  children: "children",
  label: props.label
};

const treeRef = ref<InstanceType<typeof ElTree>>();
const treeData = ref<{ [key: string]: any }[]>([]);
const treeAllData = ref<{ [key: string]: any }[]>([]);

const selected = ref();
const setSelected = () => {
  if (props.multiple) selected.value = Array.isArray(props.defaultValue) ? props.defaultValue : [props.defaultValue];
  else selected.value = typeof props.defaultValue === "string" ? props.defaultValue : "";
};

onBeforeMount(async () => {
  setSelected();
  if (props.requestApi) {
    let data = await props.requestApi!();
    props.dataCallback && (data = props.dataCallback(data));
    treeData.value = data;
    treeAllData.value = [{ id: "", [props.label]: "全部" }, ...data];
  }
});

// 使用 nextTick 防止打包后赋值不生效，开发环境是正常的
watch(
  () => props.defaultValue,
  () => nextTick(() => setSelected()),
  { deep: true, immediate: true }
);

watch(
  () => props.data,
  () => {
    if (props.data?.length) {
      treeData.value = props.data;
      treeAllData.value = [{ id: "", [props.label]: "全部" }, ...props.data];
    }
  },
  { deep: true, immediate: true }
);

const filterText = ref("");
watch(filterText, val => {
  console.log(" filterText val :>> ", val);
  treeRef.value!.filter(val);
});
watch(
  () => props.searchText,
  val => {
    console.log(" filterText val :>> ", val);
    treeRef.value!.filter(val);
  }
);

// 过滤
const filterNode = (value: string, data: { [key: string]: any }, node: any) => {
  if (!value) return true;
  let parentNode = node.parent,
    labels = [{ label: node.label }],
    level = 1;
  while (level < node.level) {
    labels.push({
      label: parentNode.label
    });
    parentNode = parentNode.parent;
    level++;
  }
  return pinyinFilter(value, labels, ["label"]).length > 0;
  // return labels.some(label => label.indexOf(value) !== -1);
};

interface FilterEmits {
  (e: "change", value: any): void;
}
const emit = defineEmits<FilterEmits>();

// 单选
const handleNodeClick = (data: { [key: string]: any }) => {
  if (props.multiple) return;
  console.log("data :>>", data);

  // // 访问当前选中节点的下级节点数值
  // const childrenValues = data.children.map((child: any) => child.value);
  // childrenValues.push(data.value);
  // console.log("Children Values:", childrenValues);

  // emit("change", data[props.id]);
  emit("change", data);
};

// 多选
const handleCheckChange = () => {
  emit("change", treeRef.value?.getCheckedKeys());
};

// 暴露给父组件使用
defineExpose({ treeData, treeAllData, treeRef });
</script>

<style scoped lang="scss">
@import "./index.scss";
</style>
