<template>
  <div class="min-h-screen bg-gray-50 dark:bg-gray-900">
    <!-- 顶部导航栏 -->
    <nav class="bg-white dark:bg-gray-800 shadow-sm">
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex justify-between h-16">
          <div class="flex items-center">
            <h1 class="text-xl font-bold text-gray-800 dark:text-white">商品管理</h1>
          </div>
          <div class="flex items-center space-x-4">
            <button 
              @click="handleAddProduct"
              class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 flex items-center space-x-2"
            >
              <PlusIcon class="h-5 w-5" />
              <span>添加商品</span>
            </button>
            <button 
              @click="handleExportReport"
              class="px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 flex items-center space-x-2"
            >
              <DownloadIcon class="h-5 w-5" />
              <span>导出报表</span>
            </button>
            <button class="p-2 rounded-full text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700">
              <SettingsIcon class="h-6 w-6" />
            </button>
          </div>
        </div>
      </div>
    </nav>

    <!-- 主要内容区域 -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
      <!-- 搜索和筛选区域 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6 mb-8">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
          <div class="relative">
            <input
              v-model="searchText"
              @input="handleSearch"
              type="text"
              placeholder="搜索商品名称/编号..."
              class="w-full pl-10 pr-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
            />
            <SearchIcon class="absolute left-3 top-2.5 h-5 w-5 text-gray-400 dark:text-gray-500" />
          </div>
          <select 
            v-model="productCategory"
            @change="handleCategoryChange"
            class="border border-gray-300 dark:border-gray-600 rounded-md px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
          >
            <option value="">所有分类</option>
            <option value="phone">手机</option>
            <option value="laptop">笔记本</option>
            <option value="tablet">平板</option>
            <option value="watch">智能手表</option>
            <option value="accessories">配件</option>
          </select>
          <select 
            v-model="productStatus"
            @change="handleStatusChange"
            class="border border-gray-300 dark:border-gray-600 rounded-md px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
          >
            <option value="">所有状态</option>
            <option value="active">在售</option>
            <option value="inactive">下架</option>
            <option value="outOfStock">缺货</option>
          </select>
          <select 
            v-model="priceRange"
            @change="handlePriceRangeChange"
            class="border border-gray-300 dark:border-gray-600 rounded-md px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
          >
            <option value="">所有价格</option>
            <option value="0-1000">1000元以下</option>
            <option value="1000-5000">1000-5000元</option>
            <option value="5000-10000">5000-10000元</option>
            <option value="10000+">10000元以上</option>
          </select>
        </div>
      </div>

      <!-- 商品统计卡片 -->
      <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <div class="flex items-center">
            <div class="p-3 rounded-full bg-blue-100 dark:bg-blue-900">
              <PackageIcon class="h-6 w-6 text-blue-600 dark:text-blue-400" />
            </div>
            <div class="ml-4">
              <p class="text-sm font-medium text-gray-600 dark:text-gray-400">总商品数</p>
              <p class="text-2xl font-semibold text-gray-900 dark:text-white">1,234</p>
            </div>
          </div>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <div class="flex items-center">
            <div class="p-3 rounded-full bg-green-100 dark:bg-green-900">
              <ShoppingBagIcon class="h-6 w-6 text-green-600 dark:text-green-400" />
            </div>
            <div class="ml-4">
              <p class="text-sm font-medium text-gray-600 dark:text-gray-400">在售商品</p>
              <p class="text-2xl font-semibold text-gray-900 dark:text-white">1,024</p>
            </div>
          </div>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <div class="flex items-center">
            <div class="p-3 rounded-full bg-yellow-100 dark:bg-yellow-900">
              <AlertTriangleIcon class="h-6 w-6 text-yellow-600 dark:text-yellow-400" />
            </div>
            <div class="ml-4">
              <p class="text-sm font-medium text-gray-600 dark:text-gray-400">库存预警</p>
              <p class="text-2xl font-semibold text-gray-900 dark:text-white">56</p>
            </div>
          </div>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <div class="flex items-center">
            <div class="p-3 rounded-full bg-purple-100 dark:bg-purple-900">
              <TrendingUpIcon class="h-6 w-6 text-purple-600 dark:text-purple-400" />
            </div>
            <div class="ml-4">
              <p class="text-sm font-medium text-gray-600 dark:text-gray-400">热销商品</p>
              <p class="text-2xl font-semibold text-gray-900 dark:text-white">78</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 商品分析图表 -->
      <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-4">销量趋势</h3>
          <div class="h-64">
            <!-- 这里可以集成图表库，如 ECharts 或 Chart.js -->
            <div ref="salesChartRef" class="flex items-center justify-center h-full text-gray-500 dark:text-gray-400">
              销量趋势图表
            </div>
          </div>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-4">分类分布</h3>
          <div class="h-64">
            <!-- 这里可以集成图表库，如 ECharts 或 Chart.js -->
            <div ref="categoryChartRef" class="flex items-center justify-center h-full text-gray-500 dark:text-gray-400">
              分类分布图表
            </div>
          </div>
        </div>
      </div>

      <!-- 商品列表 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow overflow-hidden">
        <div class="overflow-x-auto">
          <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
            <thead class="bg-gray-50 dark:bg-gray-700">
              <tr>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">商品信息</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">分类</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">价格</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">库存</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">状态</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">操作</th>
              </tr>
            </thead>
            <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
              <tr v-for="product in paginatedProducts" :key="product.id" class="hover:bg-gray-50 dark:hover:bg-gray-700">
                <td class="px-6 py-4">
                  <div class="flex items-center">
                    <img :src="product.image" :alt="product.name" class="h-16 w-16 rounded object-cover" />
                    <div class="ml-4">
                      <div class="text-sm font-medium text-gray-900 dark:text-white">{{ product.name }}</div>
                      <div class="text-sm text-gray-500 dark:text-gray-400">{{ product.description }}</div>
                      <div class="text-sm text-gray-500 dark:text-gray-400">商品编号: {{ product.sku }}</div>
                    </div>
                  </div>
                </td>
                <td class="px-6 py-4">
                  <div class="text-sm text-gray-900 dark:text-white">{{ getCategoryText(product.category) }}</div>
                </td>
                <td class="px-6 py-4">
                  <div class="text-sm text-gray-900 dark:text-white">¥{{ product.price }}</div>
                  <div v-if="product.originalPrice" class="text-sm text-gray-500 dark:text-gray-400 line-through">
                    ¥{{ product.originalPrice }}
                  </div>
                </td>
                <td class="px-6 py-4">
                  <div class="text-sm text-gray-900 dark:text-white">{{ product.stock }}</div>
                  <div v-if="product.stock <= product.stockWarning" class="text-sm text-red-600 dark:text-red-400">
                    库存不足
                  </div>
                </td>
                <td class="px-6 py-4">
                  <span 
                    :class="[
                      'px-2 inline-flex text-xs leading-5 font-semibold rounded-full',
                      getStatusClass(product.status)
                    ]"
                  >
                    {{ getStatusText(product.status) }}
                  </span>
                </td>
                <td class="px-6 py-4 text-sm font-medium">
                  <button 
                    @click="handleViewProduct(product)"
                    class="action-button view-button"
                  >
                    <EyeIcon class="h-4 w-4 inline-block mr-1" />
                    查看
                  </button>
                  <button 
                    @click="handleEditProduct(product)"
                    class="action-button edit-button"
                  >
                    <EditIcon class="h-4 w-4 inline-block mr-1" />
                    编辑
                  </button>
                  <button 
                    v-if="product.status === 'active'"
                    @click="handleDeactivateProduct(product)"
                    class="action-button deactivate-button"
                  >
                    <XIcon class="h-4 w-4 inline-block mr-1" />
                    下架
                  </button>
                  <button 
                    v-if="product.status === 'inactive'"
                    @click="handleActivateProduct(product)"
                    class="action-button activate-button"
                  >
                    <CheckIcon class="h-4 w-4 inline-block mr-1" />
                    上架
                  </button>
                  <button 
                    @click="handleDeleteProduct(product)"
                    class="action-button delete-button"
                  >
                    <Trash2Icon class="h-4 w-4 inline-block mr-1" />
                    删除
                  </button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 分页 -->
      <div class="bg-white dark:bg-gray-800 px-4 py-3 flex items-center justify-between border-t border-gray-200 dark:border-gray-700 sm:px-6 mt-4">
        <div class="flex-1 flex justify-between sm:hidden">
          <button 
            @click="handlePageChange(currentPage - 1)"
            :disabled="currentPage === 1"
            class="pagination-button"
          >
            上一页
          </button>
          <button 
            @click="handlePageChange(currentPage + 1)"
            :disabled="currentPage === totalPages"
            class="pagination-button"
          >
            下一页
          </button>
        </div>
        <div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
          <div>
            <p class="text-sm text-gray-700 dark:text-gray-300">
              显示 <span class="font-medium">{{ (currentPage - 1) * pageSize + 1 }}</span> 到 
              <span class="font-medium">{{ Math.min(currentPage * pageSize, filteredProducts.length) }}</span> 条，共 
              <span class="font-medium">{{ filteredProducts.length }}</span> 条
            </p>
          </div>
          <div>
            <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
              <button 
                @click="handlePageChange(currentPage - 1)"
                :disabled="currentPage === 1"
                class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-600"
              >
                <ChevronLeftIcon class="h-5 w-5" />
              </button>
              <button 
                v-for="page in totalPages"
                :key="page"
                @click="handlePageChange(page)"
                :class="[
                  'relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 text-sm font-medium',
                  currentPage === page ? 'bg-blue-600 text-white border-blue-600' : 'bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600'
                ]"
              >
                {{ page }}
              </button>
              <button 
                @click="handlePageChange(currentPage + 1)"
                :disabled="currentPage === totalPages"
                class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-600"
              >
                <ChevronRightIcon class="h-5 w-5" />
              </button>
            </nav>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'
