<template>

  111 
  <ElTablePro
    :data="tableData"
    :total="total"
    v-model:page="query.page"
    v-model:page-size="query.pageSize"
    table-key="product"
    row-key="id"
    border
    stripe
    v-loading="loading"
    @pagination-change="handlePaginationChange"
  >
    <template #toolbar>
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </template>

    <el-table-column type="expand">
      <template #default="{ row }">
        <div class="expand-detail">
          <el-descriptions :column="2" border size="small">
            <el-descriptions-item label="商品 ID">{{ row.id }}</el-descriptions-item>
            <el-descriptions-item label="商品名称">{{ row.name }}</el-descriptions-item>
            <el-descriptions-item label="分类">
              <el-tag :type="categoryTagType(row.category)">{{ row.category }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="价格">{{ formatPrice(row.price) }}</el-descriptions-item>
            <el-descriptions-item label="库存">{{ row.stock }}</el-descriptions-item>
            <el-descriptions-item label="状态">
              <el-tag :type="row.status ? 'success' : 'danger'">
                {{ row.status ? '启用' : '禁用' }}
              </el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </template>
    </el-table-column>

    <el-table-column
      v-for="col in visibleColumns"
      :key="col.prop"
      :prop="col.prop"
      :label="col.label"
      :width="col.width"
      :min-width="col.minWidth"
      :visible="col.visible"
      :formatter="col.formatter"
    >
      <template v-if="col.slot" #default="{ row }">
        <el-tag
          v-if="col.prop === 'category'"
          :type="categoryTagType(row.category)"
        >
          {{ row.category }}
        </el-tag>
        <el-tag
          v-else-if="col.prop === 'status'"
          :type="row.status ? 'success' : 'danger'"
        >
          {{ row.status ? '启用' : '禁用' }}
        </el-tag>
        <template v-else-if="col.prop === 'action'">
          <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </template>
    </el-table-column>
  </ElTablePro>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import ElTablePro from '@/components/ElTablePro.vue'
import { priceFormatter } from '@/utils/formatters'

interface ColumnConfig {
  prop: string
  label: string
  width?: number | string
  minWidth?: number | string
  slot?: boolean
  visible?: boolean
  formatter?: (row: any, column: any, cellValue: any, index: number) => string
}

const columns: ColumnConfig[] = [
  { prop: 'id', label: 'ID', width: 80 },
  { prop: 'name', label: '商品名称', minWidth: 150, visible: false },
  { prop: 'category', label: '分类', width: 120, slot: true },
  { prop: 'price', label: '价格', width: 100, formatter: priceFormatter },
  { prop: 'stock', label: '库存', width: 80 },
  { prop: 'status', label: '状态', width: 100, slot: true },
  { prop: 'action', label: '操作', width: 180, slot: true },
]

const visibleColumns = columns

const loading = ref(false)
const total = ref(0)

const query = reactive({
  page: 1,
  pageSize: 10,
})

const allData = [
  { id: 1, name: 'MacBook Pro 16"', category: '电子产品', price: 18999, stock: 50, status: true },
  { id: 2, name: 'iPhone 17 Pro', category: '电子产品', price: 8999, stock: 120, status: true },
  { id: 3, name: 'AirPods Max', category: '电子产品', price: 4399, stock: 80, status: false },
  { id: 4, name: '耐克运动鞋', category: '服装鞋帽', price: 899, stock: 200, status: true },
  { id: 5, name: '优衣库羽绒服', category: '服装鞋帽', price: 599, stock: 150, status: true },
  { id: 6, name: '三只松鼠坚果礼盒', category: '食品饮料', price: 128, stock: 300, status: true },
  { id: 7, name: '飞利浦电动牙刷', category: '家居用品', price: 399, stock: 90, status: true },
  { id: 8, name: '小米空气净化器', category: '家居用品', price: 1299, stock: 60, status: false },
  { id: 9, name: '星巴克咖啡豆', category: '食品饮料', price: 158, stock: 250, status: true },
  { id: 10, name: '华为 MatePad Pro', category: '电子产品', price: 3699, stock: 70, status: true },
  { id: 11, name: '阿迪达斯跑步鞋', category: '服装鞋帽', price: 799, stock: 180, status: true },
  { id: 12, name: '戴森吹风机', category: '家居用品', price: 3199, stock: 40, status: true },
]

const tableData = ref<any[]>([])

const categoryTagType = (category: string) => {
  const map: Record<string, string> = {
    '电子产品': 'primary',
    '服装鞋帽': 'warning',
    '食品饮料': 'success',
    '家居用品': 'info',
  }
  return map[category] || 'info'
}

const formatPrice = (value: number) => `¥${Number(value).toLocaleString()}`

const getList = () => {
  loading.value = true
  setTimeout(() => {
    const start = (query.page - 1) * query.pageSize
    const end = start + query.pageSize
    tableData.value = allData.slice(start, end)
    total.value = allData.length
    loading.value = false
  }, 300)
}

const handlePaginationChange = (_value: { page: number; pageSize: number }) => {
  getList()
}

const handleAdd = () => {
  ElMessage.info('新增商品')
}

const handleEdit = (row: any) => {
  ElMessage.info(`编辑商品: ${row.name}`)
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确认删除商品 ${row.name}？`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    ElMessage.success('删除成功')
  }).catch(() => {})
}

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

<style scoped>
.expand-detail {
  padding: 12px 24px;
}
</style>
