<template>
  <m-form ref="form" class="mt-5" :items="formItems" v-model="formValues"></m-form>
</template>

<script>
import MForm from '@/components/MForm'

import { metadata, metadataType } from '@/utils/dataGovernance'
import { api } from '@/api/dataGovernance'

export default {
  name: 'metadata-edit',
  components: { MForm },
  props: {
    itemData: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      formValues: {},
      pageInfo: {
        size: 50,
        current: 1
      },
      total: 0,
      loading: false,
      tagItems: []
    }
  },
  computed: {
    formItems () {
      return [
        {
          type: 'text',
          key: 'name',
          label: '请输入中文名称 *',
          rules: [v => !!v || '请输入中文名称']
        },
        {
          type: 'text',
          key: 'en_name',
          label: '请输入英文名称 *',
          rules: [v => !!v || '请输入英文名称']
        },
        {
          type: 'autocomplete',
          key: 'category',
          label: '请选择分类 *',
          noAttach: true,
          rules: [v => !!v || '请选择分类'],
          items: metadata
        },
        {
          type: 'text',
          key: 'alias',
          label: '请输入别名'
        },
        {
          type: 'text',
          key: 'affiliation',
          label: '请输入制作单位'
        },
        {
          type: 'autocomplete',
          key: 'type',
          label: '请选择数据类型 *',
          col: 6,
          noAttach: true,
          rules: [v => !!v || '请选择数据类型'],
          items: metadataType
        },
        {
          type: 'autocomplete',
          key: 'tag',
          value: null,
          label: '请选择标签',
          col: 6,
          noAttach: true,
          itemText: 'name',
          itemValue: 'id',
          items: this.tagItems
        },
        {
          type: 'text',
          key: 'describe',
          label: '请输入描述'
        },
        {
          type: 'ifRadio',
          key: 'status',
          value: 1,
          label: '启用',
          width: 120,
          items: [{ label: '是', value: 1 }, { label: '否', value: 0 }]
        }
      ]
    }
  },
  created () {
    this.init()
    if (!this.itemData.id) {
      return
    }
    this.formItems.forEach(item => {
      this.formValues[item.key] = this.itemData[item.key]
    })
  },
  methods: {
    async init () {
      try {
        this.loading = true
        try {
          const { data } = await api.dataLabelList({
            ...this.pageInfo
          })
          this.tagItems = data.records
          this.total = data.total
        } catch (error) {
          this.$snackbar.error(error)
        } finally {
          this.loading = false
        }
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    getValue () {
      if (!this.$refs.form.validate()) {
        return
      }
      const id = this.itemData.id
      return {
        ...this.formValues,
        id
      }
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
