<template>
  <component
    v-bind="getWidgetProps"
    :ref="widget.variableName"
    :is="getComponent"
    :style="getWidgetStyle"
    :disabled="getDisabledStatus()"
    :widget="widget"
    v-model="bindValue"
    :value="bindValue"
    @change="onValueChange"
    :change="onValueChange"
    @widgetClick="onWidgetClick"
  >
    <template
      v-if="form().mode === 'pc' && widget.widgetType !== SysCustomWidgetType.Cascader"
      v-slot="scope"
    >
      <template v-if="widget.widgetType === SysCustomWidgetType.Radio">
        <el-radio v-for="item in getAllDropdownData" :key="item.id" :value="item.id">
          {{ item.name }}
        </el-radio>
      </template>
      <template
        v-else-if="
          widget.widgetType === SysCustomWidgetType.Date ||
          widget.widgetType === SysCustomWidgetType.DateRange
        "
      >
        <div class="el-date-table-cell">
          <span class="el-date-table-cell__text">{{ scope.text }}</span>
        </div>
      </template>
      <template v-else-if="widget.widgetType === SysCustomWidgetType.CheckBox">
        <el-checkbox v-for="item in getAllDropdownData" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </template>
      <template v-else-if="widget.widgetType === SysCustomWidgetType.Select">
        <el-option
          v-for="item in getAllDropdownData"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </template>
      <template v-if="widget.widgetType === SysCustomWidgetType.Link">
        <span>{{ widget.props.showText || widget.showName }}</span>
      </template>
    </template>
    <template
      v-if="form().mode === 'mobile' && widget.widgetType !== SysCustomWidgetType.Input"
      #input
    >
      <van-radio-group
        v-bind="getWidgetProps"
        v-if="SysCustomWidgetType.Radio === widget.widgetType"
        v-model="tempValue"
      >
        <van-radio
          :class="{
            'van-vertical-item': getWidgetProps.direction === 'vertical',
          }"
          v-for="item in getAllDropdownData"
          :key="item.id"
          :name="item.id"
        >
          {{ item.name }}
        </van-radio>
      </van-radio-group>
      <van-checkbox-group
        v-bind="getWidgetProps"
        v-if="SysCustomWidgetType.CheckBox === widget.widgetType"
        v-model:value="tempValue"
      >
        <van-checkbox
          v-bind="getWidgetProps"
          :class="{
            'van-vertical-item': getWidgetProps.direction === 'vertical',
          }"
          v-for="item in getAllDropdownData"
          :key="item.id"
          :name="item.id"
          shape="square"
        >
          {{ item.name }}
        </van-checkbox>
      </van-checkbox-group>
      <van-rate
        v-bind="getWidgetProps"
        v-model:value="tempValue"
        v-if="SysCustomWidgetType.Rate === widget.widgetType"
      />
      <van-switch
        v-bind="getWidgetProps"
        v-model:value="tempValue"
        v-if="SysCustomWidgetType.Switch === widget.widgetType"
      />
      <van-uploader
        v-bind="getWidgetProps"
        v-if="SysCustomWidgetType.Upload === widget.widgetType && isUploadImage"
        :deletable="true"
      />
      <van-button
        v-if="SysCustomWidgetType.Upload === widget.widgetType && !isUploadImage"
        icon="plus"
        type="default"
        size="small"
        >上传文件</van-button
      >
      <van-stepper
        v-bind="getWidgetProps"
        v-model:value="tempValue"
        v-if="SysCustomWidgetType.Stepper === widget.widgetType"
      />
    </template>
    <template v-if="form().mode === 'mobile' && showRightIcon" #right-icon>
      <i class="van-icon van-icon-arrow van-cell__right-icon" />
    </template>
  </component>
</template>

