<script setup lang="ts">
import { computed } from 'vue';
import { buildWorkbenchProjectHealthCards } from '../homepage';
import { workbenchProjectHealthMock } from '../mock';
import { useWorkbenchRefresh } from '../composables/use-workbench-refresh';
import WorkbenchModuleCard from './workbench-module-card.vue';

interface Props {
  editing?: boolean;
}

withDefaults(defineProps<Props>(), { editing: false });
defineEmits<{ (e: 'hide'): void }>();

const { loading, refresh } = useWorkbenchRefresh();

const projectCards = computed(() => buildWorkbenchProjectHealthCards(workbenchProjectHealthMock));

// 产品维度 mock（蓝图 C15 要求扩到产品维度，未来后端接口落地后接通）
interface ProductHealth {
  id: string;
  name: string;
  health: 'green' | 'yellow' | 'red';
  healthLabel: string;
  activeRequirement: number;
  blockCount: number;
}
const productCards: ProductHealth[] = [
  { id: 'pr1', name: '商户后台', health: 'green', healthLabel: '良好', activeRequirement: 15, blockCount: 0 },
  { id: 'pr2', name: '收银台', health: 'green', healthLabel: '良好', activeRequirement: 8, blockCount: 0 },
  { id: 'pr3', name: '用户中心', health: 'yellow', healthLabel: '关注', activeRequirement: 11, blockCount: 1 }
];
</script>

<template>
  <WorkbenchModuleCard
    v-loading="loading"
    title="产品 / 项目健康度"
    icon="mdi:heart-pulse"
    :badge-count="projectCards.length + productCards.length"
    :editing="editing"
    @hide="$emit('hide')"
    @refresh="refresh"
  >
    <div class="demo-banner">
      <SvgIcon icon="mdi:alert-circle-outline" class="demo-banner__icon" />
      <div class="demo-banner__text">
        <b>演示态 · 当前为效果预览</b>
        <span>
          红/黄/绿健康结论依赖"风险登记 / 计划基线 / 需求阻塞与 SLA"，RDMS
          业务侧暂无对应流程，等业务流落地后再接通真实数据。
        </span>
      </div>
    </div>

    <div class="section-title">项目</div>
    <div class="health-list">
      <div v-for="card in projectCards" :key="card.projectId" class="health-card">
        <div class="health-card__ring" :class="`is-${card.health}`">
          <span>{{ card.healthLabel }}</span>
        </div>
        <div class="health-card__body">
          <div class="health-card__name">{{ card.projectName }}</div>
          <div class="health-card__meta">
            <ElTag v-if="card.riskCount > 0" size="small" type="danger">风险 {{ card.riskCount }}</ElTag>
            <ElTag size="small" type="warning">逾期任务 {{ card.overdueTasks }}</ElTag>
            <ElTag size="small">需求积压 {{ card.backlogRequirements }}</ElTag>
          </div>
        </div>
      </div>
    </div>

    <div class="section-title section-title--gap">产品</div>
    <div class="health-list">
      <div v-for="card in productCards" :key="card.id" class="health-card">
        <div class="health-card__ring" :class="`is-${card.health}`">
          <span>{{ card.healthLabel }}</span>
        </div>
        <div class="health-card__body">
          <div class="health-card__name">{{ card.name }}</div>
          <div class="health-card__meta">
            <ElTag size="small">活跃需求 {{ card.activeRequirement }}</ElTag>
            <ElTag v-if="card.blockCount > 0" size="small" type="warning">阻塞 {{ card.blockCount }}</ElTag>
            <ElTag v-else size="small" type="success">无阻塞</ElTag>
          </div>
        </div>
      </div>
    </div>
  </WorkbenchModuleCard>
</template>

<style scoped>
.demo-banner {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 10px;
  margin-bottom: 12px;
  background: var(--el-color-warning-light-9);
  border: 1px solid var(--el-color-warning-light-7);
  border-left: 3px solid var(--el-color-warning);
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.5;
}
.demo-banner__icon {
  color: var(--el-color-warning);
  font-size: 16px;
  flex-shrink: 0;
  margin-top: 1px;
}
.demo-banner__text {
  flex: 1;
  min-width: 0;
  color: var(--el-text-color-regular);
}
.demo-banner__text b {
  display: block;
  color: var(--el-color-warning-dark-2);
  font-weight: 600;
  margin-bottom: 2px;
}

.section-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--el-text-color-secondary);
  margin-bottom: 8px;
}
.section-title--gap {
  margin-top: 14px;
}
.health-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.health-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px;
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 8px;
}

.health-card__ring {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}

.health-card__ring.is-green {
  background: #10b981;
}

.health-card__ring.is-yellow {
  background: #f59e0b;
}

.health-card__ring.is-red {
  background: #ef4444;
}

.health-card__name {
  font-weight: 600;
  margin-bottom: 6px;
}

.health-card__meta {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
</style>
