<template>
  <el-dialog v-model="visible" title="驳回" width="600px" :close-on-click-modal="false" destroy-on-close>
    <el-form label-width="100px">
      <el-form-item label="姓名 / 工号:">
        <el-tag>{{ dataForm.name }} - {{ dataForm.teacherNo }}</el-tag>
      </el-form-item>
      <el-form-item label="类型:">
        <el-input v-model="typeName" disabled />
      </el-form-item>
      <el-form-item label="驳回理由">
        <el-input
          type="textarea"
          v-model="dataForm.backReason"
          :rows="3"
          placeholder="请输入驳回理由"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button @click="saveData" :loading="loading" type="primary">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useMessage, useMessageBox } from '/@/hooks/message'
import { examObj as editTitle } from '/@/api/professional/professionaluser/professionaltitlerelation'
import { examObj as editQua } from '/@/api/professional/professionaluser/professionalqualificationrelation'
import { examObj as editCer } from '/@/api/professional/professionaluser/professionalteachercertificaterelation'
import { examObj as editEducation } from '/@/api/professional/professionaluser/professionalteacheracademicrelation'
import { examObj as editHonor } from '/@/api/professional/professionaluser/professionalteacherhonor'
import { examObj as editPaper } from '/@/api/professional/professionalteacherpaper'
import { examObj as editMaterial } from '/@/api/professional/professionalteachingmaterial'
import { examObj as editTopic } from '/@/api/professional/professionaltopiclist'

// Emits
const emit = defineEmits<{
  (e: 'refreshData'): void
}>()

// 消息提示
const message = useMessage()
const messageBox = useMessageBox()

// 对话框显示状态
const visible = ref(false)

// 加载状态
const loading = ref(false)

// 表单数据
const dataForm = reactive({
  name: '',
  id: null as string | number | null,
  backReason: '',
  teacherNo: ''
})

// 类型名称
const typeName = ref('')

// 类型
const type = ref('')

// 类型映射
const typeMap: Record<string, string> = {
  title: '职称',
  qua: '职业资格',
  cer: '教师资格证',
  education: '学历学位',
  honor: '综合表彰',
  paper: '教师论文',
  material: '教材',
  topic: '课题'
}

// API 方法映射
const apiMethodMap: Record<string, any> = {
  title: editTitle,
  qua: editQua,
  cer: editCer,
  education: editEducation,
  honor: editHonor,
  paper: editPaper,
  material: editMaterial,
  topic: editTopic
}

// 初始化
const init = (row: any, typeValue: string) => {
  type.value = typeValue
  typeName.value = typeMap[typeValue] || ''
  dataForm.name = row.realName || ''
  dataForm.teacherNo = row.teacherNo || ''
  dataForm.id = row.id || null
  dataForm.backReason = ''
  visible.value = true
}

// 保存数据
const saveData = async () => {
  if (!dataForm.backReason.trim()) {
    message.warning('请填写驳回理由')
    return
  }

  const apiMethod = apiMethodMap[type.value]
  if (!apiMethod) {
    message.error('未知的类型')
    return
  }

  await saveDataMethod(apiMethod)
}

// 保存数据方法
const saveDataMethod = async (method: any) => {
  const data = {
    id: dataForm.id,
    state: '-2',
    backReason: dataForm.backReason
  }

  try {
    await messageBox.confirm(`确认驳回, ${dataForm.name}的${typeName.value}申请?`)
    loading.value = true
    await method(data)
    message.success('操作成功')
    visible.value = false
    emit('refreshData')
  } catch (error: any) {
    // 用户取消或请求失败
    if (error !== 'cancel') {
      message.error(error?.msg || '操作失败')
    }
  } finally {
    loading.value = false
  }
}

// 暴露方法
defineExpose({
  init
})
</script>

<style scoped lang="scss">
.dialog-footer {
  text-align: right;
}
</style>
