<template>
  <div class="dashboard">
    <div class="welcome-section">
      <div class="welcome-bg-decor">
        <div class="decor-ring ring-1"></div>
        <div class="decor-ring ring-2"></div>
        <div class="decor-ring ring-3"></div>
        <div class="decor-grid"></div>
        <div class="decor-particle p1"></div>
        <div class="decor-particle p2"></div>
        <div class="decor-particle p3"></div>
      </div>
      <div class="welcome-content">
        <div class="welcome-text">
          <div class="welcome-tag">华晟ERP订单管理系统</div>
          <h2>{{ greeting }}，{{ userStore.nickname || userStore.name }}</h2>
          <p class="welcome-sub">
            <span class="date-icon">📅</span>
            {{ currentDate }}
          </p>
        </div>
        <div class="welcome-actions">
          <el-button plain round icon="Refresh" @click="loadStatistics" :loading="loading">刷新数据</el-button>
        </div>
      </div>
    </div>

    <div class="stats-grid">
      <div
        v-for="(stat, idx) in statistics"
        :key="stat.label"
        class="stat-card"
        :style="{ '--card-color': stat.color, '--card-bg': stat.bgColor, '--delay': idx * 0.08 + 's' }"
        @click="router.push('/order')"
      >
        <div class="stat-card-glow" :style="{ background: stat.color }"></div>
        <div class="stat-card-inner">
          <div class="stat-top">
            <div class="stat-icon-wrap">
              <svg-icon :icon-class="stat.icon" />
            </div>
            <div class="stat-badge" :style="{ background: stat.bgColor, color: stat.color }">
              {{ stat.label }}
            </div>
          </div>
          <div class="stat-body">
            <div class="stat-value">{{ stat.count }}</div>
            <div class="stat-unit">笔</div>
          </div>
          <div class="stat-footer">
            <span class="stat-hint">点击查看详情</span>
            <span class="stat-arrow">→</span>
          </div>
        </div>
      </div>
    </div>

    <div class="chart-section">
      <div class="chart-header">
        <div class="chart-header-left">
          <div class="chart-title">订单分布</div>
          <div class="chart-subtitle">各状态订单数量占比</div>
        </div>
        <div class="chart-legend">
          <div v-for="stat in statistics" :key="stat.label" class="legend-item">
            <span class="legend-dot" :style="{ background: stat.color }"></span>
            <span class="legend-label">{{ stat.label }}</span>
          </div>
        </div>
      </div>
      <div class="chart-body">
        <div v-for="(stat, idx) in statistics" :key="stat.label" class="chart-row">
          <div class="chart-row-label">
            <span class="chart-row-dot" :style="{ background: stat.color }"></span>
            {{ stat.label }}
          </div>
          <div class="chart-row-bar-wrap">
            <div class="chart-row-bar-bg">
              <div
                class="chart-row-bar-fill"
                :style="{
                  width: barWidth(stat.count),
                  background: `linear-gradient(90deg, ${stat.color}, ${stat.color}dd)`,
                  '--delay': idx * 0.1 + 's'
                }"
              ></div>
            </div>
          </div>
          <div class="chart-row-value">{{ stat.count }}<span class="chart-row-unit">笔</span></div>
          <div class="chart-row-pct">{{ barPercent(stat.count) }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="Index" lang="ts">
import { countOrder } from '@/api/erp/order';
import { useUserStore } from '@/store/modules/user';
import { useRouter } from 'vue-router';

/** @Author: Antigravity */

const userStore = useUserStore();
const router = useRouter();
const loading = ref(false);

const statistics = ref([
  { label: '全部订单', count: 0, color: '#4F6EF7', bgColor: '#EEF1FE', icon: 'order' },
  { label: '待审核', count: 0, color: '#F7A83E', bgColor: '#FEF6E6', icon: 'wait' },
  { label: '生产中', count: 0, color: '#5FDBA7', bgColor: '#E8FBF2', icon: 'success' },
  { label: '已完成', count: 0, color: '#409EFF', bgColor: '#ECF5FF', icon: 'finish' }
]);

const currentDate = computed(() => {
  const d = new Date();
  const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
  return `${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日 星期${weekdays[d.getDay()]}`;
});

const greeting = computed(() => {
  const hour = new Date().getHours();
  if (hour < 6) return '夜深了';
  if (hour < 9) return '早上好';
  if (hour < 12) return '上午好';
  if (hour < 14) return '中午好';
  if (hour < 18) return '下午好';
  return '晚上好';
});

function barWidth(count: number) {
  const max = Math.max(...statistics.value.map(s => s.count), 1);
  return (count / max * 100).toFixed(1) + '%';
}

function barPercent(count: number) {
  const total = statistics.value.reduce((s, v) => s + v.count, 0);
  if (!total) return '0%';
  return (count / total * 100).toFixed(1) + '%';
}

function loadStatistics() {
  loading.value = true;
  countOrder().then(res => {
    const data = res.data || {};
    const total = Object.values(data).reduce((sum: number, v: any) => sum + (Number(v) || 0), 0);
    statistics.value[0].count = total;
    statistics.value[1].count = data[0] || 0;
    statistics.value[2].count = data[3] || 0;
    statistics.value[3].count = data[4] || 0;
  }).finally(() => {
    loading.value = false;
  });
}

onMounted(() => {
  loadStatistics();
});
</script>

<style lang="scss" scoped>
.dashboard {
  padding: 24px;
  min-height: calc(100vh - 100px);
  background: #f5f7fa;
}

.welcome-section {
  background: linear-gradient(135deg, #4F6EF7 0%, #7C5CFC 50%, #A855F7 100%);
  border-radius: 16px;
  padding: 36px 44px;
  margin-bottom: 28px;
  position: relative;
  overflow: hidden;

  .welcome-bg-decor {
    position: absolute;
    inset: 0;
    pointer-events: none;

    .decor-ring {
      position: absolute;
      border-radius: 50%;
      border: 1px solid rgba(255, 255, 255, 0.06);

      &.ring-1 {
        top: -30%;
        right: -3%;
        width: 450px;
        height: 450px;
      }

      &.ring-2 {
        bottom: -20%;
        right: 8%;
        width: 300px;
        height: 300px;
        border-color: rgba(255, 255, 255, 0.04);
      }

      &.ring-3 {
        top: 15%;
        left: 50%;
        width: 160px;
        height: 160px;
        border-color: rgba(255, 255, 255, 0.03);
      }
    }

    .decor-grid {
      position: absolute;
      top: 24px;
      left: 32px;
      width: 72px;
      height: 72px;
      background-image:
        linear-gradient(rgba(255,255,255,0.06) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255,255,255,0.06) 1px, transparent 1px);
      background-size: 12px 12px;
    }

    .decor-particle {
      position: absolute;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.08);

      &.p1 {
        top: 25%;
        left: 38%;
        width: 6px;
        height: 6px;
      }

      &.p2 {
        top: 55%;
        left: 42%;
        width: 4px;
        height: 4px;
        background: rgba(255, 255, 255, 0.05);
      }

      &.p3 {
        top: 35%;
        left: 60%;
        width: 5px;
        height: 5px;
      }
    }
  }

  .welcome-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    z-index: 1;
  }

  .welcome-text {
    .welcome-tag {
      display: inline-block;
      font-size: 12px;
      color: rgba(255, 255, 255, 0.7);
      background: rgba(255, 255, 255, 0.1);
      padding: 3px 12px;
      border-radius: 20px;
      margin-bottom: 12px;
      letter-spacing: 1px;
      backdrop-filter: blur(4px);
      border: 1px solid rgba(255, 255, 255, 0.08);
    }

    h2 {
      margin: 0 0 10px 0;
      font-size: 26px;
      font-weight: 700;
      color: #fff;
      letter-spacing: 0.5px;
    }

    .welcome-sub {
      margin: 0;
      font-size: 14px;
      color: rgba(255, 255, 255, 0.7);
      display: flex;
      align-items: center;
      gap: 6px;

      .date-icon {
        font-size: 14px;
      }
    }
  }

  .welcome-actions {
    .el-button {
      background: rgba(255, 255, 255, 0.12);
      border-color: rgba(255, 255, 255, 0.2);
      color: #fff;
      font-size: 13px;
      padding: 9px 22px;
      backdrop-filter: blur(6px);
      border-radius: 20px;

      &:hover {
        background: rgba(255, 255, 255, 0.22);
        border-color: rgba(255, 255, 255, 0.3);
      }
    }
  }
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 28px;

  .stat-card {
    background: #fff;
    border-radius: 16px;
    cursor: pointer;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid #edf0f5;
    position: relative;
    overflow: hidden;
    animation: cardFadeIn 0.6s ease both;
    animation-delay: var(--delay);

    &:hover {
      transform: translateY(-8px);
      box-shadow: 0 20px 48px -8px rgba(0, 0, 0, 0.1);
      border-color: var(--card-color);

      .stat-card-glow {
        opacity: 1;
        transform: scale(1);
      }

      .stat-arrow {
        transform: translateX(4px);
        opacity: 1;
      }
    }

    .stat-card-glow {
      position: absolute;
      top: -80px;
      right: -80px;
      width: 200px;
      height: 200px;
      border-radius: 50%;
      opacity: 0;
      transform: scale(0.3);
      transition: all 0.5s ease;
      filter: blur(50px);
    }

    .stat-card-inner {
      position: relative;
      z-index: 1;
      padding: 24px 24px 18px;
    }

    .stat-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
    }

    .stat-icon-wrap {
      width: 46px;
      height: 46px;
      border-radius: 14px;
      background: var(--card-bg);
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
      color: var(--card-color);
      transition: all 0.4s ease;
    }

    &:hover .stat-icon-wrap {
      transform: scale(1.08) rotate(-6deg);
      box-shadow: 0 8px 24px rgba(79, 110, 247, 0.15);
    }

    .stat-badge {
      font-size: 12px;
      font-weight: 500;
      padding: 4px 14px;
      border-radius: 20px;
      letter-spacing: 0.5px;
    }

    .stat-body {
      display: flex;
      align-items: baseline;
      gap: 4px;
      margin-bottom: 16px;

      .stat-value {
        font-size: 32px;
        font-weight: 800;
        color: #1a1a2e;
        line-height: 1;
        font-variant-numeric: tabular-nums;
        letter-spacing: -0.5px;
      }

      .stat-unit {
        font-size: 14px;
        color: #bfc0c4;
        font-weight: 400;
      }
    }

    .stat-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-top: 14px;
      border-top: 1px solid #f0f2f5;

      .stat-hint {
        font-size: 12px;
        color: #c0c4cc;
      }

      .stat-arrow {
        font-size: 14px;
        color: var(--card-color);
        transition: all 0.3s ease;
        opacity: 0.5;
      }
    }
  }
}

