<template>
  <div class="dashboard">
    <div class="welcome-bar">
      <div class="welcome-left">
        <div class="welcome-avatar">
          <span class="welcome-avatar-text">{{ (userStore.nickname || userStore.name || 'U').charAt(0) }}</span>
        </div>
        <div class="welcome-info">
          <h2 class="welcome-greeting">{{ greeting }}，{{ userStore.nickname || userStore.name }}</h2>
          <p class="welcome-date">{{ currentDate }}</p>
        </div>
      </div>
      <el-button class="refresh-btn" icon="Refresh" @click="loadStatistics" :loading="loading">刷新数据</el-button>
    </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.06 + 's' }">
        <div class="stat-icon" :style="{ background: stat.bgColor, color: stat.color }">
          <svg-icon :icon-class="stat.icon" />
        </div>
        <div class="stat-content">
          <span class="stat-label">{{ stat.label }}</span>
          <span class="stat-value" :style="{ color: stat.label === '全部订单' ? '#1f2937' : stat.color }">
            {{ stat.count.toLocaleString() }}
          </span>
        </div>
      </div>
    </div>

    <div class="chart-section">
      <div class="chart-header">
        <h3 class="chart-title">订单分布</h3>
        <span class="chart-subtitle">各状态订单数量占比</span>
      </div>
      <div class="chart-body">
        <div v-for="(stat, idx) in statistics.slice(1)" :key="stat.label" class="chart-row">
          <span class="chart-row-label">{{ stat.label }}</span>
          <div class="chart-row-track">
            <div class="chart-row-fill" :style="{
              width: barWidth(stat.count),
              background: stat.color,
              '--delay': idx * 0.08 + 's'
            }"></div>
          </div>
          <span class="chart-row-count">{{ stat.count }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

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

/** @Author: Antigravity */

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

const statistics = ref([
  { label: '全部订单', count: 0, color: '#4F6EF7', bgColor: '#EEF1FE', icon: 'star' },
  { label: '待确认', count: 0, color: '#FF9900', bgColor: '#FFF7E6', icon: 'time' },
  { label: '已确认', count: 0, color: '#1890FF', bgColor: '#E6F7FF', icon: 'checkbox' },
  { label: '已审核', count: 0, color: '#52C41A', bgColor: '#F6FFED', icon: 'log' },
  { label: '已签批', count: 0, color: '#722ED1', bgColor: '#F9F0FF', icon: 'edit' },
  { label: '挤压完成', count: 0, color: '#13C2C2', bgColor: '#E6FFFB', icon: 'component' },
  { label: '生产完成', count: 0, color: '#2F54EB', bgColor: '#F0F5FF', icon: 'finish' },
  { label: '已取消', count: 0, color: '#F5222D', bgColor: '#FFF1F0', icon: 'money' }
]);

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 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[1] || 0;
    statistics.value[3].count = data[2] || 0;
    statistics.value[4].count = data[3] || 0;
    statistics.value[5].count = data[4] || 0;
    statistics.value[6].count = data[5] || 0;
    statistics.value[7].count = data[6] || 0;
  }).finally(() => {
    loading.value = false;
  });
}

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

<style lang="scss" scoped>
.dashboard {
  padding: 28px 32px;
  min-height: calc(100vh - 100px);
  background: #f7f8fc;
  max-width: 1200px;
  margin: 0 auto;
}

/* ==================== Welcome Bar ==================== */
.welcome-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.welcome-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.welcome-avatar {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 4px 14px rgba(59, 130, 246, 0.3);
}

.welcome-avatar-text {
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  letter-spacing: 1px;
}

.welcome-greeting {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: #1f2937;
  letter-spacing: 0.3px;
}

.welcome-date {
  margin: 4px 0 0;
  font-size: 13px;
  color: #9ca3af;
}

.refresh-btn {
  padding: 8px 20px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 500;
  color: #3b82f6;
  background: rgba(59, 130, 246, 0.08);
  border: 1px solid rgba(59, 130, 246, 0.15);
  transition: all 0.25s ease;
}

.refresh-btn:hover {
  color: #fff;
  background: #3b82f6;
  border-color: #3b82f6;
  box-shadow: 0 2px 10px rgba(59, 130, 246, 0.3);
}

/* ==================== Stats Grid ==================== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 28px;

  .stat-card {
    background: #fff;
    border-radius: 14px;
    padding: 20px 22px;
    display: flex;
    align-items: center;
    gap: 16px;
    border: 1px solid #f0f2f5;
    transition: all 0.3s ease;
    animation: cardFadeIn 0.5s ease both;
    animation-delay: var(--delay);

    &:hover {
      border-color: #e0e5ea;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
      transform: translateY(-2px);
    }

    .stat-icon {
      width: 48px;
      height: 48px;
      border-radius: 12px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 22px;
      flex-shrink: 0;
      transition: transform 0.3s ease;
    }

    &:hover .stat-icon {
      transform: scale(1.08);
    }

    .stat-content {
      display: flex;
      flex-direction: column;
      gap: 4px;
      min-width: 0;
    }

    .stat-label {
      font-size: 13px;
      color: #9ca3af;
      font-weight: 500;
    }

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

/* ==================== Chart ==================== */
.chart-section {
  background: #fff;
  border-radius: 14px;
  padding: 28px 32px;
  border: 1px solid #f0f2f5;

  .chart-header {
    display: flex;
    align-items: baseline;
    gap: 12px;
    margin-bottom: 24px;

    .chart-title {
      margin: 0;
      font-size: 16px;
      font-weight: 600;
      color: #1f2937;
    }

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

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

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

      .chart-row-label {
        font-size: 13px;
        color: #6b7280;
        font-weight: 500;
        white-space: nowrap;
      }

      .chart-row-track {
        height: 10px;
        background: #f3f4f6;
        border-radius: 5px;
        overflow: hidden;

        .chart-row-fill {
          height: 100%;
          border-radius: 5px;
          animation: barGrow 0.8s ease both;
          animation-delay: var(--delay);
          opacity: 0.85;
        }
      }

      .chart-row-count {
        font-size: 14px;
        font-weight: 600;
        color: #4b5563;
        text-align: right;
      }
    }
  }
}

/* ==================== Animations ==================== */
@keyframes cardFadeIn {
  from {
    opacity: 0;
    transform: translateY(16px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

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

@media (max-width: 768px) {
  .dashboard {
    padding: 16px;
  }

  .stats-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .chart-section {
    padding: 20px;

    .chart-body .chart-row {
      grid-template-columns: 70px 1fr 50px;
      gap: 10px;
    }
  }

  .welcome-greeting {
    font-size: 18px;
  }
}
</style>