<template>
  <div class="products-page">
    <div class="page-header">
      <h2>产品管理</h2>
      <p>这里展示了通过SSO认证保护的业务接口数据</p>
    </div>

    <el-card>
      <template #header>
        <div class="card-header">
          <span>产品列表</span>
          <el-button type="primary" @click="loadProducts">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </template>

      <div v-loading="loading">
        <el-table :data="products" style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="name" label="产品名称" />
          <el-table-column prop="price" label="价格">
            <template #default="{ row }">
              ¥{{ row.price.toFixed(2) }}
            </template>
          </el-table-column>
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作" width="200">
            <template #default="{ row }">
              <el-button size="small" @click="viewProduct(row)">
                查看
              </el-button>
              <el-button size="small" type="primary" @click="editProduct(row)">
                编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div v-if="!loading && products.length === 0" class="empty-state">
          <el-empty description="暂无产品数据" />
        </div>
      </div>
    </el-card>

    <!-- 用户信息展示 -->
    <el-card class="user-info-card">
      <template #header>
        <span>当前用户信息</span>
      </template>
      <div class="user-info">
        <p><strong>用户名:</strong> {{ authStore.user?.username }}</p>
        <p><strong>邮箱:</strong> {{ authStore.user?.email }}</p>
        <p><strong>状态:</strong> 
          <el-tag :type="authStore.user?.is_active ? 'success' : 'danger'">
            {{ authStore.user?.is_active ? '活跃' : '非活跃' }}
          </el-tag>
        </p>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import axios from 'axios'

const authStore = useAuthStore()
const loading = ref(false)
const products = ref<any[]>([])

const loadProducts = async () => {
  loading.value = true
  try {
    const response = await axios.get('/api/products')
    if (response.data.code === 0) {
      products.value = response.data.data.products
      ElMessage.success('产品列表加载成功')
    } else {
      ElMessage.error(response.data.message || '加载失败')
    }
  } catch (error: any) {
    console.error('加载产品列表失败:', error)
    ElMessage.error('加载产品列表失败')
  } finally {
    loading.value = false
  }
}

const viewProduct = (product: any) => {
  ElMessage.info(`查看产品: ${product.name}`)
}

const editProduct = (product: any) => {
  ElMessage.info(`编辑产品: ${product.name}`)
}

onMounted(() => {
  loadProducts()
})
</script>

<style scoped>
.products-page {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.page-header p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

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

.empty-state {
  padding: 40px 0;
}

.user-info-card {
  margin-top: 20px;
}

.user-info p {
  margin: 8px 0;
  color: #333;
}
</style>