<template>
  <div class="data-stand-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2 class="page-title">数据看板</h2>
      <el-button type="primary" :icon="Refresh" @click="refreshAll" :loading="isRefreshing">
        刷新全部
      </el-button>
    </div>

    <!-- 两列布局 -->
    <el-row :gutter="20" class="stats-row">
      <!-- 左侧列 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <div class="card-wrapper">
          <TodayOrderCard ref="todayOrderCardRef" />
        </div>
        <div class="card-wrapper">
          <IncomeSummaryCard ref="incomeSummaryCardRef" />
        </div>
      </el-col>

      <!-- 右侧列 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <div class="card-wrapper">
          <CurrencyInfoCard ref="currencyInfoCardRef" />
        </div>
        <div class="card-wrapper">
          <UserStatsCard ref="userStatsCardRef" />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import TodayOrderCard from './components/TodayOrderCard.vue'
import CurrencyInfoCard from './components/CurrencyInfoCard.vue'
import IncomeSummaryCard from './components/IncomeSummaryCard.vue'
import UserStatsCard from './components/UserStatsCard.vue'

// ==================== 组件引用 ====================

const todayOrderCardRef = ref<InstanceType<typeof TodayOrderCard> | null>(null)
const currencyInfoCardRef = ref<InstanceType<typeof CurrencyInfoCard> | null>(null)
const incomeSummaryCardRef = ref<InstanceType<typeof IncomeSummaryCard> | null>(null)
const userStatsCardRef = ref<InstanceType<typeof UserStatsCard> | null>(null)

// ==================== 状态定义 ====================

const isRefreshing = ref(false)

// ==================== 方法定义 ====================

/**
 * 刷新所有卡片数据
 */
async function refreshAll() {
  isRefreshing.value = true
  ElMessage.info('正在刷新所有数据...')

  try {
    // 并行刷新所有卡片
    await Promise.all([
      todayOrderCardRef.value?.refresh(),
      currencyInfoCardRef.value?.refresh(),
      incomeSummaryCardRef.value?.refresh(),
      userStatsCardRef.value?.refresh()
    ])
    ElMessage.success('数据刷新完成')
  } catch (error) {
    console.error('刷新数据失败:', error)
    ElMessage.error('部分数据刷新失败')
  } finally {
    isRefreshing.value = false
  }
}
</script>

<style scoped>
.data-stand-container {
  padding: 20px;
  background: var(--el-bg-color-page);
  min-height: calc(100vh - 84px);
}

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

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  margin: 0;
}

.stats-row {
  margin: 0 !important;
}

.card-wrapper {
  margin-bottom: 20px;
}

/* 响应式布局调整 */
@media (max-width: 991px) {
  .data-stand-container {
    padding: 16px;
  }

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

  .page-title {
    font-size: 18px;
  }
}

@media (max-width: 767px) {
  .data-stand-container {
    padding: 12px;
  }

  .card-wrapper {
    margin-bottom: 16px;
  }
}
</style>
