<template>
  <Dialog :title="formType === 'details' ? '详情' : t('common.audit')" v-model="dialogVisible">
		<el-descriptions title="" border :column="1">
      <el-descriptions-item label="学校名称">{{ formatName(info?.school?.schoolName) }}</el-descriptions-item>
      <el-descriptions-item label="老师昵称">{{ info?.school?.teacherNickname || '-' }}</el-descriptions-item>
      <el-descriptions-item label="联系电话">{{ info?.school?.phone || '-' }}</el-descriptions-item>
      <el-descriptions-item label="学校地址">{{ info?.school?.schoolAdderss || '-' }}</el-descriptions-item>
      <el-descriptions-item label="创建时间">{{ info?.school?.createDate ? formatDate(info?.school?.createDate) : '-' }}</el-descriptions-item>
			<el-descriptions-item label="负责院系">
        {{ info?.schoolDepartments && info?.schoolDepartments.length > 0 ? info?.schoolDepartments.map(item => item.departmentTitle).join('、') : '-' }}
      </el-descriptions-item>
      <el-descriptions-item label="在岗证明图片">
        <el-image class="!w-80px h-80px" :src="info?.schoolQualificaions?.jobImg || ''" hide-on-click-modal :preview-src-list="[info?.schoolQualificaions?.jobImg]"/>
      </el-descriptions-item>
			<el-descriptions-item label="身份证正面图片">
        <el-image class="!w-80px h-80px" :src="info?.schoolQualificaions?.schoolLegalPersonIdCardImg1 || ''" hide-on-click-modal :preview-src-list="[info?.schoolQualificaions?.schoolLegalPersonIdCardImg1]"/>
      </el-descriptions-item>
			<el-descriptions-item label="身份证背面图片">
        <el-image class="!w-80px h-80px" :src="info?.schoolQualificaions?.schoolLegalPersonIdCardImg2 || ''" hide-on-click-modal :preview-src-list="[info?.schoolQualificaions?.schoolLegalPersonIdCardImg2]"/>
      </el-descriptions-item>
      <el-descriptions-item v-if="refuseMessage && info?.school?.authStatus === '2'" label="审核拒绝原因">{{ refuseMessage || '-' }}</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="message">
        <el-input v-model="formData.message" 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({
  message: [{ required: true, message: '拒绝原因不能为空', trigger: 'blur' }]
})
const formData = ref({
	message: ''
})
const query = ref({})

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

		await FlameSchoolApi.flameSchoolExamine({ ...query.value, status: 1, message: '' })
		dialogVisible.value = false
		message.success('操作成功')
    // 发送操作成功的事件
    emit('success')
	} catch {}
}

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

  formLoading.value = true
  try {
   	await FlameSchoolApi.flameSchoolExamine({ ...query.value, status: 0, message: formData.value.message })
    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 refuseMessage = ref('')
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const open = async (id: Number, type: string) => {
	formType.value = type
  dialogVisible.value = true
  if (type === 'refuse') resetForm()

	if (id) {
		const data = await FlameSchoolApi.flameSchoolDetail({ schoolId: id })
		info.value = data || {}
		query.value = {
			title: '门墩儿管理员',
			userType: 1,
			entityId: data.school.schoolId
		}

    // 如果审核状态为拒绝，则获取拒绝理由
    if (data?.school?.authStatus === '2') {
      const result = await FlameSchoolApi.flameSchoolExamineReasonById({ schoolId: id })
      refuseMessage.value = result?.message
    }
	}
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

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