<template>
  <div class="home">
    <!-- 欢迎横幅 -->
    <el-row class="welcome-banner">
      <el-col :span="24">
        <div class="banner-content">
          <h1>欢迎使用 AIGC Demo</h1>
          <p>现代化的订单管理和支付系统</p>
          <div class="banner-actions">
            <el-button v-if="!userStore.isAuthenticated" type="primary" size="large" @click="$router.push('/login')">
              立即登录
            </el-button>
            <el-button v-if="!userStore.isAuthenticated" type="success" size="large" @click="$router.push('/register')">
              免费注册
            </el-button>
            <el-button v-if="userStore.isAuthenticated" type="primary" size="large" @click="$router.push('/orders/create')">
              创建订单
            </el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 功能特色 -->
    <el-row :gutter="20" class="features">
      <el-col :xs="24" :sm="12" :md="8">
        <el-card class="feature-card" @click="goToOrders" style="cursor: pointer;">
          <div class="feature-icon">
            <el-icon size="48" color="#409EFF"><ShoppingCart /></el-icon>
          </div>
          <h3>订单管理</h3>
          <p>完整的订单生命周期管理，从创建到完成的全流程跟踪</p>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :sm="12" :md="8">
        <el-card class="feature-card" @click="goToPayments" style="cursor: pointer;">
          <div class="feature-icon">
            <el-icon size="48" color="#67C23A"><CreditCard /></el-icon>
          </div>
          <h3>支付</h3>
          <p>支持支付宝、PayPal等多种支付方式，安全便捷</p>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :sm="12" :md="8">
        <el-card class="feature-card" @click="goToAdmin" style="cursor: pointer;">
          <div class="feature-icon">
            <el-icon size="48" color="#E6A23C"><Management /></el-icon>
          </div>
          <h3>管理后台</h3>
          <p>强大的管理功能，支持用户管理、订单统计等</p>
        </el-card>
      </el-col>
    </el-row>

    <!-- 统计数据 -->
    <el-row v-if="userStore.isAuthenticated" :gutter="20" class="stats">
      <el-col :xs="12" :sm="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-number">{{ stats.totalOrders || 0 }}</div>
            <div class="stat-label">总订单数</div>
          </div>
          <el-icon class="stat-icon" color="#409EFF"><List /></el-icon>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-number">{{ stats.pendingOrders || 0 }}</div>
            <div class="stat-label">待支付</div>
          </div>
          <el-icon class="stat-icon" color="#E6A23C"><Clock /></el-icon>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-number">{{ stats.completedOrders || 0 }}</div>
            <div class="stat-label">已完成</div>
          </div>
          <el-icon class="stat-icon" color="#67C23A"><Check /></el-icon>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-number">{{ stats.totalAmount || 0 }}</div>
            <div class="stat-label">总金额</div>
          </div>
          <el-icon class="stat-icon" color="#F56C6C"><Money /></el-icon>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最近订单 -->
    <el-row v-if="userStore.isAuthenticated" class="recent-orders">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近订单</span>
              <el-button type="primary" @click="$router.push('/orders')">查看全部</el-button>
            </div>
          </template>
          
          <el-table :data="recentOrders" v-loading="loading" empty-text="暂无订单">
            <el-table-column prop="orderNumber" label="订单号" width="150">
              <template #default="{ row }">
                <router-link :to="`/orders/${row.id}`" class="order-link">
                  {{ row.orderNumber }}
                </router-link>
              </template>
            </el-table-column>
            <el-table-column prop="totalAmount" label="金额" width="100">
              <template #default="{ row }">
                {{ row.currency }} {{ row.totalAmount }}
              </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getStatusType(row.status)">
                  {{ getStatusText(row.status) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="createdAt" label="创建时间" width="150">
              <template #default="{ row }">
                {{ formatDate(row.createdAt) }}
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
              <template #default="{ row }">
                <el-button size="small" @click="$router.push(`/orders/${row.id}`)">
                  查看详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useOrderStore } from '@/stores/orders'
import { getOrderStats } from '@/api/orders'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const orderStore = useOrderStore()

const stats = ref({})
const recentOrders = ref([])
const loading = ref(false)

// 功能卡片点击事件
const goToOrders = () => {
  if (userStore.isAuthenticated) {
    router.push('/orders')
  } else {
    ElMessage.warning('请先登录')
    router.push('/login')
  }
}

const goToPayments = () => {
  router.push('/payments')
}

const goToAdmin = () => {
  if (userStore.isAuthenticated) {
    if (userStore.isAdmin) {
      router.push('/admin/orders')
    } else {
      ElMessage.warning('需要管理员权限')
    }
  } else {
    ElMessage.warning('请先登录')
    router.push('/login')
  }
}

// 获取统计数据
const fetchStats = async () => {
  try {
    const response = await getOrderStats()
    if (response.success) {
      stats.value = response.data
    }
  } catch (error) {
    console.error('Fetch stats error:', error)
  }
}

// 获取最近订单
const fetchRecentOrders = async () => {
  try {
    loading.value = true
    const response = await orderStore.fetchOrders({ page: 0, size: 5 })
    if (response.success) {
      recentOrders.value = orderStore.orders
    }
  } catch (error) {
    console.error('Fetch recent orders error:', error)
  } finally {
    loading.value = false
  }
}

// 获取状态类型
const getStatusType = (status) => {
  const statusMap = {
    'PENDING': 'warning',
    'CONFIRMED': 'info',
    'PAID': 'primary',
    'PROCESSING': '',
    'SHIPPED': 'success',
    'DELIVERED': 'success',
    'COMPLETED': 'success',
    'CANCELLED': 'danger',
    'REFUNDED': 'info'
  }
  return statusMap[status] || ''
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    'PENDING': '待支付',
    'CONFIRMED': '已确认',
    'PAID': '已支付',
    'PROCESSING': '处理中',
    'SHIPPED': '已发货',
    'DELIVERED': '已送达',
    'COMPLETED': '已完成',
    'CANCELLED': '已取消',
    'REFUNDED': '已退款'
  }
  return statusMap[status] || status
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

onMounted(() => {
  if (userStore.isAuthenticated) {
    fetchStats()
    fetchRecentOrders()
  }
})
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow-x: hidden;
  padding: 20px;
}

