<template>
  <a-modal
    :visible="value"
    title="编辑标签"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form
      :form="form"
      v-if="formData"
    >
      <a-form-item label="名称">
        <a-input
          disabled
          v-decorator="[
            'name',
            {
              initialValue:formData.name,
              rules: [{ required: true, message: '请输入名称' }]
            }
          ]"
        ></a-input>
      </a-form-item>
      <a-form-item label="描述">
        <a-textarea
          v-decorator="[
            'description',
            {
              initialValue:formData.description,
              rules: [{ required: true, message: '请输入描述' }]
            }
          ]"
        >
        </a-textarea>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { setTicketTag } from '@/api/ticketSetting/ticket'
import modalMixin from '@/mixin/modalMixin'

export default {
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  props: {
    tags: {
      type: Array,
      default: () => []
    },
    formData: {
      type: Object,
      default: () => {}
    }
  },
  mixins: [modalMixin],
  methods: {
    handleOk () {
      const name = this.form.getFieldValue('name') || ''
      const description = this.form.getFieldValue('description') || ''
      const existTag = this.tags.find(item => {
        return item.name.toLowerCase() === name.toLowerCase() && item.id !== this.formData.id
      })

      if (existTag) {
        this.$message.error('输入的标签名或描述已存在，请重新输入')
        return
      }

      this.form.validateFields((err, values) => {
        if (!err) {
          setTicketTag(this.formData.id, {
            name,
            description
          }).then(res => {
            this.$message.success('修改成功')
            this.$emit('update')
            this.handleCancel()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
