<template>
  <div class="advert-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <el-table-column prop="id" label="ID" width="80" align="center" />
      
      <el-table-column label="广告类型" width="120" align="center">
        <template #default="{ row }">
          <el-tag size="small">{{ row.typeName }}</el-tag>
        </template>
      </el-table-column>
      
      <el-table-column label="广告图片" width="150" align="center">
        <template #default="{ row }">
          <el-image
            v-if="row.imageUrl"
            :src="row.imageUrl"
            :preview-src-list="[row.imageUrl]"
            fit="cover"
            class="advert-image"
            preview-teleported
          >
            <template #error>
              <div class="image-error">
                <el-icon><Picture /></el-icon>
              </div>
            </template>
          </el-image>
          <span v-else class="no-image">暂无图片</span>
        </template>
      </el-table-column>
      
      <el-table-column label="跳转类型" width="120" align="center">
        <template #default="{ row }">
          <span>{{ row.jumpTypeName || getJumpTypeName(row.jumpType) }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="跳转目标" min-width="150">
        <template #default="{ row }">
          <span v-if="row.jumpType === AdvertJumpType.None">-</span>
          <span v-else-if="row.jumpType === AdvertJumpType.Coupon">优惠券ID: {{ row.couponId }}</span>
          <span v-else-if="row.jumpType === AdvertJumpType.YiFanShang || row.jumpType === AdvertJumpType.WuXianShang || row.jumpType === AdvertJumpType.LianJiShang">
            盒子ID: {{ row.goodsId }}
          </span>
          <span v-else-if="row.jumpType === AdvertJumpType.CustomUrl" class="url-text">
            {{ row.urlLink }}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      
      <el-table-column prop="sort" label="排序值" width="100" align="center" />
      
      <el-table-column label="创建时间" width="160" align="center">
        <template #default="{ row }">
          {{ row.createdAt || '-' }}
        </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 {
  AdvertJumpType,
  AdvertJumpTypeLabels,
  type AdvertResponse
} from '@/api/business/advert'

interface Props {
  data: AdvertResponse[]
  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: AdvertResponse): void
  (e: 'delete', row: AdvertResponse): 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 getJumpTypeName = (jumpType: number): string => {
  return AdvertJumpTypeLabels[jumpType] || '未知'
}

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

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

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

const handleDelete = (row: AdvertResponse) => {
  emit('delete', row)
}
</script>

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

.advert-image {
  width: 80px;
  height: 60px;
  border-radius: 4px;
}

.image-error {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 80px;
  height: 60px;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
}

.no-image {
  color: #909399;
  font-size: 12px;
}

.url-text {
  color: #409eff;
  word-break: break-all;
}

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