<script setup>
import { ref } from 'vue'
import { formatDateTime } from '@/utils/format'
import { hotQueryService, hotDelService } from '@/api/hot'
import PhotoShow from '@/components/PhotoShow.vue'
import HotEdit from './components/HotEdit.vue'
import PhotoUpdate from '@/components/PhotoUpdate.vue'
import defaultImg from '@/assets/cover.jpg'
const loading = ref(false)
const hotList = ref()
const params = ref({
  current: 1,
  size: 10
})
const total = ref(0)
const getHotList = async () => {
  loading.value = true
  const {
    data: { data }
  } = await hotQueryService(params.value)
  hotList.value = data.list
  total.value = data.total
  loading.value = false
}
getHotList()
// 图片放大
const imgRef = ref()
const imgPreview = (imgUrl) => {
  imgRef.value.open(imgUrl)
}

// 编辑热点信息
const editRef = ref()
const onEdit = (row) => {
  editRef.value.open(row)
}

// 上传图片
const uploadRef = ref()
const upload = (id, url) => {
  uploadRef.value.open(id, url, 'hot')
}

// 删除记录
const onDelete = (id) => {
  ElMessageBox.confirm('确认删除该条信息？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await hotDelService(id)
      ElMessage.success('删除成功')
      getHotList()
    })
    .catch(() => {})
}

const onCurrentChange = (current) => {
  params.value.current = current
  getHotList()
}

const onSizeChange = (size) => {
  params.value.current = 1
  params.value.size = size
  getHotList()
}
</script>
<template>
  <page-container title="轮播事件管理">
    <template #extra>
      <el-button type="primary" @click="onEdit({ id: 0 })"
        >+ 添加活动
      </el-button>
    </template>
    <!-- 表格区域 -->
    <el-table :data="hotList" stripe v-loading="loading">
      <el-table-column label="场地照片" width="160px">
        <template #default="{ row }">
          <img
            style="
              height: 80px;
              width: 120px;
              cursor: pointer;
              border-radius: 4px;
              box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
            "
            :src="
              row.imgUrl !== null && row.imgUrl !== '' && row.imgUrl != ' '
                ? row.imgUrl
                : defaultImg
            "
            :title="row.venueName"
            @click="imgPreview(row.imgUrl)"
          />
        </template>
      </el-table-column>
      <el-table-column label="描述" prop="description"></el-table-column>
      <el-table-column label="添加时间">
        <template #default="{ row }">
          {{ formatDateTime(row.addTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button link type="success" @click="onEdit(row)" title="编辑描述"
            >编辑</el-button
          >
          <el-button link type="primary" @click="onDelete(row.id)"
            >删除</el-button
          >
          <el-button link type="info" @click="upload(row.id, row.imgUrl)"
            >上传图片</el-button
          >
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="无热点数据" />
      </template>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.current"
      v-model:page-size="params.size"
      :page-sizes="[10, 20, 50]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </page-container>
  <!-- 图片预览框 -->
  <PhotoShow ref="imgRef"></PhotoShow>
  <!-- 事件编辑 -->
  <HotEdit ref="editRef" @success="onSizeChange(5)"></HotEdit>
  <!-- 上传图片 -->
  <PhotoUpdate ref="uploadRef" @success="onSizeChange(5)"></PhotoUpdate>
</template>
