<template>
  <el-dialog 
    title="查看详情" 
    v-model="visible"
    :close-on-click-modal="false" 
    draggable
    width="900px">
    <el-descriptions :column="2" border v-loading="loading" v-if="detailData">
      <el-descriptions-item label="学年">{{ detailData.schoolYear || '-' }}</el-descriptions-item>
      <el-descriptions-item label="学期">
        <span>{{ formatSchoolTerm(detailData.schoolTerm) }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="学院">{{ detailData.deptName || '-' }}</el-descriptions-item>
      <el-descriptions-item label="班号">{{ detailData.classNo || '-' }}</el-descriptions-item>
      <el-descriptions-item label="班主任">{{ detailData.teacherRealName || '-' }}</el-descriptions-item>
      <el-descriptions-item label="人数">{{ detailData.num || '-' }}</el-descriptions-item>
      <el-descriptions-item label="请假开始时间">
        <span>{{ detailData.startTime ? formatDateTime(detailData.startTime) : '-' }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="请假结束时间">
        <span>{{ detailData.endTime ? formatDateTime(detailData.endTime) : '-' }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="请假类型">
        <span>{{ formatLeaveType(detailData.leaveType) }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="是否住宿">
        <span>{{ formatYesNo(detailData.stayDorm) }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="时段请假">
        <span>{{ formatYesNo(detailData.isSegment) }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="请假时段" :span="2">{{ detailData.segmentString || '-' }}</el-descriptions-item>
      <el-descriptions-item label="请假校门">
        <span>{{ formatSchoolDoor(detailData.schoolDoor) }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="基础部审核">
        <span>{{ formatAuditType(detailData.deptAudit) }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="学工处审批">
        <span>{{ formatAuditType(detailData.schoolAudit) }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="驳回原因" :span="2">{{ detailData.rejectReason || '-' }}</el-descriptions-item>
      <el-descriptions-item label="请假事由" :span="2">{{ detailData.reason || '-' }}</el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">关 闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ClassLeaveApplyDetailDialog">
import { ref, onMounted } from 'vue'
import { getDetail } from '/@/api/stuwork/classleaveapply'
import { getDicts } from '/@/api/admin/dict'

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const detailData = ref<any>(null)
const schoolTermList = ref<any[]>([])
const leaveTypeList = ref<any[]>([])
const yesNoList = ref<any[]>([])
const auditTypeList = ref<any[]>([])
const schoolDoorList = ref<any[]>([])

// 校门列表
const schoolDoorListData = [
  { label: '校门东', value: '0' },
  { label: '校门南', value: '1' },
  { label: '校门西', value: '2' }
]

// 格式化学期
const formatSchoolTerm = (value: string | number) => {
  if (value === null || value === undefined || value === '') {
    return '-'
  }
  const dictItem = schoolTermList.value.find(item => item.value == value)
  return dictItem ? dictItem.label : value
}

// 格式化日期时间
const formatDateTime = (dateTime: string) => {
  if (!dateTime) return '-'
  if (dateTime.includes(' ')) {
    return dateTime.split('.')[0]
  }
  return dateTime
}

// 格式化请假类型
const formatLeaveType = (value: string) => {
  if (!value) return '-'
  const item = leaveTypeList.value.find((item: any) => item.value === value)
  return item ? item.label : value
}

// 格式化是否
const formatYesNo = (value: string) => {
  if (value === null || value === undefined || value === '') {
    return '-'
  }
  const dictItem = yesNoList.value.find(item => item.value == value)
  return dictItem ? dictItem.label : value
}

// 格式化校门
const formatSchoolDoor = (value: string) => {
  if (!value) return '-'
  const item = schoolDoorList.value.find((item: any) => item.value === value)
  return item ? item.label : value
}

// 格式化审核类型
const formatAuditType = (value: string) => {
  if (value === null || value === undefined || value === '') {
    return '-'
  }
  const dictItem = auditTypeList.value.find(item => item.value == value)
  return dictItem ? dictItem.label : value
}

// 打开弹窗
const openDialog = async (id: string) => {
  visible.value = true
  detailData.value = null
  loading.value = true
  try {
    const res = await getDetail(id)
    if (res.data) {
      detailData.value = res.data
    }
  } catch (err) {
  } finally {
    loading.value = false
  }
}

// 获取学期字典
const getSchoolTermDict = async () => {
  try {
    const res = await getDicts('school_term')
    if (res.data) {
      schoolTermList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    schoolTermList.value = []
  }
}

// 获取请假类型字典
const getLeaveTypeDict = async () => {
  try {
    const res = await getDicts('leave_type')
    if (res.data) {
      leaveTypeList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    leaveTypeList.value = []
  }
}

// 获取是否字典
const getYesNoDict = async () => {
  try {
    const res = await getDicts('yes_no')
    if (res.data) {
      yesNoList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    yesNoList.value = []
  }
}

// 获取审核类型字典
const getAuditTypeDict = async () => {
  try {
    const res = await getDicts('class_audit_type')
    if (res.data) {
      auditTypeList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    auditTypeList.value = []
  }
}

// 初始化
onMounted(() => {
  schoolDoorList.value = schoolDoorListData
  getSchoolTermDict()
  getLeaveTypeDict()
  getYesNoDict()
  getAuditTypeDict()
})

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

<style scoped lang="scss">
</style>

