<template>
  <div class="project-detail-panel">
    <!-- 项目头部 -->
    <div class="project-header">
      <div class="header-left">
        <el-button @click="backToList" link>
          <el-icon><ArrowLeft /></el-icon>
          返回列表
        </el-button>
      </div>
      <div class="header-center">
        <h2>{{ project.name }}</h2>
        <el-tag :type="getStatusTagType(project.status)" size="large">
          {{ getStatusText(project.status) }}
        </el-tag>
      </div>
      <div class="header-right">
        <el-button @click="exportProject">
          <el-icon><Download /></el-icon>
          导出项目
        </el-button>
        <el-button type="primary" @click="editProject">
          <el-icon><Edit /></el-icon>
          编辑项目
        </el-button>
      </div>
    </div>

    <!-- Tab切换 -->
    <el-tabs v-model="activeTab" class="project-tabs">
      <el-tab-pane label="基本信息" name="basic">
        <div class="basic-info-panel">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="项目名称">
              {{ project.name }}
            </el-descriptions-item>
            <el-descriptions-item label="招标单位">
              {{ project.client || 'XX科技有限公司' }}
            </el-descriptions-item>
            <el-descriptions-item label="创建时间">
              {{ project.createTime }}
            </el-descriptions-item>
            <el-descriptions-item label="当前进度">
              {{ project.progress }}
            </el-descriptions-item>
            <el-descriptions-item label="项目状态">
              <el-tag :type="getStatusTagType(project.status)">
                {{ getStatusText(project.status) }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="项目经理">
              张三
            </el-descriptions-item>
          </el-descriptions>

          <div class="project-timeline">
            <h3>项目流程</h3>
            <el-steps :active="getActiveStep(project.progress)" align-center>
              <el-step 
                v-for="(step, index) in projectSteps" 
                :key="index"
                :title="step.title"
                :description="step.description"
                :status="getStepStatus(project.progress, step.key)"
              >
                <template #icon>
                  <el-icon v-if="getStepStatus(project.progress, step.key) === 'finish'">
                    <CircleCheck />
                  </el-icon>
                  <el-icon v-else-if="getStepStatus(project.progress, step.key) === 'process'">
                    <Loading />
                  </el-icon>
                  <span v-else>{{ index + 1 }}</span>
                </template>
              </el-step>
            </el-steps>
          </div>
        </div>
      </el-tab-pane>
      
      <el-tab-pane name="submission">
        <template #label>
          <span class="tab-label">
            <el-icon><Document /></el-icon>
            文件提交
          </span>
        </template>
        <FileSubmission :project="project" @update="handleUpdate" />
      </el-tab-pane>
      
      <el-tab-pane name="bidding">
        <template #label>
          <span class="tab-label">
            <el-icon><Bell /></el-icon>
            开标信息
          </span>
        </template>
        <BiddingInfo :project="project" @update="handleUpdate" />
      </el-tab-pane>
      
      <el-tab-pane name="result">
        <template #label>
          <span class="tab-label">
            <el-icon><Trophy /></el-icon>
            结果公示
          </span>
        </template>
        <ResultAnnouncement :project="project" @update="handleUpdate" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  ArrowLeft, Download, Edit, Document, Bell, Trophy,
  CircleCheck, Loading
} from '@element-plus/icons-vue'
import FileSubmission from './FileSubmission.vue'
import BiddingInfo from './BiddingInfo.vue'
import ResultAnnouncement from './ResultAnnouncement.vue'

const props = defineProps({
  project: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['back', 'update'])

const activeTab = ref('basic')

// 项目流程步骤
const projectSteps = [
  { key: 'info-extract', title: '信息提取', description: '招标文件解析' },
  { key: 'doc-prepare', title: '文件准备', description: '标书文件生成' },
  { key: 'submission', title: '文件提交', description: '投标文件提交' },
  { key: 'bidding', title: '开标', description: '开标会议' },
  { key: 'evaluation', title: '评标', description: '评标过程' },
  { key: 'result', title: '结果公示', description: '中标公示' }
]

// 状态相关
const getStatusTagType = (status) => {
  const typeMap = {
    'ongoing': '',
    'pending': 'warning',
    'won': 'success',
    'lost': 'info',
    'failed': 'danger'
  }
  return typeMap[status] || 'info'
}

const getStatusText = (status) => {
  const textMap = {
    'ongoing': '进行中',
    'pending': '待提交',
    'won': '已中标',
    'lost': '未中标',
    'failed': '流标',
    '信息提取': '信息提取',
    '文件准备': '文件准备',
    '文件提交': '文件提交',
    '开标': '开标',
    '评标': '评标',
    '结果公示': '结果公示'
  }
  return textMap[status] || status
}

const getActiveStep = (progress) => {
  const stepMap = {
    '信息提取': 0,
    '文件准备': 1,
    '文件提交': 2,
    '开标': 3,
    '评标': 4,
    '结果公示': 5
  }
  return stepMap[progress] || 0
}

const getStepStatus = (progress, stepKey) => {
  const currentStep = getActiveStep(progress)
  const stepIndex = projectSteps.findIndex(s => s.key === stepKey)
  
  if (stepIndex < currentStep) return 'finish'
  if (stepIndex === currentStep) return 'process'
  return 'wait'
}

// 方法
const backToList = () => {
  emit('back')
}

const exportProject = () => {
  ElMessage.success('项目信息导出成功')
}

const editProject = () => {
  ElMessage.info('进入编辑模式')
}

const handleUpdate = () => {
  ElMessage.success('信息更新成功')
  emit('update')
}
</script>

<style lang="scss" scoped>
.project-detail-panel {
  .project-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 24px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    margin-bottom: 24px;

    .header-left {
      flex: 0 0 auto;
    }

    .header-center {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 16px;

      h2 {
        color: #fff;
        font-size: 24px;
        font-weight: 600;
        margin: 0;
      }
    }

    .header-right {
      flex: 0 0 auto;
      display: flex;
      gap: 12px;
    }
  }

  .project-tabs {
    :deep(.el-tabs__header) {
      margin-bottom: 24px;
    }

    .tab-label {
      display: flex;
      align-items: center;
      gap: 6px;
    }
  }

  .basic-info-panel {
    .project-timeline {
      margin-top: 32px;

      h3 {
        font-size: 18px;
        font-weight: 600;
        color: #1f2937;
        margin: 0 0 24px 0;
      }
    }
  }
}
</style>
