<template>
  <div class="pt-3">
    <form-list ref="form" :items="formItems"></form-list>
  </div>
</template>

<script>
import FormList from '@/components/Form/list'
import { metaDataTypes } from '@/api/dataBook'
export default {
  name: 'edit-mate-data-add',
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  components: { FormList },
  data () {
    return {
      formItems: {
        options: [
          {
            type: 'text',
            key: 'title',
            value: this.title,
            label: '请输入中文名称 *',
            outlined: true,
            dense: true,
            rules: [v => !!v || '请输入中文名称']
          },
          {
            type: 'text',
            key: 'dbColumn',
            value: null,
            label: '请输入英文名称 *',
            outlined: true,
            dense: true,
            rules: [v => !!v || '请输入英文名称']
          },
          {
            type: 'autocomplete',
            key: 'type',
            value: null,
            label: '请选择数据类型 *',
            outlined: true,
            noAttach: true,
            dense: true,
            itemText: 'title',
            itemValue: 'title',
            items: [],
            rules: [v => !!v || '请选择数据类型']
          },
          {
            type: 'text',
            key: 'remark',
            value: null,
            label: '请输入备注',
            outlined: true,
            dense: true
          }
        ]
      }
    }
  },
  created () {
    this.getMetaDataTypes()
  },
  methods: {
    // 获取元数据数据类型列表
    async getMetaDataTypes () {
      try {
        const { data } = await metaDataTypes()
        const item = this.formItems.options.find(_e => _e.key === 'type')
        item.items = data
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    getValue () {
      if (!this.$refs.form.validate()) {
        return
      }
      return this.formItems.options.reduce((res, item) => {
        res[item.key] = item.value
        if (item.key === 'type') {
          const val = item.items.find(_e => _e.title === item.value)
          res.dbType = val.dbType
          res.dbColumnLength = val.dbColumnLength
        }
        return res
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
