<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="设备分类" prop="deviceCategory">
        <el-input
          v-model="queryParams.deviceCategory"
          placeholder="请输入设备分类"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="采购地方" prop="purchasePlace">
        <el-input
          v-model="queryParams.purchasePlace"
          placeholder="请输入采购地方"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </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="['iot:device-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['iot:device-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
        <el-button
          type="warning"
          plain
          @click="handleImport"
          v-hasPermi="['iot:device-info:import']"
        >
          <Icon icon="ep:upload" class="mr-5px" />
          导入
        </el-button>
        <el-button
          type="danger"
          plain
          @click="handleDeleteList"
          :disabled="selectedIds.length === 0"
          v-hasPermi="['iot:device-info:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" />
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="设备名称" align="center" prop="deviceName" min-width="120" />
      <el-table-column label="设备分类" align="center" prop="deviceCategory" min-width="100" />
      <el-table-column label="采购地方" align="center" prop="purchasePlace" min-width="100" />
      <el-table-column label="供应商" align="center" prop="supplier" min-width="120" />
      <el-table-column label="联系方式" align="center" prop="contactInfo" min-width="120" />
      <el-table-column label="采购价格" align="center" prop="purchasePrice" min-width="100">
        <template #default="scope">
          {{ scope.row.purchasePrice ? '¥' + scope.row.purchasePrice : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="picUrl" min-width="80">
        <template #default="scope">
          <el-image
            v-if="scope.row.picUrl"
            :src="scope.row.picUrl"
            :preview-src-list="[scope.row.picUrl]"
            class="w-[50px] h-[50px]"
            fit="cover"
            preview-teleported
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="150" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['iot:device-info:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
          >
            详情
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iot:device-info: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>

  <!-- 表单弹窗：添加/修改 -->
  <DeviceInfoForm ref="formRef" @success="getList" />
  <!-- 详情弹窗 -->
  <DeviceInfoDetail ref="detailRef" />
  <!-- 导入弹窗 -->
  <DeviceInfoImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { DeviceInfoApi, DeviceInfoVO } from '@/api/iot/device/deviceInfo'
import DeviceInfoForm from './DeviceInfoForm.vue'
import DeviceInfoDetail from './DeviceInfoDetail.vue'
import DeviceInfoImportForm from './DeviceInfoImportForm.vue'
import download from '@/utils/download'

/** 设备信息库列表 */
defineOptions({ name: 'IoTDeviceInfo' })

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

const loading = ref(true) // 列表加载中
const list = ref<DeviceInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceName: undefined,
  deviceCategory: undefined,
  supplier: undefined,
  purchasePlace: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出加载状态
const selectedIds = ref<number[]>([]) // 选中的编号数组

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

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

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

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

/** 详情操作 */
const detailRef = ref()
const openDetail = (row: DeviceInfoVO) => {
  detailRef.value.open(row)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await DeviceInfoApi.deleteDeviceInfo(id)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

/** 批量删除按钮操作 */
const handleDeleteList = async () => {
  try {
    await message.delConfirm()
    await DeviceInfoApi.deleteDeviceInfoList(selectedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

/** 导出方法 */
const handleExport = async () => {
  try {
    await message.exportConfirm()
    exportLoading.value = true
    const data = await DeviceInfoApi.exportDeviceInfoExcel(queryParams)
    download.excel(data, '设备信息库.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: DeviceInfoVO[]) => {
  selectedIds.value = selection.map((item) => item.id)
}

/** 设备导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

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