<script setup lang="ts">
import {
  ElCascader,
  ElCheckboxGroup,
  ElDatePicker,
  ElInput,
  ElInputNumber,
  ElLink,
  ElRadioGroup,
  ElSelect,
  ElSwitch,
} from 'element-plus';
import { Field as VanField } from 'vant';
import { ANY_OBJECT } from '@/types/generic';
import { OnlineFormEventType, SysCustomWidgetType, SysOnlineFormType } from '@/common/staticDict';
import { SysOnlineColumnFilterType, SysOnlineFieldKind } from '@/common/staticDict/online';
import {
  treeDataTranslate,
  findTreeNode,
  findItemFromList,
  findTreeNodePath,
} from '@/common/utils';
import RichEditor from '@/components/RichEditor/index.vue';
import UserSelect from '@/components/UserSelect/index.vue';
import DeptSelect from '@/components/DeptSelect/index.vue';
import InputNumberRange from '@/components/InputNumberRange/index.vue';
import OnlineCustomLabel from './OnlineCustomLabel.vue';
import OnlineCustomUpload from './OnlineCustomUpload.vue';
import OnlineCustomTree from './OnlineCustomTree.vue';
import OnlineCustomChart from './OnlineCustomChart.vue';
import OnlineCustomDataSelect from './OnlineCustomDataSelect/index.vue';
import OnlineCustomText from './OnlineCustomText.vue';
import OnlineCustomImage from './OnlineCustomImage.vue';
import OnlineMobileSelectFilter from './mobile/MobileSelectFilter.vue';
import OnlineMobileInputFilter from './mobile/MobileInputFilter.vue';
import OnlineMobileSwitchFilter from './mobile/MobileSwitchFilter.vue';
import OnlineMobileDateRangeFilter from './mobile/MobileDateRangeFilter.vue';
import OnlineMobileNumbrRangeFilter from './mobile/MobileNumberRangeFilter.vue';
import { WidgetEmit, WidgetProps } from './types/widget';
import { useWidget } from './hooks/widget';

type ValueType = string | boolean | Date | number | ANY_OBJECT | Array<ANY_OBJECT>;

interface IEmit extends WidgetEmit {
  (event: 'change', value: ANY_OBJECT | undefined, params: ANY_OBJECT | null): void;
  (event: 'update:value', value: ValueType | undefined): void;
  (event: 'update:modelValue', value: ValueType | undefined): void;
  (event: 'update:widget', value: ANY_OBJECT): void;
  (event: 'input', value: ValueType | undefined): void;
}
const emit = defineEmits<IEmit>();

interface IProps extends WidgetProps {
  value?: ValueType;
  widget: ANY_OBJECT;
}
const pps = withDefaults(defineProps<IProps>(), {});

const form = inject('form', () => {
  return { isEdit: false } as ANY_OBJECT;
});
const parentWidget = inject<ANY_OBJECT | null>('parentWidget', null);

const { propsWidget, onWidgetClick } = useWidget(pps, emit);

const dictDataList = ref<ANY_OBJECT[]>([]);
const tempValue = ref();

