<template>
  <div class="diamond-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <el-table-column prop="id" label="ID" width="80" align="center" />
      
      <el-table-column prop="name" label="商品名称" min-width="120" />
      
      <el-table-column prop="productsId" label="商品编号" width="120" align="center" />
      
      <el-table-column prop="price" label="价格" width="100" align="right">
        <template #default="{ row }">
          <span class="money">¥{{ row.price?.toFixed(2) || '0.00' }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="是否首充" width="90" align="center">
        <template #default="{ row }">
          <el-tag :type="row.isFirst === 1 ? 'success' : 'info'" size="small">
            {{ row.isFirst === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column label="展示图" width="100" align="center">
        <template #default="{ row }">
          <el-image
            v-if="getDisplayImage(row)"
            :src="getDisplayImage(row)"
            :preview-src-list="[getDisplayImage(row)]"
            fit="cover"
            style="width: 50px; height: 50px; border-radius: 4px;"
            preview-teleported
          >
            <template #error>
              <div class="image-error">
                <el-icon><Picture /></el-icon>
              </div>
            </template>
          </el-image>
          <span v-else class="text-muted">-</span>
        </template>
      </el-table-column>
      
      <el-table-column prop="sortOrder" label="排序" width="80" align="center" />
      
      <el-table-column label="状态" width="90" align="center">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="(val: number) => handleStatusChange(row, val)"
          />
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" width="160" align="center">
        <template #default="{ row }">
          {{ row.createdAt || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="更新时间" width="160" align="center">
        <template #default="{ row }">
          {{ row.updatedAt || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="150" align="center" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link size="small" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button type="danger" link size="small" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="currentPageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
      class="pagination"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { Picture } from '@element-plus/icons-vue'
import type { DiamondProductResponse } from '@/api/business/diamond'

interface Props {
  data: DiamondProductResponse[]
  loading: boolean
  total: number
  page: number
  pageSize: number
}

const props = defineProps<Props>()

const emit = defineEmits<{
  (e: 'page-change', page: number): void
  (e: 'size-change', size: number): void
  (e: 'edit', row: DiamondProductResponse): void
  (e: 'delete', row: DiamondProductResponse): void
  (e: 'status-change', row: DiamondProductResponse, status: number): void
}>()

const currentPage = ref(props.page)
const currentPageSize = ref(props.pageSize)

watch(() => props.page, (val) => {
  currentPage.value = val
})

watch(() => props.pageSize, (val) => {
  currentPageSize.value = val
})

// 获取展示图片（首充商品显示首充图，否则显示普通图）
const getDisplayImage = (row: DiamondProductResponse): string => {
  if (row.isFirst === 1 && row.firstChargeImage) {
    return row.firstChargeImage
  }
  return row.normalImage || ''
}

const handlePageChange = (page: number) => {
  emit('page-change', page)
}

const handleSizeChange = (size: number) => {
  emit('size-change', size)
}

const handleEdit = (row: DiamondProductResponse) => {
  emit('edit', row)
}

const handleDelete = (row: DiamondProductResponse) => {
  emit('delete', row)
}

const handleStatusChange = (row: DiamondProductResponse, status: number) => {
  emit('status-change', row, status)
}
</script>

<style scoped>
.diamond-table {
  width: 100%;
}

.money {
  color: #e6a23c;
  font-weight: 500;
}

.text-muted {
  color: #909399;
}

.image-error {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 50px;
  background: #f5f7fa;
  color: #909399;
  border-radius: 4px;
}

.pagination {
  margin-top: 16px;
  justify-content: flex-end;
}
</style>
