<template>
  <sd-list-picker
    single
    :load-list-data="loadFieldList"
    v-bind="pickerMixinProps"
    v-on="$listeners"
  />
</template>

<script>
import axios from '@/common/services/axios-instance'
import pickerMixin from '@/common/components/sd-picker/picker-mixin'
import components from './_import-components/sd-field-picker-import'
const getRealDataType = (item) => {
  // 获取控件实际的数据类型
  let dataType = item.dataType
  if (item?.attr?.selectionType) {
    dataType = item?.attr?.selectionType?.toLowerCase()
  }
  if (dataType === 'title' || dataType === 'selectlist' || dataType === 'selectTree') {
    dataType = 'string'
  }
  if (item?.attr?.showDate === 'false') {
    // 日期组件如果不显示日期，则定义为时间类型
    dataType = 'time'
  }
  return dataType
}
export default {
  name: 'SdFieldPicker',
  metaInfo: {
    title: '',
  },
  components,
  mixins: [pickerMixin],
  props: {
    formId: {
      type: String,
      default: undefined,
    },
    type: {
      type: String,
      default: undefined,
    },
    disabledTypes: {
      type: Array,
      default: () => [],
    },
    // 指定使用子表的字段
    childTableName: {
      type: String,
      default: undefined,
    },
  },
  data() {
    return {}
  },
  methods: {
    loadFieldList() {
      if (!this.formId) return Promise.resolve([])
      const filterFields = (item) => {
        if (item.attrFD?.cid === 'sd-linked-data') {
          return false
        }
        if (item.dataType === 'collection') {
          return false
        }
        if (this.type && getRealDataType(item) !== this.type) {
          return false
        }
        return true
      }

      return axios
        .get(`api/framework/v1/onlineform/getById/${this.formId}/true`)
        .then((res) => {
          let fields = res.data.fields
          // 有指定使用子表，则获取子表字段
          if (this.childTableName) {
            fields = res.data.fields.find((f) => f.name === this.childTableName)?.attr.dync || []
          }
          return fields.filter((f) => !this.disabledTypes?.includes(f.dataType))
        })
        .then((fields) => {
          // 把子表字段拉平到一层
          const childFields = fields
            .filter((item) => item.dataType === 'collection')
            .map((item) => {
              const subFields = [
                { name: 'id', caption: '文档唯一ID(系统)', dataType: 'string' },
              ].concat(item.attr.dync)
              subFields.forEach((i) => {
                i.name = item.name + '.' + i.name
                i.caption = item.caption + '.' + i.caption
              })
              return subFields.filter((f) => !this.disabledTypes?.includes(f.dataType))
            })
          return fields.concat(...childFields)
        })
        .then((fields) => {
          // 添加id字段
          fields.unshift({ name: 'id', caption: '文档唯一ID(系统)', dataType: 'string' })
          return fields.filter(filterFields).map((field) => ({
            id: field.name,
            name: field.caption,
            type: getRealDataType(field),
          }))
        })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
