<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'
import type { ExperimentDiscussType } from '../types'
import { ElMessage } from 'element-plus'
import { addExperimentDiscussComment } from '../api'

interface Props {
  data: ExperimentDiscussType
}
const props = defineProps<Props>()

const emit = defineEmits<{
  (e: 'update'): void
  (e: 'update:modelValue', visible: boolean): void
}>()

const formRef = $ref<FormInstance>()
const form = reactive({ content: '' })
const rules = ref<FormRules>({
  content: [{ required: true, message: '请输入话题评论内容', trigger: 'blur' }]
})

// 提交
function handleSubmit() {
  formRef?.validate().then(handleAdd)
}
// 发布评论
function handleAdd() {
  const params = { ...form, discussion_id: props.data.id }
  addExperimentDiscussComment(params).then(() => {
    ElMessage({ message: '评论成功', type: 'success' })
    emit('update')
    emit('update:modelValue', false)
  })
}
</script>

<template>
  <el-dialog
    title="话题评论"
    :close-on-click-modal="false"
    width="600px"
    @update:modelValue="$emit('update:modelValue')"
  >
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item prop="content">
        <el-input type="textarea" v-model="form.content" :autosize="{ minRows: 4, maxRows: 6 }" />
      </el-form-item>
      <el-row justify="center">
        <el-button round auto-insert-space @click="$emit('update:modelValue', false)">取消</el-button>
        <el-button type="primary" round auto-insert-space @click="handleSubmit">评论</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