const multiSelect = computed(() => {
  if (
    [
      SysCustomWidgetType.Select,
      SysCustomWidgetType.CheckBox,
      SysCustomWidgetType.Cascader,
      SysCustomWidgetType.Tree,
    ].indexOf(pps.widget.widgetType) === -1
  ) {
    return false;
  }
  if (pps.widget.widgetType === SysCustomWidgetType.CheckBox) return true;
  if (
    form().formType === SysOnlineFormType.QUERY ||
    form().formType === SysOnlineFormType.ADVANCE_QUERY
  ) {
    return (
      pps.widget.column &&
      pps.widget.column.filterType === SysOnlineColumnFilterType.MULTI_SELECT_FILTER
    );
  } else if (form().formType === SysOnlineFormType.REPORT) {
    return SysCustomWidgetType.CheckBox === pps.widget.widgetType;
  } else {
    return pps.widget.column && pps.widget.column.fieldKind === SysOnlineFieldKind.MULTI_SELECT;
  }
});
const isMobileFilter = computed(() => {
  return (
    [
      SysCustomWidgetType.MobileRadioFilter,
      SysCustomWidgetType.MobileCheckBoxFilter,
      SysCustomWidgetType.MobileInputFilter,
      SysCustomWidgetType.MobileDateRangeFilter,
      SysCustomWidgetType.MobileNumberRangeFilter,
      SysCustomWidgetType.MobileSwitchFilter,
    ].indexOf(pps.widget.widgetType) !== -1
  );
});
const getAllDropdownData = computed(() => {
  if (pps.widget.props.supportAll) {
    return [
      {
        id: '',
        name: '全部',
      },
      ...dictDataList.value,
    ];
  } else {
    return dictDataList.value;
  }
});
const getWidgetStyle = computed(() => {
  return {
    width: pps.widget.widgetType !== SysCustomWidgetType.Link ? '100%' : undefined,
  };
});
const getComponent = computed(() => {
  if (
    [
      SysCustomWidgetType.Text,
      SysCustomWidgetType.Image,
      SysCustomWidgetType.Upload,
      SysCustomWidgetType.Link,
    ].indexOf(pps.widget.widgetType) === -1 &&
    form().readOnly
  ) {
    return OnlineCustomLabel;
  }
  let mode = form().mode;

  switch (pps.widget.widgetType) {
    case SysCustomWidgetType.Label:
      return mode !== 'pc' ? VanField : OnlineCustomLabel;
    case SysCustomWidgetType.Input:
      return mode !== 'pc' ? VanField : ElInput;
    case SysCustomWidgetType.NumberInput:
      return ElInputNumber;
    case SysCustomWidgetType.NumberRange:
      return InputNumberRange;
    case SysCustomWidgetType.Switch:
      return mode !== 'pc' ? VanField : ElSwitch;
    case SysCustomWidgetType.Radio:
      return mode !== 'pc' ? VanField : ElRadioGroup;
    case SysCustomWidgetType.CheckBox:
      return mode !== 'pc' ? VanField : ElCheckboxGroup;
    case SysCustomWidgetType.Select:
      return mode !== 'pc' ? VanField : ElSelect;
    case SysCustomWidgetType.Cascader:
      return mode !== 'pc' ? VanField : ElCascader;
    case SysCustomWidgetType.Date:
      return mode !== 'pc' ? VanField : ElDatePicker;
    case SysCustomWidgetType.DateRange:
      return ElDatePicker;
    case SysCustomWidgetType.Upload:
      return mode !== 'pc' ? VanField : OnlineCustomUpload;
    case SysCustomWidgetType.RichEditor:
      return RichEditor;
    case SysCustomWidgetType.Link:
      return ElLink;
    case SysCustomWidgetType.UserSelect:
      return mode !== 'pc' ? VanField : UserSelect;
    case SysCustomWidgetType.DeptSelect:
      return mode !== 'pc' ? VanField : DeptSelect;
    case SysCustomWidgetType.DataSelect:
      return mode !== 'pc' ? VanField : OnlineCustomDataSelect;
    case SysCustomWidgetType.Tree:
      return OnlineCustomTree;
    case SysCustomWidgetType.Rate:
      return VanField;
    case SysCustomWidgetType.Stepper:
      return VanField;
    case SysCustomWidgetType.Calendar:
      return VanField;
    case SysCustomWidgetType.LineChart:
    case SysCustomWidgetType.PieChart:
    case SysCustomWidgetType.BarChart:
    case SysCustomWidgetType.ScatterChart:
    case SysCustomWidgetType.PivotTable:
    case SysCustomWidgetType.DataViewTable:
    case SysCustomWidgetType.Carousel:
    case SysCustomWidgetType.RichText:
    case SysCustomWidgetType.GaugeChart:
    case SysCustomWidgetType.FunnelChart:
    case SysCustomWidgetType.RadarChart:
    case SysCustomWidgetType.ProgressBar:
    case SysCustomWidgetType.ProgressCircle:
    case SysCustomWidgetType.DataCard:
    case SysCustomWidgetType.DataProgressCard:
    case SysCustomWidgetType.CommonList:
      return OnlineCustomChart;
    case SysCustomWidgetType.Text:
      return OnlineCustomText;
    case SysCustomWidgetType.Image:
      return OnlineCustomImage;
    case SysCustomWidgetType.MobileRadioFilter:
    case SysCustomWidgetType.MobileCheckBoxFilter:
      return OnlineMobileSelectFilter;
    case SysCustomWidgetType.MobileInputFilter:
      return OnlineMobileInputFilter;
    case SysCustomWidgetType.MobileSwitchFilter:
      return OnlineMobileSwitchFilter;
    case SysCustomWidgetType.MobileDateRangeFilter:
      return OnlineMobileDateRangeFilter;
    case SysCustomWidgetType.MobileNumberRangeFilter:
      return OnlineMobileNumbrRangeFilter;
    default:
      console.warn('widget getComponent 未知类型');
      return 'div';
  }
});
const getLinkHerf = computed(() => {
  let temp = pps.widget.widgetType === SysCustomWidgetType.Link ? pps.widget.props.href : undefined;
  if (
    !form().isEdit &&
    pps.widget.eventInfo &&
    typeof pps.widget.eventInfo[OnlineFormEventType.LINK_HERF] === 'function'
  ) {
    temp = pps.widget.eventInfo[OnlineFormEventType.LINK_HERF](
      pps.widget.props.href,
      form().instanceData(),
    );
  }
  return temp;
});
const isDictWidget = computed(() => {
  return (
    [
      SysCustomWidgetType.Select,
      SysCustomWidgetType.CheckBox,
      SysCustomWidgetType.Radio,
      SysCustomWidgetType.Cascader,
      SysCustomWidgetType.Tree,
      SysCustomWidgetType.MobileCheckBoxFilter,
      SysCustomWidgetType.MobileRadioFilter,
    ].indexOf(pps.widget.widgetType) !== -1
  );
});

