<template>
  <div class="production-data-panel">
    <div class="panel-header">
      <h3>生产数据统计</h3>
    </div>

    <!-- 主要指标卡片 -->
    <div class="main-metrics">
      <div class="metric-card total-production">
        <div class="metric-icon">📊</div>
        <div class="metric-info">
          <div class="metric-label">总产量</div>
          <div class="metric-value">{{ productionData.totalProduction }}</div>
          <div class="metric-unit">件</div>
        </div>
      </div>

      <div class="metric-card efficiency">
        <div class="metric-icon">⚡</div>
        <div class="metric-info">
          <div class="metric-label">综合效率</div>
          <div class="metric-value">{{ productionData.efficiency }}</div>
          <div class="metric-unit">%</div>
        </div>
      </div>

      <div class="metric-card device-count">
        <div class="metric-icon">🏭</div>
        <div class="metric-info">
          <div class="metric-label">设备总数</div>
          <div class="metric-value">{{ productionData.deviceCount }}</div>
          <div class="metric-unit">台</div>
        </div>
      </div>
    </div>

    <!-- 设备状态分布 -->
    <div class="status-distribution">
      <h4>设备状态分布</h4>
      <div class="status-bars">
        <div class="status-bar running">
          <div class="bar-label">运行中</div>
          <div class="bar-container">
            <div class="bar-fill" :style="{ width: runningPercentage + '%' }"></div>
          </div>
          <div class="bar-value">{{ productionData.runningDevices }}</div>
        </div>

        <div class="status-bar warning">
          <div class="bar-label">警告</div>
          <div class="bar-container">
            <div class="bar-fill" :style="{ width: warningPercentage + '%' }"></div>
          </div>
          <div class="bar-value">{{ productionData.warningDevices }}</div>
        </div>

        <div class="status-bar stopped">
          <div class="bar-label">停止</div>
          <div class="bar-container">
            <div class="bar-fill" :style="{ width: stoppedPercentage + '%' }"></div>
          </div>
          <div class="bar-value">{{ productionData.stoppedDevices }}</div>
        </div>
      </div>
    </div>

    <!-- 产量趋势 -->
    <div class="production-trend">
      <h4>生产趋势</h4>
      <div class="trend-chart">
        <div class="chart-container">
          <svg class="trend-svg" viewBox="0 0 280 100">
            <defs>
              <linearGradient id="trendGradient" x1="0%" y1="0%" x2="0%" y2="100%">
                <stop offset="0%" style="stop-color:#00d4ff;stop-opacity:0.3" />
                <stop offset="100%" style="stop-color:#00d4ff;stop-opacity:0" />
              </linearGradient>
            </defs>
            <path
              :d="trendPath"
              fill="url(#trendGradient)"
              stroke="#00d4ff"
              stroke-width="2"
              fill-opacity="0.3"
            />
            <path :d="trendLine" fill="none" stroke="#00d4ff" stroke-width="2" />
          </svg>
        </div>
        <div class="trend-labels">
          <span v-for="(label, index) in trendLabels" :key="index">{{ label }}</span>
        </div>
      </div>
    </div>

    <!-- 实时产量 -->
    <div class="realtime-production">
      <h4>实时产量</h4>
      <div class="production-counter">
        <a-statistic
          :value="currentProduction"
          :precision="0"
          :value-style="{ color: '#00d4ff', fontSize: '28px' }"
        >
          <template #suffix>
            <span style="color: #7ec8e3; font-size: 14px">件/小时</span>
          </template>
        </a-statistic>
      </div>
    </div>

    <!-- 质量指标 -->
    <div class="quality-metrics">
      <h4>质量指标</h4>
      <div class="quality-grid">
        <div class="quality-item">
          <div class="quality-label">合格率</div>
          <div class="quality-value">98.5%</div>
        </div>
        <div class="quality-item">
          <div class="quality-label">不良率</div>
          <div class="quality-value bad">1.5%</div>
        </div>
        <div class="quality-item">
          <div class="quality-label">返工率</div>
          <div class="quality-value warning">2.3%</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { computed } from 'vue';
  import type { PropType } from 'vue';

  interface ProductionData {
    totalProduction: number;
    efficiency: number;
    deviceCount: number;
    runningDevices: number;
    warningDevices: number;
    stoppedDevices: number;
  }

  const props = defineProps({
    productionData: {
      type: Object as PropType<ProductionData>,
      required: true,
    },
  });

  const runningPercentage = computed(
    () => ((props.productionData.runningDevices / props.productionData.deviceCount) * 100).toFixed(1)
  );
  const warningPercentage = computed(
    () => ((props.productionData.warningDevices / props.productionData.deviceCount) * 100).toFixed(1)
  );
  const stoppedPercentage = computed(
    () => ((props.productionData.stoppedDevices / props.productionData.deviceCount) * 100).toFixed(1)
  );

  // 模拟产量趋势数据
  const trendData = [45, 52, 48, 65, 58, 72, 68, 80, 75, 85];
  const trendLabels = ['08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '00:00', '02:00'];

  const currentProduction = computed(() => {
    return trendData[trendData.length - 1];
  });

  const trendPath = computed(() => {
    const points = trendData.map((value, index) => {
      const x = (index / (trendData.length - 1)) * 280;
      const y = 100 - (value / 100) * 80;
      return `${x},${y}`;
    });
    return `M ${points.join(' L ')} L 280,100 L 0,100 Z`;
  });

  const trendLine = computed(() => {
    const points = trendData.map((value, index) => {
      const x = (index / (trendData.length - 1)) * 280;
      const y = 100 - (value / 100) * 80;
      return `${x},${y}`;
    });
    return `M ${points.join(' L ')}`;
  });
</script>

<style scoped>
  .production-data-panel {
    background: rgba(0, 20, 40, 0.9);
    border: 1px solid #00d4ff;
    border-radius: 8px;
    padding: 16px;
    backdrop-filter: blur(10px);
    box-shadow: 0 0 20px rgba(0, 212, 255, 0.3);
    color: #ffffff;
    max-height: 80vh;
    overflow-y: auto;
  }

  .panel-header {
    margin-bottom: 16px;
    border-bottom: 1px solid rgba(0, 212, 255, 0.3);
    padding-bottom: 12px;
  }

  .panel-header h3 {
    color: #00d4ff;
    margin: 0;
    font-size: 18px;
    text-shadow: 0 0 5px rgba(0, 212, 255, 0.5);
  }

  .main-metrics {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 20px;
  }

  .metric-card {
    display: flex;
    align-items: center;
    background: rgba(0, 40, 80, 0.5);
    border: 1px solid rgba(0, 212, 255, 0.3);
    border-radius: 8px;
    padding: 12px;
    transition: all 0.3s;
  }

  .metric-card:hover {
    border-color: #00d4ff;
    box-shadow: 0 0 15px rgba(0, 212, 255, 0.4);
    transform: translateX(-5px);
  }

  .metric-icon {
    font-size: 32px;
    margin-right: 12px;
  }

  .metric-info {
    flex: 1;
  }

  .metric-label {
    font-size: 12px;
    color: #7ec8e3;
    margin-bottom: 4px;
  }

  .metric-value {
    font-size: 28px;
    font-weight: bold;
    color: #00d4ff;
    text-shadow: 0 0 10px rgba(0, 212, 255, 0.5);
  }

  .metric-unit {
    font-size: 12px;
    color: #7ec8e3;
    margin-left: 4px;
  }

  .status-distribution {
    margin-bottom: 20px;
  }

  .status-distribution h4 {
    color: #7ec8e3;
    margin: 0 0 12px 0;
    font-size: 14px;
  }

  .status-bars {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .status-bar {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .bar-label {
    width: 50px;
    font-size: 12px;
    color: #ffffff;
  }

  .bar-container {
    flex: 1;
    height: 20px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 10px;
    overflow: hidden;
    position: relative;
  }

  .bar-fill {
    height: 100%;
    border-radius: 10px;
    transition: width 0.5s ease-out;
    position: relative;
    overflow: hidden;
  }

  .bar-fill::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    animation: shimmer 2s infinite;
  }

  @keyframes shimmer {
    0% {
      transform: translateX(-100%);
    }
    100% {
      transform: translateX(100%);
    }
  }

  .status-bar.running .bar-fill {
    background: linear-gradient(90deg, #52c41a, #73d13d);
  }

  .status-bar.warning .bar-fill {
    background: linear-gradient(90deg, #faad14, #ffc53d);
  }

  .status-bar.stopped .bar-fill {
    background: linear-gradient(90deg, #f5222d, #ff4d4f);
  }

  .bar-value {
    width: 30px;
    text-align: right;
    font-size: 12px;
    font-weight: bold;
    color: #ffffff;
  }

  .production-trend {
    margin-bottom: 20px;
  }

  .production-trend h4 {
    color: #7ec8e3;
    margin: 0 0 12px 0;
    font-size: 14px;
  }

  .trend-chart {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    padding: 8px;
  }

  .chart-container {
    height: 100px;
    margin-bottom: 4px;
  }

  .trend-svg {
    width: 100%;
    height: 100%;
  }

  .trend-labels {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    color: #7ec8e3;
    padding: 0 4px;
  }

  .realtime-production {
    margin-bottom: 20px;
    text-align: center;
  }

  .realtime-production h4 {
    color: #7ec8e3;
    margin: 0 0 12px 0;
    font-size: 14px;
  }

  .quality-metrics {
    margin-bottom: 16px;
  }

  .quality-metrics h4 {
    color: #7ec8e3;
    margin: 0 0 12px 0;
    font-size: 14px;
  }

  .quality-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .quality-item {
    background: rgba(0, 40, 80, 0.5);
    border: 1px solid rgba(0, 212, 255, 0.3);
    border-radius: 8px;
    padding: 8px;
    text-align: center;
  }

  .quality-label {
    font-size: 10px;
    color: #7ec8e3;
    margin-bottom: 4px;
  }

  .quality-value {
    font-size: 16px;
    font-weight: bold;
    color: #52c41a;
  }

  .quality-value.bad {
    color: #f5222d;
  }

  .quality-value.warning {
    color: #faad14;
  }

  /* 自定义滚动条 */
  .production-data-panel::-webkit-scrollbar {
    width: 6px;
  }

  .production-data-panel::-webkit-scrollbar-track {
    background: rgba(0, 20, 40, 0.5);
    border-radius: 3px;
  }

  .production-data-panel::-webkit-scrollbar-thumb {
    background: rgba(0, 212, 255, 0.5);
    border-radius: 3px;
  }

  .production-data-panel::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 212, 255, 0.7);
  }
</style>
