<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="地图名称" prop="mapId">
        <el-select v-model="queryParams.mapId" placeholder="地图名称" class="!w-240px">
          <el-option
            v-for="item in mapOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </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-point:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete()"
          v-hasPermi="['iscs:map-point: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" width="210px" prop="mapName" />
      <el-table-column label="地图类型" align="center" prop="mapType" :show-overflow-tooltip="true">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MAP_TYPE" :value="scope.row.mapType" />
        </template>
      </el-table-column>
      <el-table-column label="实体名称" align="center" width="210px" prop="entityName" />
      <el-table-column label="横坐标" align="center" prop="x" width="180" />
      <el-table-column label="纵坐标" align="center" prop="y" width="180" />
      <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-point:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iscs:map-point: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>

  <!-- 表单弹窗：添加/修改 -->
  <MapPointForm
    ref="formRef"
    :map-options="mapOptions"
    :dept-options="deptOptions"
    :spm-options="spmOptions"
    @success="getList"
  />
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as MapPointApi from '@/api/basic/mappoint'
import * as MapApi from '@/api/basic/mapconfig'
import * as SpmApi from '@/api/dv/spm/index'
import MapPointForm from './MapPointForm.vue'
import * as MarsDeptApi from '@/api/system/marsdept/index'
import { handleTree } from '@/utils/tree'

defineOptions({ name: 'SystemMapPoint' })

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

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

// 下拉选项数据
const mapOptions = ref([]) // 地图选项
const deptOptions = ref([]) // 岗位选项
const spmOptions = ref([]) // 隔离点选项

const queryFormRef = ref() // 搜索的表单

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

/** 获取其他列表数据 */
const getOtherList = async () => {
  const params = { pageNo: 1, pageSize: -1 }
  try {
    const [mapRes, deptRes, spmRes] = await Promise.all([
      MapApi.getIsMapPage(params),
      MarsDeptApi.listMarsDept(params),
      SpmApi.getIsIsolationPointPage(params)
    ])
    console.log(mapRes,'地图名称哒哒哒')
    mapOptions.value = mapRes.list
    console.log(mapOptions.value,'地图名称')
    deptOptions.value = handleTree(deptRes.list)
    spmOptions.value = spmRes.list
  } catch (error) {
    console.error('获取下拉选项数据失败:', error)
  }
}



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

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

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {

  ids.value = selection.map(item => item.id)
  console.log(ids.value,'ids.value')
  multiple.value = !selection.length
}
/** 删除按钮操作 */
/** 删除按钮操作 */
// const handleDelete = async (id?: number) => {
//   try {
//     // 如果传了 id，单删；否则批删
//     const Pointsids = id ? [id] : ids.value
//
//     // 批删时如果没选中任何数据
//     if (!Pointsids.length) {
//       message.warning('请选择要删除的数据')
//       return
//     }
//
//     await message.delConfirm()
//     await MapPointApi.deleteIsMapPointByIds(Pointsids)
//     message.success(t('common.delSuccess'))
//     await getList()
//   } catch {}
// }

const handleDelete = async (id: number) => {
  try {
    const Pointsids=id||ids.value
    console.log(Pointsids,'Pointsids')
    await message.delConfirm()
    await MapPointApi.deleteIsMapPointByIds(Pointsids)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}



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