import { CanvasRenderer } from 'echarts/renderers'
import { LineChart, PieChart, BarChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
} from 'echarts/components'
import { 
  SearchIcon, 
  SettingsIcon, 
  ChevronLeftIcon, 
  ChevronRightIcon,
  PackageIcon,
  ShoppingBagIcon,
  AlertTriangleIcon,
  TrendingUpIcon,
  DollarSignIcon,
  PlusIcon,
  DownloadIcon,
  EyeIcon,
  EditIcon,
  XIcon,
  CheckIcon,
  Trash2Icon
} from 'lucide-vue-next'

// 注册必要的组件
echarts.use([
  CanvasRenderer,
  LineChart,
  PieChart,
  BarChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
])

// 状态管理
const isLoading = ref(false)
const searchText = ref('')
const productCategory = ref('')
const productStatus = ref('')
const priceRange = ref('')
const currentPage = ref(1)
const pageSize = ref(10)

// 默认图片
const defaultImages = {
  product: 'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=200&h=200&fit=crop&auto=format'
}

// 模拟数据
const mockProducts = ref([
  {
    id: 'P1001',
    name: 'iPhone 15 Pro',
    category: '手机',
    price: 8999,
    stock: 100,
    sales: 50,
    status: 'on_sale',
    image: defaultImages.product,
    description: '最新款苹果手机，搭载A17芯片',
    createTime: '2024-03-20 10:30:00',
    updateTime: '2024-03-20 10:30:00'
  },
  {
    id: 'P1002',
    name: 'MacBook Pro',
    category: '电脑',
    price: 12999,
    stock: 50,
    sales: 20,
    status: 'on_sale',
    image: defaultImages.product,
    description: '专业级笔记本电脑，M2芯片',
    createTime: '2024-03-19 14:20:00',
    updateTime: '2024-03-19 15:30:00'
  },
  {
    id: 'P1003',
    name: 'AirPods Pro',
    category: '耳机',
    price: 1999,
    stock: 200,
    sales: 100,
    status: 'on_sale',
    image: defaultImages.product,
    description: '主动降噪无线耳机',
    createTime: '2024-03-18 09:15:00',
    updateTime: '2024-03-18 11:30:00'
  },
  {
    id: 'P1004',
    name: 'iPad Pro',
    category: '平板',
    price: 6999,
    stock: 80,
    sales: 30,
    status: 'on_sale',
    image: defaultImages.product,
    description: '专业级平板电脑，M2芯片',
    createTime: '2024-03-17 16:45:00',
    updateTime: '2024-03-17 17:00:00'
  },
  {
    id: 'P1005',
    name: 'Apple Watch',
    category: '手表',
    price: 2999,
    stock: 150,
    sales: 70,
    status: 'on_sale',
    image: defaultImages.product,
    description: '智能手表，支持健康监测',
    createTime: '2024-03-16 11:25:00',
    updateTime: '2024-03-16 11:40:00'
  }
])

