<template>
  <div class="floatball-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <el-table-column prop="id" label="ID" width="70" align="center" />
      
      <el-table-column prop="title" label="标题" width="120" align="center">
        <template #default="{ row }">
          {{ row.title || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="悬浮球图片" width="100" align="center">
        <template #default="{ row }">
          <el-image
            v-if="row.image"
            :src="row.image"
            :preview-src-list="[row.image]"
            fit="cover"
            class="table-image"
            preview-teleported
            lazy
          >
            <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="100" align="center">
        <template #default="{ row }">
          <el-image
            v-if="row.imageBj"
            :src="row.imageBj"
            :preview-src-list="[row.imageBj]"
            fit="cover"
            class="table-image"
            preview-teleported
            lazy
          >
            <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="100" align="center">
        <template #default="{ row }">
          <el-image
            v-if="row.imageDetails"
            :src="row.imageDetails"
            :preview-src-list="[row.imageDetails]"
            fit="cover"
            class="table-image"
            preview-teleported
            lazy
          >
            <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="100" align="center">
        <template #default="{ row }">
          <el-tag :type="row.type === FloatBallType.ShowImage ? 'info' : 'primary'" size="small">
            {{ FloatBallTypeLabels[row.type] || '未知' }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column label="跳转链接" min-width="150">
        <template #default="{ row }">
          <span v-if="row.type === FloatBallType.JumpPage && row.linkUrl" class="url-text">
            {{ row.linkUrl }}
          </span>
          <span v-else class="no-image">-</span>
        </template>
      </el-table-column>
      
      <el-table-column label="位置" width="100" align="center">
        <template #default="{ row }">
          <span>{{ row.positionX }}, {{ row.positionY }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="尺寸" width="100" align="center">
        <template #default="{ row }">
          <span>{{ row.width }} × {{ row.height }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="特效" width="100" align="center">
        <template #default="{ row }">
          <el-tag :type="row.effect === FloatBallEffect.Scale ? 'success' : 'info'" size="small">
            {{ FloatBallEffectLabels[row.effect] || '无特效' }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column label="状态" width="90" align="center">
        <template #default="{ row }">
          <el-switch
            :model-value="row.status === 1"
            @change="(val: boolean) => handleStatusChange(row, val ? 1 : 0)"
            inline-prompt
            active-text="开"
            inactive-text="关"
          />
        </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="130" 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 {
  FloatBallType,
  FloatBallTypeLabels,
  FloatBallEffect,
  FloatBallEffectLabels,
  type FloatBallResponse
} from '@/api/business/floatball'

interface Props {
  data: FloatBallResponse[]
  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: FloatBallResponse): void
  (e: 'delete', row: FloatBallResponse): void
  (e: 'status-change', row: FloatBallResponse, 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 handlePageChange = (page: number) => {
  emit('page-change', page)
}

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

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

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

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

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

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

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

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

.url-text {
  color: #409eff;
  word-break: break-all;
  font-size: 12px;
}

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