<template>
  <el-dialog
    v-model="visible"
    class="page-im-msg-biz-skills-add-or-update"
    :title="!dataForm.id ? $t('admin.add') : '编辑'"
    :close-on-click-modal="false"
    top="5vh"
    width="780px"
    @close="dialogClose"
  >
    <el-form
      ref="dataFormRef"
      class="dialog-form"
      :model="dataForm"
      :rules="dataRule"
      label-width="auto"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        label="关键字"
        prop="keywords"
      >
        <el-input
          v-model.trim="dataForm.keywords"
          maxlength="20"
        />
      </el-form-item>
      <el-form-item
        label="回复内容"
        prop="content"
      >
        <el-input
          v-model="dataForm.content"
          class="content"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 6 }"
          maxlength="1000"
        />
      </el-form-item>
      <el-form-item
        label="备注"
        prop="remark"
      >
        <el-input
          v-model="dataForm.remark"
          type="textarea"
          maxlength="100"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">{{
          $t("chat.cancel")
        }}</el-button>
        <el-button
          type="primary"
          @click="onSubmit()"
        >{{
          $t("chat.confirm")
        }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import * as http from '@/api/im-msg-biz-skills'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refreshDataList'])
const dataFormRef = ref(null)

const visible = ref(false)
const dataForm = ref({
  id: null,
  keywords: '',
  content: '',
  remark: null
})
const isSubmit = ref(false)
const dataRule = {
  keywords: [
    { required: true, message: '关键字不能为空', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '回复内容不能为空', trigger: 'blur' }
  ]
}

const init = (id = null) => {
  dataForm.value.id = id || ''
  visible.value = true
  dialogClose()
  isSubmit.value = false
  if (dataForm.value.id) {
    http.getSkills(id)
      .then((data) => {
        dataForm.value = data
      })
  }
}
// 表单提交
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      if (isSubmit.value) {
        return false
      }
      const obj = JSON.parse(JSON.stringify(dataForm.value))
      if (!obj.id) {
        delete obj.id
      }
      isSubmit.value = true
      http[dataForm.value.id ? 'putSkills' : 'postSkills'](obj)
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              visible.value = false
              emit('refreshDataList')
              dataForm.value.content = ''
            }
          })
        }).catch((e) => {
          isSubmit.value = false
        })
    }
  })
}
// 重置表单
const dialogClose = () => {
  nextTick(() => {
    dataFormRef.value?.resetFields()
  })
}

defineExpose({ init })
</script>
