<template>
  <m-form ref="form" class="mt-5" :items="formItems" v-model="formValues">
    <template #en_name>
      <v-btn color="primary" class="ml-3" @click="getTranslate" :loading="translateLoading">翻译</v-btn>
    </template>
  </m-form>
</template>

<script>
import MForm from '@/components/MForm'
import {
  getTranslate
} from '@/api'

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 {
      translateLoading: false,
      formValues: {
        name: null,
        en_name: null,
        category: null,
        alias: null,
        affiliation: null,
        type: null,
        tag: null,
        describe: null,
        status: 1
      },
      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 || '请输入英文名称'],
          slotName: 'en_name'
        },
        {
          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.formValues = this.formItems.reduce((acc, cur) => {
      acc[cur.key] = this.itemData[cur.key]
      return acc
    }, {})
  },
  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)
      }
    },
    async getTranslate () {
      if (!this.formValues.name) {
        this.$snackbar.error('请输入名称')
        return
      }
      this.translateLoading = true
      try {
        const { data } = await getTranslate({
          node_name: this.formValues.name
        })
        this.formValues.en_name = data.translated
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.translateLoading = false
      }
    },
    getValue () {
      if (!this.$refs.form.validate()) {
        return
      }
      const id = this.itemData.id
      return {
        ...this.formValues,
        id
      }
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
