<template>
  <div style="width: 100%">
    <!-- 级联选择组件 -->
    <a-cascader
      v-model:value="dataValue"
      :options="dictList"
      @change="dataValueChange"
      @click="dataValueClick"
      @blur="dataValueBlur"
      @focus="dataValueFocus"
      :show-search="{ filter }"
      :multiple="props.record?.itemMultipleChoiceFlag"
      :field-names="{ label: 'name', value: valueType == 1 ? 'id' : 'code', children: 'children' }"
      :disabled="props.readonly || props.disabled"
      :placeholder="props.record?.placeholderContent || props.placeholder"
    />
  </div>
</template>

<script setup name="FormLinkSelect">
import { ref, onMounted, watch } from 'vue';
import Request from '@/utils/request/request-util';

const props = defineProps({
  value: {
    type: String,
    default: ''
  },
  //当前行配置
  record: {
    type: Object,
    default: {}
  },
  // 当前行数据
  formData: {
    type: Object,
    default: {}
  },
  //是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  placeholder: {
    type: String,
    default: '请选择'
  },
  // 是否只读
  readonly: {
    type: Boolean,
    default: false
  },
  // 是否是设计
  isDesgin: {
    type: Boolean,
    default: false
  }
});

const emits = defineEmits(['update:value', 'onChange', 'onClick', 'onBlur', 'onFocus']);

// 下拉取值 1: id;2: code
const valueType = ref(1);
// 数据来源
const dataFromType = ref(1);

// 选中的值
const dataValue = ref('');

// 可选中列表
const dictList = ref([]);

// 是否有关联字段
const linkFieldCode = ref('');

onMounted(() => {
  if (props.record?.formItemSelect?.valueType == 2) {
    valueType.value = 2;
  }
  if (props.record?.formItemSelect?.linkFieldCode) {
    linkFieldCode.value = props.record.formItemSelect.linkFieldCode;
  }
  if (props.record?.formItemSelect?.dataFromType) {
    dataFromType.value = props.record.formItemSelect.dataFromType;
  }
  getDictList();
  setValueData();
});

// 获取选择列表
const getDictList = searchText => {
  if (props.isDesgin || dataFromType.value != 1 || !props.record.formItemSelect.backendUrl) return;
  let params = {
    searchText: searchText
  };
  if (linkFieldCode.value) {
    params[linkFieldCode.value] = props.formData[linkFieldCode.value];
    params.valueType = valueType.value;
  }
  Request.get(props.record.formItemSelect.backendUrl, params).then(res => {
    props.record.dictList = res.data;
  });
};

// 更改值
const dataValueChange = () => {
  let data = null;
  if (dataValue.value?.length == 0) {
    data = '';
  } else {
    data = JSON.stringify(dataValue.value);
  }
  emits('update:value', data);
  emits('onChange', props.record);
};


const dataValueClick = () => {
  emits('onClick', props.record);
};

const dataValueBlur = () => {
  emits('onBlur', props.record);
};

const dataValueFocus = () => {
  emits('onFocus', props.record);
};

const setValueData = () => {
  // 多选
  if (props.value) {
    dataValue.value = JSON.parse(props.value);
  } else {
    dataValue.value = [];
  }
};

// 搜索
const filter = (inputValue, path) => {
  return path.some(option => option.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
}

watch(
  () => props.formData[linkFieldCode.value],
  val => {
    getDictList();
  },
  { deep: true }
);

watch(
  () => props.value,
  val => {
    setValueData();
  },
  { deep: true }
);

watch(
  () => props.record.dictList,
  val => {
    if (val) {
      dictList.value = val;
    }
  },
  { deep: true, immediate: true }
);
</script>

<style></style>