// 商品统计数据
const productStats = ref({
  totalProducts: 1234,
  activeProducts: 1024,
  stockWarning: 56,
  hotProducts: 78
})

// 销量趋势数据
const salesTrends = ref([
  { date: '2024-03-01', count: 120, amount: 120000 },
  { date: '2024-03-02', count: 150, amount: 150000 },
  { date: '2024-03-03', count: 180, amount: 180000 },
  { date: '2024-03-04', count: 200, amount: 200000 },
  { date: '2024-03-05', count: 220, amount: 220000 },
  { date: '2024-03-06', count: 250, amount: 250000 },
  { date: '2024-03-07', count: 280, amount: 280000 }
])

// 分类分布数据
const categoryDistribution = ref([
  { category: 'phone', count: 200, percentage: 16 },
  { category: 'laptop', count: 150, percentage: 12 },
  { category: 'tablet', count: 100, percentage: 8 },
  { category: 'watch', count: 180, percentage: 15 },
  { category: 'accessories', count: 604, percentage: 49 }
])

// 计算属性
const filteredProducts = computed(() => {
  let result = [...mockProducts.value]
  
  // 搜索过滤
  if (searchText.value) {
    result = result.filter(product => 
      product.name.toLowerCase().includes(searchText.value.toLowerCase()) ||
      product.sku.toLowerCase().includes(searchText.value.toLowerCase())
    )
  }
  
  // 分类过滤
  if (productCategory.value) {
    result = result.filter(product => product.category === productCategory.value)
  }
  
  // 状态过滤
  if (productStatus.value) {
    result = result.filter(product => product.status === productStatus.value)
  }
  
  // 价格范围过滤
  if (priceRange.value) {
    const [min, max] = priceRange.value.split('-').map(v => v === '+' ? Infinity : Number(v))
    result = result.filter(product => {
      const price = product.price
      return price >= min && (max === Infinity || price <= max)
    })
  }
  
  return result
})

