<template>
  <div class="personnel-detail">
    <el-card v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>人员详情</span>
          <el-button @click="$router.back()">返回</el-button>
        </div>
      </template>
      
      <div v-if="person" class="detail-content">
        <el-row :gutter="40">
          <el-col :span="6">
            <div class="photo-section">
              <img v-if="person.photoPath" :src="person.photoPath" class="person-photo" />
              <div v-else class="no-photo">
                <el-icon :size="48"><User /></el-icon>
                <span>暂无照片</span>
              </div>
              <div class="status-badge">
                <el-tag :type="getStatusTagType(person.status)" size="large">
                  {{ getStatusName(person.status) }}
                </el-tag>
              </div>
            </div>
          </el-col>
          <el-col :span="18">
            <el-descriptions :column="3" border>
              <el-descriptions-item label="姓名">{{ person.name }}</el-descriptions-item>
              <el-descriptions-item label="单位">{{ person.unit || '-' }}</el-descriptions-item>
              <el-descriptions-item label="部职别">{{ person.position }}</el-descriptions-item>
              <el-descriptions-item label="军衔">{{ person.rank }}</el-descriptions-item>
              <el-descriptions-item label="士兵证号">{{ person.idNumber || '-' }}</el-descriptions-item>
              <el-descriptions-item label="专业岗位">{{ person.professionalTitle || '-' }}</el-descriptions-item>
              <el-descriptions-item label="政治面貌">{{ person.politicalStatus || '-' }}</el-descriptions-item>
              <el-descriptions-item label="文化程度">{{ person.educationLevel || '-' }}</el-descriptions-item>
              <el-descriptions-item label="民族">{{ person.ethnicity || '-' }}</el-descriptions-item>
              <el-descriptions-item label="籍贯">{{ person.hometown || '-' }}</el-descriptions-item>
              <el-descriptions-item label="出生年月">{{ person.birthDate || '-' }}</el-descriptions-item>
              <el-descriptions-item label="入伍年月">{{ person.enlistmentDate || '-' }}</el-descriptions-item>
              <el-descriptions-item label="特长" :span="3">{{ person.specialty || '-' }}</el-descriptions-item>
              <el-descriptions-item label="人员等级">
                <el-tag v-if="person.approvedLevel" :type="getLevelTagType(person.approvedLevel)">
                  {{ getLevelName(person.approvedLevel) }}
                </el-tag>
                <span v-else>-</span>
              </el-descriptions-item>
              <el-descriptions-item label="提交单位">{{ person.submittedByUnitName }}</el-descriptions-item>
              <el-descriptions-item label="审批单位">{{ person.approvedByUnitName || '-' }}</el-descriptions-item>
              <el-descriptions-item label="提交时间">{{ formatDate(person.submittedAt) }}</el-descriptions-item>
              <el-descriptions-item label="审批时间" :span="2">{{ person.approvedAt ? formatDate(person.approvedAt) : '-' }}</el-descriptions-item>
            </el-descriptions>
            
            <el-divider />
            
            <el-descriptions :column="1" border>
              <el-descriptions-item label="参加培训情况">
                <div class="text-content">{{ person.trainingParticipation || '-' }}</div>
              </el-descriptions-item>
              <el-descriptions-item label="取得成绩">
                <div class="text-content">{{ person.achievements || '-' }}</div>
              </el-descriptions-item>
            </el-descriptions>
            
            <div v-if="person.supportingDocuments" class="documents-section">
              <h4>佐证材料</h4>
              <el-link type="primary" :href="person.supportingDocuments" target="_blank">
                <el-icon><Document /></el-icon>
                查看文件
              </el-link>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Document } from '@element-plus/icons-vue'
import { personnelApi } from '@/api'
import type { Personnel } from '@/types'
import { PersonnelStatus, PersonnelLevel } from '@/types'

const route = useRoute()

const person = ref<Personnel | null>(null)
const loading = ref(false)

function getStatusName(status: PersonnelStatus): string {
  switch (status) {
    case PersonnelStatus.Pending: return '待审批'
    case PersonnelStatus.Approved: return '已批准'
    case PersonnelStatus.Rejected: return '已拒绝'
    default: return ''
  }
}

function getStatusTagType(status: PersonnelStatus): string {
  switch (status) {
    case PersonnelStatus.Pending: return 'warning'
    case PersonnelStatus.Approved: return 'success'
    case PersonnelStatus.Rejected: return 'danger'
    default: return 'info'
  }
}

function getLevelName(level: PersonnelLevel): string {
  switch (level) {
    case PersonnelLevel.Division: return '师级人才'
    case PersonnelLevel.Regiment: return '团级人才'
    case PersonnelLevel.Battalion: return '营级人才'
    case PersonnelLevel.Company: return '连级人才'
    default: return ''
  }
}

function getLevelTagType(level: PersonnelLevel): string {
  switch (level) {
    case PersonnelLevel.Division: return 'danger'
    case PersonnelLevel.Regiment: return 'warning'
    case PersonnelLevel.Battalion: return 'success'
    case PersonnelLevel.Company: return 'info'
    default: return 'info'
  }
}

function formatDate(dateStr: string): string {
  return new Date(dateStr).toLocaleString('zh-CN')
}

async function loadPersonnel() {
  loading.value = true
  try {
    person.value = await personnelApi.getById(Number(route.params.id))
  } catch {
    ElMessage.error('加载人员信息失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadPersonnel()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.photo-section {
  text-align: center;
}

.person-photo {
  width: 150px;
  height: 200px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}

.no-photo {
  width: 150px;
  height: 200px;
  background-color: #f5f7fa;
  border: 1px dashed #dcdfe6;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #909399;
}

.status-badge {
  margin-top: 12px;
}

.text-content {
  white-space: pre-wrap;
  line-height: 1.6;
}

.documents-section {
  margin-top: 20px;
}

.documents-section h4 {
  margin-bottom: 12px;
  color: #303133;
}

/* 缩短标签列宽度 */
:deep(.el-descriptions__label) {
  width: 120px !important;
  min-width: 120px !important;
}
</style>