/* 页面特殊效果 */
.home::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, transparent 30%, rgba(255,255,255,0.1) 50%, transparent 70%);
  animation: shimmer 3s infinite;
  pointer-events: none;
  z-index: 1;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

/* 内容层级 */
.home > * {
  position: relative;
  z-index: 2;
}

.welcome-banner {
  margin-bottom: 40px;
}

.banner-content {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 40px;
  border-radius: 12px;
  text-align: center;
}

.banner-content h1 {
  font-size: 2.5rem;
  margin-bottom: 16px;
  font-weight: bold;
}

.banner-content p {
  font-size: 1.2rem;
  margin-bottom: 32px;
  opacity: 0.9;
}

.banner-actions .el-button {
  margin: 0 8px;
}

.features {
  margin-bottom: 40px;
}

.feature-card {
  text-align: center;
  height: 200px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  transition: all 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.feature-icon {
  margin-bottom: 16px;
}

.feature-card h3 {
  margin-bottom: 12px;
  color: #303133;
}

.feature-card p {
  color: #606266;
  line-height: 1.6;
}

.stats {
  margin-bottom: 40px;
}

.stat-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 2rem;
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
}

.stat-label {
  color: #909399;
  font-size: 14px;
}

.stat-icon {
  font-size: 2rem;
  opacity: 0.8;
}

.recent-orders {
  margin-bottom: 40px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.order-link {
  color: #409EFF;
  text-decoration: none;
}

.order-link:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .banner-content {
    padding: 40px 20px;
  }
  
  .banner-content h1 {
    font-size: 2rem;
  }
  
  .banner-content p {
    font-size: 1rem;
  }
  
  .feature-card {
    height: auto;
    margin-bottom: 20px;
  }
}
</style>
