<template>
  <div class="welfarehouse-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" align="center">
        <template #default="{ row }">
          {{ row.name || '-' }}
        </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="跳转链接" min-width="180">
        <template #default="{ row }">
          <span v-if="row.url" class="url-text">{{ row.url }}</span>
          <span v-else class="no-image">-</span>
        </template>
      </el-table-column>
      
      <el-table-column prop="sort" label="排序" width="80" align="center" />
      
      <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.createTime || '-' }}
        </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 type { WelfareHouseResponse } from '@/api/business/welfarehouse'

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

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

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

<style scoped>
.welfarehouse-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>