.chart-section {
  background: #fff;
  border-radius: 16px;
  padding: 28px 32px;
  border: 1px solid #edf0f5;

  .chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 28px;

    .chart-header-left {
      .chart-title {
        font-size: 16px;
        font-weight: 600;
        color: #1a1a2e;
        margin-bottom: 4px;
      }

      .chart-subtitle {
        font-size: 13px;
        color: #bfc0c4;
      }
    }

    .chart-legend {
      display: flex;
      gap: 20px;

      .legend-item {
        display: flex;
        align-items: center;
        gap: 6px;

        .legend-dot {
          width: 8px;
          height: 8px;
          border-radius: 50%;
        }

        .legend-label {
          font-size: 12px;
          color: #606266;
        }
      }
    }
  }

  .chart-body {
    display: flex;
    flex-direction: column;
    gap: 18px;

    .chart-row {
      display: grid;
      grid-template-columns: 100px 1fr 70px 60px;
      align-items: center;
      gap: 16px;

      .chart-row-label {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 13px;
        color: #606266;
        font-weight: 500;

        .chart-row-dot {
          width: 6px;
          height: 6px;
          border-radius: 50%;
          flex-shrink: 0;
        }
      }

      .chart-row-bar-wrap {
        .chart-row-bar-bg {
          height: 8px;
          background: #f0f2f5;
          border-radius: 4px;
          overflow: hidden;

          .chart-row-bar-fill {
            height: 100%;
            border-radius: 4px;
            animation: barGrow 1s ease both;
            animation-delay: var(--delay);
          }
        }
      }

      .chart-row-value {
        font-size: 14px;
        font-weight: 600;
        color: #1a1a2e;
        text-align: right;

        .chart-row-unit {
          font-size: 11px;
          color: #bfc0c4;
          font-weight: 400;
          margin-left: 2px;
        }
      }

      .chart-row-pct {
        font-size: 12px;
        color: #909399;
        text-align: right;
      }
    }
  }
}

@keyframes cardFadeIn {
  from {
    opacity: 0;
    transform: translateY(24px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes barGrow {
  from {
    width: 0 !important;
  }
}

@media (max-width: 1200px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .welcome-section {
    padding: 24px;

    .welcome-content {
      flex-direction: column;
      align-items: flex-start;
      gap: 16px;
    }
  }

  .chart-section {
    padding: 20px;

    .chart-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 12px;

      .chart-legend {
        flex-wrap: wrap;
        gap: 12px;
      }
    }

    .chart-body .chart-row {
      grid-template-columns: 80px 1fr 50px 50px;
      gap: 10px;
    }
  }
}
</style>