<script setup>
import { ref } from 'vue'
import { Edit, Delete, Upload } from '@element-plus/icons-vue'
import defaultImg from '@/assets/cover.jpg'
import { typeQueryService, typeDelService } from '@/api/type'
import TypeEdit from './components/TypeEdit.vue'
import PhotoUpdate from '@/components/PhotoUpdate.vue'
import PhotoShow from '@/components/PhotoShow.vue'
const loading = ref(false)
const typeList = ref()
const total = ref(0)
const params = ref({
  current: 1,
  size: 10
})

const getTypeList = async () => {
  loading.value = true
  const {
    data: { data }
  } = await typeQueryService(params.value)
  loading.value = false
  console.log(typeof data.list[0].openTime)
  typeList.value = data.list
  total.value = data.total
}
getTypeList()

const onCurrentChange = (current) => {
  params.value.current = current
  getTypeList()
}
const onSizeChange = (size) => {
  params.value.current = 1
  params.value.size = size
  getTypeList()
}

const typeEditRef = ref()
const onEdit = (row) => {
  typeEditRef.value.open(row)
}

// 图片放大
const photoShowRef = ref()
const imgPreview = (imgUrl) => {
  photoShowRef.value.open(imgUrl)
}

// 修改图片
const photoUpdateRef = ref()
const photoUpdate = (tid, tUrl) => {
  photoUpdateRef.value.open(tid, tUrl, 'venue')
}

// 移除场馆信息
const onDel = async (id) => {
  ElMessageBox.confirm('确认移除该条数据？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await typeDelService(id)
      ElMessage.success('移除成功')
      getTypeList()
    })
    .catch(() => {})
}
</script>
<template>
  <page-container title="场馆信息列表">
    <template #extra>
      <el-button type="primary" @click="onEdit">添加场馆信息</el-button>
    </template>

    <!-- 表格区域 -->
    <el-table
      :data="typeList"
      stripe
      v-loading="loading"
      element-loading-text="场馆数据加载中......"
    >
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column label="场馆照片">
        <template #default="{ row }">
          <img
            style="height: 100px; width: 120px; cursor: pointer"
            :src="
              row.imgUrl === null || row.imgUrl === '' || row.imgUrl === ' '
                ? defaultImg
                : row.imgUrl
            "
            :title="row.venueName"
            @click="imgPreview(row.imgUrl)"
          />
        </template>
      </el-table-column>
      <el-table-column label="场馆名称" prop="vname" />
      <el-table-column label="位置" prop="location" />
      <el-table-column label="开馆时间" prop="openTime" />
      <el-table-column label="闭馆时间" prop="closeTime" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEdit(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDel(row.id)"
          ></el-button>
          <el-button
            circle
            plain
            type="info"
            :icon="Upload"
            title="上传场馆图片"
            @click="photoUpdate(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"
    />

    <!-- 抽屉 -->
    <TypeEdit ref="typeEditRef" @success="onSizeChange(5)"></TypeEdit>
    <!-- 弹窗 -->
    <PhotoUpdate ref="photoUpdateRef" @success="onSizeChange(5)"></PhotoUpdate>
    <!-- 图片放大 -->
    <PhotoShow ref="photoShowRef"></PhotoShow>
  </page-container>
</template>
