<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="组件名称" prop="componentName">
        <el-input
          v-model="queryParams.componentName"
          placeholder="请输入组件名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组件路径" prop="componentFilePath">
        <el-input
          v-model="queryParams.componentFilePath"
          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="tableList" @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="componentName" />
      <el-table-column label="组件类型" align="center" prop="componentCategory">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COCKPIT_COMPONENT_TYPE" :value="scope.row.componentCategory" />
        </template>
      </el-table-column>
      <el-table-column label="组件缩略图" align="left" prop="componentThumbnail" :show-overflow-tooltip="true">
        <template #default="scope">
          <div class="img-box" v-if="scope.row.componentThumbnail">
            <el-image
              style="width: 75px; height: 75px"
              :preview-teleported="true"
              class="images"
              :hide-on-click-modal="true"
              :src="scope.row.componentThumbnail"
              :zoom-rate="1.2"
              :preview-src-list="[scope.row.componentThumbnail]"
              :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="componentFilePath" />
      <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="primary"
            @click="goView(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>

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

<script setup lang="ts">
import * as cockpitApi from '@/api/system/cockpitComponent/index'
import ComponentForm from './componentForm.vue'
import {DICT_TYPE} from "@/utils/dict";
import { useRouter } from "vue-router"

const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const tableList=ref([])
const queryFormRef = ref() // 搜索的表单
const ids = ref() // 选中的数据
const multiple = ref(true) // 非多个禁用
const formRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: -1,
  componentName:'',
  componentFilePath:''
})

onMounted(()=>{
  getList()
})

// 获取组件数据
const getList=async()=>{
  loading.value = true
try{
const data=await cockpitApi.getUiComponentPage(queryParams)
  tableList.value=data.list
  // console.log(data, tableList.value,'shuju')
}finally {
  loading.value = false
}
}

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

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

// 表格数据选择
const handleSelectionChange=(selection:any[])=>{
  ids.value=selection.map((item)=>item.id)
  multiple.value=!selection.length
}

// 预览
const goView=async(id:number)=>{
  console.log(id)
  const pathData = await cockpitApi.selectUiComponentById(id)
  console.log(pathData,'路径')
  if (pathData && pathData.componentFilePath) {
    const fullPath = `/uiCom${pathData.componentFilePath}`
    router.push(fullPath)
  }
}

// 新增/修改
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 cockpitApi.deleteUiComponentList(realId)
    message.success(t('common.delSuccess'))
    await getList()
  }catch (error) {
    console.error('删除失败:', error)
    message.error(t('common.delFailed'))
  }
}

</script>

<style scoped lang="scss">

</style>
