<template>
  <view class="stats-section">
    <view class="section-header">
      <text class="section-title">{{ currentMode === 'production' ? t('dashboard.productionOverview') : t('dashboard.qualityOverview') }}</text>
      <ModeSwitchPopup :currentMode="currentMode" @modeChange="switchMode" />
    </view>

    <scroll-view scroll-x enable-flex class="stats-scroll">
      <view class="stats-grid">
        <view
          v-for="(stat, index) in taskStats"
          :key="stat.key"
          class="stat-card"
          :style="cardStyle(index)"
        >
          <text class="stat-value">{{ formatNumber(stat.value) }}</text>
          <text class="stat-label">{{ stat.label }}</text>
        </view>
      </view>
    </scroll-view>

    <PlanSection />
    <DeviceSection />
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import request from '@/utils/request'
import { formatNumber } from '@/utils/format'
import PlanSection from '@/components/dashboard/PlanSection.vue'
import DeviceSection from '@/components/dashboard/DeviceSection.vue'
import ModeSwitchPopup from '@/components/dashboard/ModeSwitchPopup.vue'

const { t } = useI18n()

const emit = defineEmits(['modeChange'])

const currentMode = ref('production')

const COLOR_PALETTE = [
  { border: '#1a3a5c', value: '#1a3a5c' },
  { border: '#ff8c00', value: '#ff8c00' },
  { border: '#18bc37', value: '#18bc37' },
  { border: '#4a90c2', value: '#4a90c2' },
  { border: '#e74c3c', value: '#e74c3c' },
  { border: '#8e44ad', value: '#8e44ad' },
  { border: '#16a085', value: '#16a085' },
  { border: '#d35400', value: '#d35400' },
  { border: '#2980b9', value: '#2980b9' },
  { border: '#c0392b', value: '#c0392b' }
]

const taskStats = ref([])

function cardStyle(index) {
  const color = COLOR_PALETTE[index % COLOR_PALETTE.length]
  return {
    borderLeft: `6rpx solid ${color.border}`,
    color: color.value
  }
}

async function loadProductionStats() {
  const res = await request({ url: '/admin-api/mes/dashboard/getProduction', method: 'get' })
  const items = res?.data?.taskItems || []
  taskStats.value = items.map((i) => ({
    key: String(i.key),
    label: i.label || '',
    value: Number(i.value ?? 0)
  }))
}

function switchMode(mode) {
  currentMode.value = mode
  emit('modeChange', mode)
}

onMounted(() => {
  loadProductionStats()
})

defineExpose({ loadProductionStats })
</script>

<style lang="scss" scoped>
.stats-section {
  margin-bottom: 24rpx;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1a3a5c;
}

.stats-scroll {
  width: 100%;
  white-space: nowrap;
}

.stats-grid {
  display: inline-flex;
  flex-wrap: nowrap;
  padding: 0 4rpx;
}

.stat-card {
  flex-shrink: 0;
  width: 174rpx;
  background: #ffffff;
  border-radius: 16rpx;
  padding: 24rpx 12rpx;
  margin: 0 6rpx;
  text-align: center;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  white-space: normal;

  &:first-child {
    margin-left: 0;
  }

  &:last-child {
    margin-right: 0;
  }

  &:active {
    transform: scale(0.98);
  }
}

.stat-value {
  display: block;
  font-size: 40rpx;
  font-weight: bold;
  margin-bottom: 6rpx;
}

.stat-label {
  display: block;
  font-size: 24rpx;
  color: #666666;
  word-break: break-all;
}
</style>