const getColumnDataType = computed(() => {
  if (pps.widget == null || pps.widget.column == null) return undefined;
  switch (pps.widget.column.objectFieldType) {
    case 'String':
      return 'String';
    case 'Date':
      return 'Date';
    case 'Boolean':
      return 'Boolean';
    case 'Integer':
    case 'Long':
    case 'Float':
    case 'Double':
    case 'BigDecimal':
      return 'Number';
    default:
      return undefined;
  }
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const bindValue = computed<string | number | boolean | any[] | ANY_OBJECT | Date | undefined>({
  get() {
    let tempValue = pps.value === undefined ? '' : pps.value;
    if (isDictWidget.value) tempValue = tempValue == null ? '' : tempValue + '';
    if (multiSelect.value && pps.value && typeof tempValue === 'string') {
      tempValue = tempValue.split(',');
      if (Array.isArray(tempValue)) {
        tempValue = tempValue.filter(item => {
          return item != null && item !== '';
        });
      }
    }
    // 开关组件根据绑定字段类型转换值
    let bindColumnType = getColumnDataType.value;
    if (pps.widget.widgetType === SysCustomWidgetType.Switch) {
      // 如果开关组件的字段绑定了字典，并且是只读显示，那么显示字典的值
      let dictInfo = (pps.widget.column || {}).dictId;
      if ((this.form().readOnly || this.widget.props.readOnly) && dictInfo != null) {
        return tempValue;
      }
      // 根据绑定字段的类型，转换开关组件的值
      if (bindColumnType === 'Number') {
        tempValue = tempValue !== 0 && tempValue != null;
      } else if (bindColumnType === 'String') {
        tempValue = tempValue === 'true';
      } else if (bindColumnType !== 'Boolean') {
        tempValue = false;
      }
      return tempValue;
    }
    if (pps.widget.widgetType === SysCustomWidgetType.CheckBox) {
      return form().mode === 'mobile' ? undefined : tempValue || [];
    } else if (pps.widget.widgetType === SysCustomWidgetType.Cascader) {
      if (multiSelect.value) {
        // 多选
        if (Array.isArray(tempValue)) {
          return tempValue
            .map(item => {
              let temp = findTreeNodePath(dictDataList.value, item);
              if (Array.isArray(temp) && temp.length > 0) return temp;
              return null;
            })
            .filter(item => item != null);
        } else {
          let temp = findTreeNodePath(dictDataList.value, tempValue.toString());
          return temp || [];
        }
      } else {
        // 单选
        let temp = findTreeNodePath(dictDataList.value, tempValue.toString());
        return temp.length > 0 ? temp : tempValue;
      }
    } else if (pps.widget.widgetType === SysCustomWidgetType.NumberInput) {
      return tempValue as number;
    } else {
      return tempValue;
    }
  },
  set(val) {
    onValueInput(val as ANY_OBJECT | undefined);
  },
});
const isUploadImage = computed(() => {
  if (pps.widget.widgetType !== SysCustomWidgetType.Upload) return false;
  let column = pps.widget.bindData.column || pps.widget.column;
  return column ? column.fieldKind === SysOnlineFieldKind.UPLOAD_IMAGE : false;
});
const showRightIcon = computed(() => {
  return (
    [
      SysCustomWidgetType.Select,
      SysCustomWidgetType.Cascader,
      SysCustomWidgetType.Date,
      SysCustomWidgetType.UserSelect,
      SysCustomWidgetType.DeptSelect,
      SysCustomWidgetType.Calendar,
    ].indexOf(pps.widget.widgetType) !== -1
  );
});

const getWidgetProps = computed(() => {
  let props = {
    ...(pps.widget.props || {}),
  };
  // 日期组件，根据类型设置format
  if (
    pps.widget.widgetType === SysCustomWidgetType.Date ||
    pps.widget.widgetType === SysCustomWidgetType.DateRange
  ) {
    props['value-format'] = 'YYYY-MM-DD HH:mm:ss';
  }
  return {
    ...props,
    clearable: true,
    filterable: true,
    readonly:
      form().mode === 'mobile' && SysCustomWidgetType.Label === pps.widget.widgetType
        ? true
        : undefined,
    readOnly:
      pps.widget.widgetType === SysCustomWidgetType.Upload
        ? form().readOnly || props.readOnly
        : undefined,
    options:
      pps.widget.widgetType === SysCustomWidgetType.Cascader ? dictDataList.value : undefined,
    props:
      pps.widget.widgetType === SysCustomWidgetType.Cascader
        ? {
            label: 'name',
            value: 'id',
            multiple: multiSelect.value,
            checkStrictly: true,
          }
        : undefined,
    'picker-options':
      pps.widget.widgetType === SysCustomWidgetType.Date
        ? {
            disabledDate: (pps.widget.eventInfo || {})[OnlineFormEventType.DISABLED_DATE],
          }
        : undefined,
    operationList: pps.widget.operationList,
    multiple: multiSelect.value,
    'collapse-tags': multiSelect.value,
    dataList: pps.widget.widgetType === SysCustomWidgetType.Tree ? dictDataList.value : undefined,
    dictDataList: isMobileFilter.value ? getAllDropdownData.value : undefined,
    href: getLinkHerf.value,
    label:
      form().mode === 'mobile' && (getComponent.value === VanField || isMobileFilter.value)
        ? pps.widget.showName
        : undefined,
    widget: pps.widget,
  };
});

const getDisabledStatus = () => {
  const formWidgetAuth: ANY_OBJECT | null =
    form().formAuth && form().formAuth() && form().formAuth().pc
      ? form().formAuth().pc[pps.widget.variableName]
      : null;
  if (formWidgetAuth && formWidgetAuth.disabled) return true;
  if (pps.widget.eventInfo && pps.widget.eventInfo[OnlineFormEventType.DISABLE]) {
    return pps.widget.eventInfo[OnlineFormEventType.DISABLE](form().instanceData());
  } else {
    return pps.widget.props.disabled;
  }
};

const parseValue = (val: ValueType): ANY_OBJECT | number | boolean | string => {
  let bindColumnType = getColumnDataType.value;
  if (pps.widget?.widgetType === SysCustomWidgetType.Switch) {
    if (bindColumnType === 'Number') {
      return val ? 1 : 0;
    } else if (bindColumnType === 'String') {
      return val ? 'true' : 'false';
    } else if (bindColumnType !== 'Boolean') {
      return false;
    } else {
      return val;
    }
  }
  if (pps.widget.widgetType === SysCustomWidgetType.Cascader) {
    if (multiSelect.value && Array.isArray(val)) {
      return val
        .map((item: ANY_OBJECT) => {
          return item[item.length - 1];
        })
        .filter((item: ANY_OBJECT) => item != null);
    } else {
      return Array.isArray(val) ? val[val.length - 1] : undefined;
    }
  } else {
    return val as ANY_OBJECT;
  }
};
const onValueInput = (val: ANY_OBJECT | undefined) => {
  let tempValue: ValueType | undefined = undefined;
  if (val) {
    if (val instanceof InputEvent) {
      return;
    } else if (val instanceof Event) {
      if (val.target instanceof HTMLInputElement) {
        tempValue = val.target.value;
      }
    } else {
      tempValue = parseValue(val);
    }

    if (multiSelect.value && Array.isArray(tempValue) /*&& tempValue.length > 0*/) {
      tempValue = tempValue.join(',') + ',';
    }
  } else {
    tempValue = val;
  }
  emit('update:value', tempValue);
  emit('update:modelValue', tempValue);
  // ElDatePicker(DateRange)有了这个事件才能更新显示
  emit('input', tempValue);
};
const onValueChange = (
  val: ANY_OBJECT | undefined,
  selectRow: ANY_OBJECT | undefined = undefined,
) => {
  let tempVal: ValueType | undefined = undefined;
  let dictData = null;
  tempVal = parseValue(val);
  if (val != null) {
    if (multiSelect.value) {
      dictData = val
        .map((item: string) => {
          if (pps.widget.widgetType === SysCustomWidgetType.Cascader) {
            if (Array.isArray(item)) {
              item = item[item.length - 1];
            }
            return findTreeNode(dictDataList.value, item, 'id', 'children');
          } else {
            return findItemFromList(dictDataList.value, item, 'id');
          }
        })
        .filter((item: ANY_OBJECT) => item != null);
    } else {
      let id = val.toString();
      if (pps.widget.widgetType === SysCustomWidgetType.Cascader) {
        if (Array.isArray(val)) {
          val = val[val.length - 1];
        }
        dictData = findTreeNode(dictDataList.value, id, 'id', 'children');
      } else {
        dictData = findItemFromList(dictDataList.value, id, 'id');
      }
    }
  }
  emit('change', tempVal, {
    dictData: dictData,
    selectRow: selectRow,
  });
};

const loadDropdownData = () => {
  if (pps.widget == null || !isDictWidget.value) {
    return;
  }
  dictDataList.value = [];
  setTimeout(() => {
    let dictInfo = (pps.widget.props.dictInfo || {}).dict;
    if (dictInfo && form().getDictDataList) {
      let dictCall;
      if (form().pageCode != null) {
        // 报表字典
        dictCall = form().getDictDataList(dictInfo, form().getDropdownParams(pps.widget));
      } else {
        // 在线表单字典
        dictCall = form().getDictDataList(dictInfo, form().getDropdownParams(pps.widget));
      }
      dictCall
        .then((res: ANY_OBJECT[]) => {
          res.forEach((item: ANY_OBJECT) => {
            item.id = item.id + '';
            if (item.parentId) item.parentId = item.parentId + '';
          });
          // 级联组件将列表转换成树
          if (pps.widget.widgetType === SysCustomWidgetType.Cascader) {
            res = treeDataTranslate(res, 'id', 'parentId');
          }
          if (pps.widget.eventInfo && pps.widget.eventInfo[OnlineFormEventType.DROPDOWN_CHANGE]) {
            res = pps.widget.eventInfo[OnlineFormEventType.DROPDOWN_CHANGE](res);
          }
          dictDataList.value = res;
        })
        .catch((e: Error) => {
          console.log(e);
        });
    }
  }, 30);
};

const getHtml = () => {
  const refs = (getCurrentInstance()?.refs || {}) as ANY_OBJECT;
  if (pps.widget.widgetType === SysCustomWidgetType.RichEditor) {
    return refs[pps.widget.variableName] ? refs[pps.widget.variableName].getHtml() : undefined;
  }
};
const reset = () => {
  onValueInput(undefined);
  onValueChange(undefined);
  nextTick(() => {
    loadDropdownData();
  });
};
const refresh = () => {
  const refs = (getCurrentInstance()?.refs || {}) as ANY_OBJECT;
  if (
    refs[pps.widget.variableName] &&
    typeof refs[pps.widget.variableName].refresh === 'function'
  ) {
    refs[pps.widget.variableName].refresh();
  }
};

defineExpose({
  getHtml,
  reset,
  refresh,
});

watch(
  () => pps.widget,
  () => {
    if (pps.widget) loadDropdownData();
  },
  {
    deep: true,
    immediate: true,
  },
);

// watch(
//   () => pps.widget.props.dictInfo,
//   () => {
//     if (pps.widget) loadDropdownData();
//   },
//   {
//     deep: true,
//     immediate: true,
//   },
// );
// 如果父组件为数据表格或者列表组件，那么子组件绑定表必须跟父组件一致
watch(
  () => parentWidget,
  (newValue, oldValue) => {
    const widget = { ...propsWidget.value };
    if (parentWidget != null && propsWidget.value) {
      // 如果绑定的数据表改变了，修改子组件绑定的数据表
      if (parentWidget.bindData.tableId !== propsWidget.value.bindData.tableId) {
        widget.props.bindData = {
          ...parentWidget.bindData,
        };
        widget.showName = undefined;
        if (widget.widgetType === SysCustomWidgetType.Text) {
          widget.props.text = '文本内容';
        }
      }
    } else {
      if (newValue == null && oldValue != null) {
        widget.bindData = {
          dataType: 0,
          tableId: undefined,
          table: undefined,
          columnId: undefined,
          column: undefined,
        };
      }
    }
    propsWidget.value = widget;
  },
  {
    deep: true,
    immediate: true,
  },
);

onMounted(() => {
  propsWidget.value.hasParent = parentWidget != null;
});
</script>

<style scoped>
.van-vertical-item + .van-vertical-item {
  margin-top: 8px;
}
</style>
