<template>
  <component
    :is="data?.render ?? `el-${data?.el}`"
    v-bind="{ ...handleSearchProps, ...placeholder, param, clearable }"
    v-model.trim="param[data?.key]"
    :data="data?.el === 'tree-select' ? columnEnum : []"
    :options="['cascader', 'select-v2'].includes(data?.el!) ? columnEnum : []"
  >
    <!-- <template #default="{ data }" v-if="data?.el === 'cascader'">
      <span>{{ data[fieldNames.label] }}</span>
    </template> -->
    <template v-if="data?.el === 'select'">
      <component
        :is="`el-option`"
        v-for="(col, index) in columnEnum"
        :key="index"
        :label="col[fieldNames.label]"
        :value="col[fieldNames.value]"
      ></component>
    </template>
    <template v-else-if="data?.el === 'checkbox-group'">
      <component
        :is="data.props?.define ? `el-checkbox` : `el-checkbox-button`"
        v-for="(col, index) in columnEnum"
        :key="index"
        :label="col[fieldNames.value]"
        >{{ col[fieldNames.label] }}</component
      >
    </template>
    <template v-else-if="data?.el === 'radio-group'">
      <component :is="`el-radio-button`" v-for="(col, index) in columnEnum" :key="index" :label="col[fieldNames.value]">{{
        col[fieldNames.label]
      }}</component>
    </template>
    <slot v-else></slot>
  </component>
</template>

<script setup lang="ts" name="HomeAttrSetting">
import { computed, inject, ref } from "vue";
import { handleProp } from "@/utils";
// import { ColumnProps } from "../interface";
import { useI18n } from "vue-i18n";

const { t } = useI18n();

interface BaseFormItem {
  data: {
    [key: string]: any;
  };
  param: { [key: string]: any };
}
const props = defineProps<BaseFormItem>();

// 判断 fieldNames 设置 label && value && children 的 key 值
const fieldNames = computed(() => {
  return {
    label: props.data.fieldNames?.label ?? "label",
    value: props.data.fieldNames?.value ?? "value",
    children: props.data.fieldNames?.children ?? "children"
  };
});

// 接收 enumMap (el 为 select-v2 需单独处理 enumData)
const enumMap = inject("homeAttrEnumMap", ref(new Map()));
const columnEnum = computed(() => {
  let enumData = enumMap.value.get(props.data.key);
  if (!enumData) return [];
  if (props.data?.el === "select-v2" && props.data.fieldNames) {
    enumData = enumData.map((item: { [key: string]: any }) => {
      return { ...item, label: item[fieldNames.value.label], value: item[fieldNames.value.value] };
    });
  }
  return enumData;
});

// 处理透传的 searchProps (el 为 tree-select、cascader 的时候需要给下默认 label && value && children)
const handleSearchProps = computed(() => {
  const label = fieldNames.value.label;
  const value = fieldNames.value.value;
  const children = fieldNames.value.children;
  const searchEl = props.data?.el;
  let searchProps = props.data?.props ?? {};
  if (searchEl === "tree-select") {
    searchProps = { ...searchProps, props: { ...searchProps.props, label, children }, nodeKey: value };
  }
  if (searchEl === "cascader") {
    searchProps = { ...searchProps, props: { ...searchProps.props, label, value, children } };
  }
  console.log("handleSearchProps searchProps :>> ", searchProps);
  return searchProps;
});

// 处理默认 placeholder
const placeholder = computed(() => {
  const search = props.data;
  if (["datetimerange", "daterange", "monthrange"].includes(search?.props?.type) || search?.props?.isRange) {
    return { rangeSeparator: t("common.to"), startPlaceholder: t("common.starttime"), endPlaceholder: t("common.endtime") };
  }
  const placeholder =
    search?.props?.placeholder ?? (search?.el?.includes("input") ? t("common.inputText") : t("common.chooseText"));
  return { placeholder };
});

// 是否有清除按钮 (当搜索项有默认值时，清除按钮不显示)
const clearable = computed(() => {
  const search = props.data;
  return search?.props?.clearable ?? (search?.defaultValue == null || search?.defaultValue == undefined);
});
</script>
