<template>
  <div class="feedback-panel">
    <div class="feedback-panel__header">
      <div class="feedback-panel__title">{{ dialogTitle }}</div>
      <el-button text @click="closeForm">
        <Icon icon="ep:close" />
      </el-button>
    </div>
    <div class="feedback-dialog" v-loading="formLoading">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-input v-model="formData.tags" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Editor v-model="formData.content" height="150px" />
        </el-form-item>
      </el-form>
    </div>
    <div class="feedback-footer">
      <el-button @click="closeForm">取 消</el-button>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { FeedbackApi, FeedbackVO } from '@/api/iot/feedback'

/** 用户反馈 表单 */
defineOptions({ name: 'FeedbackForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  title: undefined,
  content: undefined,
  tags: undefined
})
const formRules = reactive({
  title: [{ required: true, message: '标题不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await FeedbackApi.getFeedback(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const closeForm = () => {
  emit('closed')
}

/** 提交表单 */
const emit = defineEmits(['success', 'closed']) // 定义 success 和 closed 事件
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as FeedbackVO
    if (formType.value === 'create') {
      await FeedbackApi.createFeedback(data)
      message.success(t('common.createSuccess'))
    } else {
      await FeedbackApi.updateFeedback(data)
      message.success(t('common.updateSuccess'))
    }
    closeForm()
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    title: undefined,
    content: undefined,
    tags: undefined
  }
  formRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
.feedback-panel {
  background: #fff;
  border-radius: 12px;
  box-shadow: var(--el-box-shadow-light);
}

.feedback-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid #ebeef5;
}

.feedback-panel__title {
  color: #1f2937;
  font-size: 18px;
  font-weight: 600;
}

.feedback-dialog {
  max-height: calc(100vh - 220px);
  padding: 20px 20px 0;
  padding-right: 16px;
  overflow-y: auto;
}

.feedback-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  width: 100%;
  padding: 16px 20px 20px;
  border-top: 1px solid #ebeef5;
}

@media (max-width: 768px) {
  .feedback-panel__header {
    padding: 14px 16px;
  }

  .feedback-dialog {
    max-height: none;
    padding: 16px 16px 0;
    overflow-y: visible;
  }

  .feedback-footer {
    padding: 16px;
  }
}
</style>
