<script setup>
import { ref, onMounted } from 'vue'
import { reportApi } from '@/api/report.js'

// ─── State ────────────────────────────────────────────────────────────────────

const loading = ref(false)

const cards = ref([
  { key: 'customer_count', title: '客户数', value: '--', unit: '位', color: '#E8A87C', bg: '#FBE8D5' },
  { key: 'contract_amount', title: '合同金额', value: '--', unit: '元', color: '#67C23A', bg: '#E8F5E1' },
  { key: 'total_rooms', title: '总房间数', value: '--', unit: '间', color: '#409EFF', bg: '#E0EFFF' },
  { key: 'occupied_rooms', title: '入住房间数', value: '--', unit: '间', color: '#E6A23C', bg: '#FDF0D6' },
  { key: 'today_revenue', title: '今日营收', value: '--', unit: '元', color: '#F56C6C', bg: '#FDE2E2' },
  { key: 'month_revenue', title: '月度营收', value: '--', unit: '元', color: '#C97B5E', bg: '#F9E8E0' }
])

// ─── Methods ──────────────────────────────────────────────────────────────────

async function fetchOverview() {
  loading.value = true
  try {
    const res = await reportApi.overview()
    const data = res.data || {}
    cards.value.forEach(card => {
      if (data[card.key] !== undefined && data[card.key] !== null) {
        card.value = data[card.key]
      }
    })
  } finally { loading.value = false }
}

onMounted(fetchOverview)
</script>

<template>
  <div class="p-6 space-y-4" v-loading="loading">
    <div class="flex items-center gap-2 mb-2">
      <span class="w-1 h-5 rounded bg-primary inline-block" style="background:var(--color-primary)"></span>
      <span class="font-semibold text-base text-gray-800">经营概览</span>
    </div>

    <el-row :gutter="16">
      <el-col
        v-for="card in cards"
        :key="card.key"
        :xs="24"
        :sm="12"
        :md="8"
        :lg="8"
        :xl="4"
      >
        <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5 mb-4 flex items-center gap-4">
          <div class="w-14 h-14 rounded-xl flex items-center justify-center flex-shrink-0" :style="{ background: card.bg }">
            <span class="text-2xl font-bold" :style="{ color: card.color }">{{ card.title.charAt(0) }}</span>
          </div>
          <div class="flex-1 min-w-0">
            <div class="text-sm text-gray-500 mb-1">{{ card.title }}</div>
            <div class="flex items-baseline gap-1">
              <span class="text-3xl font-bold leading-none" :style="{ color: card.color }">{{ card.value }}</span>
              <span class="text-sm text-gray-400">{{ card.unit }}</span>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
.section-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text-primary);
  padding-left: 8px;
  border-left: 3px solid var(--color-primary);
}

.mt-16 {
  margin-top: 16px;
}

.overview-card {
  margin-bottom: 16px;
}

.overview-card :deep(.el-card__body) {
  padding: 20px;
}

.card-inner {
  display: flex;
  align-items: center;
  gap: 16px;
}

.card-icon-wrap {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.card-icon-text {
  font-size: 22px;
  font-weight: 700;
}

.card-info {
  flex: 1;
  min-width: 0;
}

.card-label {
  font-size: 13px;
  color: var(--color-text-secondary);
  margin-bottom: 4px;
}

.card-value {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.card-number {
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
}

.card-unit {
  font-size: 13px;
  color: var(--color-text-secondary);
}
</style>
