<script>
import { agentEdit, agentAdd } from '@/api/intelligent-agent/list'

export default {
  name: 'info',
  inject: ['dialog', 'fetchAgentList'],
  data() {
    return {
      rules: {
        name: [{ required: true, message: '请输入智能体名称', trigger: 'blur' }],
        description: [{ required: false, message: '请输入智能体描述', trigger: 'blur' }]
      }
    }
  },
  methods: {
    /**
     * 提交表单
     */
    async handleSubmit() {
      // 更新表单之后重新获取列表，然后退出 dialog
      await this.dialog.type === 'edit' ? agentEdit(this.dialog.agent) : agentAdd(this.dialog.agent)
      this.fetchAgentList()
      this.dialog.visible = false
    },
    validateForm() {
      this.$refs.form.validate(valid => {
        if (valid) this.handleSubmit()
      })
    }
  }
}

</script>

<template>
  <div>
    <el-form :model="dialog.agent" :rules="rules" ref="form">
      <el-form-item label="名称" prop="name" label-width="100px">
        <el-input v-model="dialog.agent.name" />
      </el-form-item>
      <el-form-item label="描述" prop="description" label-width="100px">
        <el-input type="textarea" :rows="8" v-model="dialog.agent.description" />
      </el-form-item>

      <el-row type="flex" justify="end">
        <el-button type="primary" @click="validateForm">保存</el-button>
        <el-button type="default" @click="dialog.visible = false">取消</el-button>
      </el-row>
    </el-form>
  </div>
</template>
