<template>
  <MForm ref="form" :items="formItems" v-model="formValues"></MForm>
</template>

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

export default {
  name: 'CreateOrderDialog',
  components: {
    MForm
  },
  props: {
    itemData: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      formValues: {
        title: '',
        description: '',
        created_by: '',
        extracted_domains: [],
        extracted_fields: [],
        extraction_purpose: ''
      }
    }
  },
  computed: {
    formItems () {
      return [
        {
          type: 'text',
          key: 'title',
          label: '订单标题 *',
          placeholder: '请输入订单标题，例如：员工与部门关联数据',
          rules: [
            v => !!v || '请输入订单标题',
            v => (v && v.length <= 200) || '标题不能超过200个字符'
          ],
          maxlength: 200,
          counter: true,
          outlined: true,
          dense: true,
          hideDetails: 'auto'
        },
        {
          type: 'textarea',
          key: 'description',
          label: '需求描述 *',
          placeholder: '请详细描述需要什么数据，包括： 1. 涉及的业务领域（如员工、部门、项目等） 2. 需要的具体字段 3. 数据用途',
          rules: [
            v => !!v || '请输入需求描述',
            v => (v && v.length >= 10) || '描述至少需要10个字符'
          ],
          maxlength: 2000,
          counter: true,
          outlined: true,
          rows: 8,
          hideDetails: 'auto'
        },
        {
          type: 'text',
          key: 'created_by',
          label: '创建人',
          outlined: true,
          dense: true,
          hideDetails: 'auto'
        }
      ]
    }
  },
  created () {
    if (this.itemData && this.itemData.id) {
      this.formValues = {
        title: this.itemData.title || '',
        description: this.itemData.description || '',
        extracted_domains: this.itemData.extracted_domains || [],
        extracted_fields: this.itemData.extracted_fields || [],
        extraction_purpose: this.itemData.extraction_purpose || ''
      }
    }
  },
  methods: {
    getValue () {
      if (!this.$refs.form.validate()) {
        return
      }
      const params = {
        title: this.formValues.title,
        description: this.formValues.description,
        created_by: this.formValues.created_by
      }
      return this.itemData.id ? this.formValues : params
    }
  }
}
</script>
