<template>
  <Dialog :title="formType === 'details' ? '详情' : t('common.audit')" v-model="dialogVisible">
		<el-descriptions title="" border :column="1">
      <el-descriptions-item label="学校名称">{{ formatName(info?.school?.name) }}</el-descriptions-item>
      <el-descriptions-item label="老师昵称">{{ info?.name || '-' }}</el-descriptions-item>
      <el-descriptions-item label="联系电话">{{ info?.phone || '-' }}</el-descriptions-item>
			<el-descriptions-item label="负责院系">
        {{ info?.authDept && info?.authDept.length > 0 ? info?.authDept.map(item => item.name).join('、') : '-' }}
      </el-descriptions-item>
      <el-descriptions-item label="在岗证明图片">
        <el-image class="!w-80px h-80px" :src="info?.employmentCertificate || ''" hide-on-click-modal :preview-src-list="[info?.employmentCertificate]"/>
      </el-descriptions-item>
      <el-descriptions-item label="创建时间">{{ info?.createTime ? formatDate(info?.createTime) : '-' }}</el-descriptions-item>
      <el-descriptions-item v-if="info?.authStatus === '2'" label="审核拒绝原因">{{ info?.authMessage || '-' }}</el-descriptions-item>
    </el-descriptions>

    <el-form
			v-if="formType === 'refuse'"
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="80px"
			class="mt-50px"
      v-loading="formLoading"
    >
      <el-form-item label="拒绝原因" prop="authMessage">
        <el-input v-model="formData.authMessage" type="textarea" placeholder="请输入拒绝原因" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button v-if="formType === 'agree'" @click="handleAgree" type="success" :disabled="formLoading">通 过</el-button>
      <el-button v-if="formType === 'refuse'" @click="handleRefuse" type="danger" :disabled="formLoading">拒绝</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
/** 门墩儿-学校注册申请拒绝 表单 */
defineOptions({ name: 'SchoolRegisterRejectForm' })
import { formatName } from '@/utils'
import { formatDate } from '@/utils/formatTime'
import { FlameSchoolApi } from '@/api/menduner/schoolRegisterExamine'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const formRef = ref() // 表单 Ref
const formType = ref('')
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formRules = reactive({
  authMessage: [{ required: true, message: '拒绝原因不能为空', trigger: 'blur' }]
})
const formData = ref({
	authMessage: ''
})

const handleAgree = async () => {
	try {
		await message.confirm('确定要通过该学校注册申请吗？')

		await FlameSchoolApi.flameSchoolExaminePass({ id: info.value.id, authMessage: '' })
		dialogVisible.value = false
		message.success('操作成功')
    // 发送操作成功的事件
    emit('success')
	} catch {}
}

const handleRefuse = async () => {
	// 校验表单
  await formRef.value.validate()

  formLoading.value = true
  try {
    await message.confirm('确定要拒绝该学校注册申请吗？')

   	await FlameSchoolApi.flameSchoolExamineRefuse({ id: info.value.id, authMessage: formData.value.authMessage })
    formLoading.value = false
		dialogVisible.value = false
		message.success('操作成功')
    // 发送操作成功的事件
    emit('success')
  } catch (err) {
    message.error('拒绝失败')
  } finally {
    formLoading.value = false
		dialogVisible.value = false
  }
}

/** 打开弹窗 */
const info = ref({})
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const open = async (item: object, type: string) => {
	formType.value = type
  dialogVisible.value = true
  if (type === 'refuse') resetForm()

  info.value = item
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    authMessage: ''
  }
  formRef.value?.resetFields()
}
</script>