<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="地图名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入地图名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['iscs:map:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iscs:map:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" />
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="地图编号" align="center" prop="id" />
      <el-table-column label="地图名称" align="center" prop="name" />
      <el-table-column label="地图图片" align="left" prop="imageUrl" :show-overflow-tooltip="true">
        <template #default="scope">
          <div class="img-box" v-if="scope.row.imageUrl">
            <el-image
              style="width: 75px; height: 75px"
              :preview-teleported="true"
              class="images"
              :hide-on-click-modal="true"
              :src="scope.row.imageUrl"
              :zoom-rate="1.2"
              :preview-src-list="[scope.row.imageUrl]"
              :initial-index="1"
            />
            <Icon icon="ep:zoom-in" class="eye-icon" />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="地图宽度" align="center" prop="width" />
      <el-table-column label="地图高度" align="center" prop="height" />
      <el-table-column label="横坐标" align="center" prop="x" />
      <el-table-column label="纵坐标" align="center" prop="y" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['iscs:map:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iscs:map:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MapConfigForm ref="formRef" @success="getList" />
</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as MapConfigApi from '@/api/basic/mapconfig'
import MapConfigForm from './MapConfigForm.vue'

defineOptions({ name: 'SystemMap' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const ids = ref() // 选中的数据
const multiple = ref(true) // 非多个禁用
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询地图列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MapConfigApi.getIsMapPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value?.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id?: number) => {
  try {
    await message.delConfirm()
    // 类型守卫处理
    const realId = id instanceof PointerEvent
      ? ids.value
      : id || ids.value;
    await MapConfigApi.deleteIsMapByIds(realId)
    message.success(t('common.delSuccess'))
    await getList()
  } catch (error) {
    console.error('删除失败:', error)
    message.error(t('common.delFailed'))
  }
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map((item) => item.id)
  multiple.value = !selection.length
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.img-box {
  width: 75px;
  height: 75px;
  position: relative;

  .eye-icon {
    display: none;
  }

  &:hover {
    background: #000;

    .images {
      opacity: 0.6;
    }

    .eye-icon {
      display: block;
      position: absolute;
      top: 40%;
      left: 40%;
      z-index: 100;
      color: white;
      pointer-events: none;
    }
  }
}
</style>
