<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">
                  {{ person.pendingUpgradeByUnitId ? '待上级审批' : 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>
        
        <!-- 审批历史 -->
        <el-divider content-position="left">审批历史</el-divider>
        <el-timeline v-if="approvalHistory.length > 0">
          <el-timeline-item
            v-for="item in approvalHistory"
            :key="item.id"
            :timestamp="formatDate(item.reviewedAt)"
            :type="getActionType(item.action)"
            placement="top"
          >
            <el-card shadow="hover" class="history-card">
              <div class="history-content">
                <div class="history-action">
                  <el-tag :type="getActionType(item.action)" size="small">{{ getActionName(item.action) }}</el-tag>
                  <span v-if="item.reviewedByUnitName" class="history-unit">{{ item.reviewedByUnitName }}</span>
                </div>
                <div v-if="item.newLevel" class="history-level">
                  <span v-if="item.previousLevel">{{ getLevelName(item.previousLevel) }} → </span>
                  <el-tag :type="getLevelTagType(item.newLevel)" size="small">{{ getLevelName(item.newLevel) }}</el-tag>
                </div>
                <div v-if="item.comments" class="history-comments">
                  <el-icon><ChatDotRound /></el-icon>
                  <span>{{ item.comments }}</span>
                </div>
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
        <el-empty v-else description="暂无审批记录" :image-size="60" />
      </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, ChatDotRound } from '@element-plus/icons-vue'
import { personnelApi } from '@/api'
import type { PersonnelApprovalHistory } from '@/api/personnel'
import type { Personnel } from '@/types'
import { PersonnelStatus, PersonnelLevel } from '@/types'

const route = useRoute()

const person = ref<Personnel | null>(null)
const approvalHistory = ref<PersonnelApprovalHistory[]>([])
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 | null): string {
  switch (level) {
    case PersonnelLevel.Division:
    case 'Division': return '师级人才'
    case PersonnelLevel.Regiment:
    case 'Regiment': return '团级人才'
    case PersonnelLevel.Battalion:
    case 'Battalion': return '营级人才'
    case PersonnelLevel.Company:
    case 'Company': return '连级人才'
    default: return ''
  }
}

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

function getActionName(action: string): string {
  switch (action) {
    case 'Approved': return '审批通过'
    case 'Rejected': return '审批拒绝'
    case 'LevelUpgraded': return '等级升级'
    default: return action
  }
}

function getActionType(action: string): string {
  switch (action) {
    case 'Approved': return 'success'
    case 'Rejected': return 'danger'
    case 'LevelUpgraded': return 'primary'
    default: return 'info'
  }
}

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

async function loadPersonnel() {
  loading.value = true
  try {
    const id = Number(route.params.id)
    person.value = await personnelApi.getById(id)
    
    // 加载审批历史
    try {
      approvalHistory.value = await personnelApi.getApprovalHistory(id)
    } catch {
      // 审批历史加载失败不影响主要信息显示
      approvalHistory.value = []
    }
  } 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;
  margin: 0 auto;
}

.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;
}

/* 审批历史样式 */
.history-card {
  padding: 0;
}

.history-card :deep(.el-card__body) {
  padding: 12px 16px;
}

.history-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-action {
  display: flex;
  align-items: center;
  gap: 8px;
}

.history-unit {
  color: #606266;
  font-size: 13px;
}

.history-level {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: #909399;
}

.history-comments {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  padding: 8px 12px;
  background-color: #f5f7fa;
  border-radius: 4px;
  font-size: 13px;
  color: #606266;
}

.history-comments .el-icon {
  margin-top: 2px;
  color: #909399;
}

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