const paginatedProducts = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return filteredProducts.value.slice(start, end)
})

const totalPages = computed(() => {
  return Math.ceil(filteredProducts.value.length / pageSize.value)
})

// 方法
const handleSearch = () => {
  currentPage.value = 1
}

const handleCategoryChange = () => {
  currentPage.value = 1
}

const handleStatusChange = () => {
  currentPage.value = 1
}

const handlePriceRangeChange = () => {
  currentPage.value = 1
}

const handlePageChange = (page) => {
  currentPage.value = page
}

const getCategoryText = (category) => {
  const texts = {
    phone: '手机',
    laptop: '笔记本',
    tablet: '平板',
    watch: '智能手表',
    accessories: '配件'
  }
  return texts[category] || category
}

const getStatusClass = (status) => {
  const classes = {
    active: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300',
    inactive: 'bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-300',
    outOfStock: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300'
  }
  return classes[status] || 'bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-300'
}

const getStatusText = (status) => {
  const texts = {
    active: '在售',
    inactive: '下架',
    outOfStock: '缺货'
  }
  return texts[status] || status
}

const handleAddProduct = () => {
  console.log('添加商品')
}

const handleViewProduct = (product) => {
  console.log('查看商品:', product)
}

const handleEditProduct = (product) => {
  console.log('编辑商品:', product)
}

const handleActivateProduct = (product) => {
  console.log('上架商品:', product)
}

const handleDeactivateProduct = (product) => {
  console.log('下架商品:', product)
}

const handleDeleteProduct = (product) => {
  console.log('删除商品:', product)
}

const handleExportReport = () => {
  console.log('导出报表')
}

// 图表相关
const salesChartRef = ref(null)
const categoryChartRef = ref(null)
const inventoryChartRef = ref(null)
let salesChart = null
let categoryChart = null
let inventoryChart = null

// 初始化销售趋势图表
const initSalesChart = () => {
  if (!salesChartRef.value) return
  
  if (salesChart) {
    salesChart.dispose()
  }
  
  salesChart = echarts.init(salesChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      formatter: '{b}<br/>{a0}: {c0}件<br/>{a1}: ¥{c1}'
    },
    legend: {
      data: ['销量', '销售额']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: salesTrends.value.map(item => item.date),
      boundaryGap: false
    },
    yAxis: [
      {
        type: 'value',
        name: '销量',
        position: 'left'
      },
      {
        type: 'value',
        name: '销售额',
        position: 'right'
      }
    ],
    series: [
      {
        name: '销量',
        type: 'line',
        data: salesTrends.value.map(item => item.quantity),
        smooth: true,
        areaStyle: {
          opacity: 0.3
        },
        lineStyle: {
          width: 3
        },
        itemStyle: {
          color: '#4F46E5'
        }
      },
      {
        name: '销售额',
        type: 'line',
        yAxisIndex: 1,
        data: salesTrends.value.map(item => item.amount),
        smooth: true,
        areaStyle: {
          opacity: 0.3
        },
        lineStyle: {
          width: 3
        },
        itemStyle: {
          color: '#10B981'
        }
      }
    ]
  }
  salesChart.setOption(option)
}

// 初始化分类分布图表
const initCategoryChart = () => {
  if (!categoryChartRef.value) return
  
  if (categoryChart) {
    categoryChart.dispose()
  }
  
  categoryChart = echarts.init(categoryChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c}件 ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '商品分类',
        type: 'pie',
        radius: '50%',
        data: categoryDistribution.value.map(item => ({
          value: item.count,
          name: item.category
        })),
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        },
        itemStyle: {
          borderRadius: 5,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: true,
          formatter: '{b}: {c}件'
        }
      }
    ]
  }
  categoryChart.setOption(option)
}

// 初始化库存预警图表
const initInventoryChart = () => {
  if (!inventoryChartRef.value) return
  
  if (inventoryChart) {
    inventoryChart.dispose()
  }
  
  inventoryChart = echarts.init(inventoryChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: inventoryAlerts.value.map(item => item.name),
      axisLabel: {
        interval: 0,
        rotate: 45
      }
    },
    yAxis: {
      type: 'value',
      name: '库存数量'
    },
    series: [
      {
        name: '库存',
        type: 'bar',
        data: inventoryAlerts.value.map(item => item.stock),
        itemStyle: {
          color: function(params) {
            const value = params.value
            if (value < 10) {
              return '#EF4444'
            } else if (value < 50) {
              return '#F59E0B'
            } else {
              return '#10B981'
            }
          }
        }
      }
    ]
  }
  inventoryChart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  if (salesChart) salesChart.resize()
  if (categoryChart) categoryChart.resize()
  if (inventoryChart) inventoryChart.resize()
}

// 生命周期钩子
onMounted(() => {
  // 初始化图表
  initSalesChart()
  initCategoryChart()
  initInventoryChart()
  
  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)
  
  // 销毁图表实例
  if (salesChart) salesChart.dispose()
  if (categoryChart) categoryChart.dispose()
  if (inventoryChart) inventoryChart.dispose()
})
</script>

<style scoped>
@media (max-width: 475px) {
  .grid {
    @apply grid-cols-1;
  }
  table {
    @apply block overflow-x-auto whitespace-nowrap;
  }
}

/* 操作按钮样式 */
.action-button {
  @apply text-sm font-medium mr-3;
}

.view-button {
  @apply text-blue-600 dark:text-blue-400 hover:text-blue-900 dark:hover:text-blue-300;
}

.edit-button {
  @apply text-yellow-600 dark:text-yellow-400 hover:text-yellow-900 dark:hover:text-yellow-300;
}

.activate-button {
  @apply text-green-600 dark:text-green-400 hover:text-green-900 dark:hover:text-green-300;
}

.deactivate-button {
  @apply text-red-600 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300;
}

.delete-button {
  @apply text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-300;
}

/* 分页样式 */
.pagination-button {
  @apply relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 text-sm font-medium rounded-md text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600;
}

.pagination-button-active {
  @apply bg-blue-600 text-white border-blue-600;
}

/* 加载动画 */
.loading-spinner {
  @apply animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600;
}
</style> 