<template>
  <el-row :gutter="20">
    <el-col :span="5" :xs="24">
      <ContentWrap class="h-1/1">
            <FileTree @node-click="handleFileNodeClick" :deviceId="deviceId" />
      </ContentWrap>
    </el-col>
    <el-col :span="19">
      <ContentWrap>
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="文件名称" prop="filename">
            <el-input
              v-model="queryParams.filename"
              placeholder="请输入文件名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="文件类型" prop="fileType">
            <el-select
              v-model="queryParams.fileType"
              placeholder="请选择文件类型"
              clearable
              class="!w-240px"
            >
              <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
            <el-button @click="resetQuery"><Icon icon="ep:refresh" />重置</el-button>
            <el-button type="primary" plain @click="openForm('create')">
              <Icon icon="ep:plus" /> 文件上传
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>
      <ContentWrap>
        <el-table v-loading="loading" :data="list">
          <el-table-column label="设备名称" align="center" prop="deviceId" />
          <el-table-column label="资料分类" align="center" prop="classId" />
          <el-table-column label="文件名称" align="center" prop="filename" />
          <el-table-column label="文件类型" align="center" prop="fileType" />
          <el-table-column label="文件路径" align="center" prop="filePath" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180px"
          />
          <el-table-column label="操作" align="center" width="160">
            <template #default="scope">
              <div class="flex items-center justify-center">
                <el-button
                  type="primary"
                  link
                  @click="openForm('update', scope.row.id)"
                >
                  <Icon icon="ep:edit" />修改
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>
  <IotInfoForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import { IotDeviceApi, IotDeviceVO } from '@/api/pms/device'
import FileTree from '@/views/pms/device/FileTree.vue'
import { dateFormatter } from '@/utils/formatTime'
import IotInfoForm from "@/views/pms/iotinfo/IotInfoForm.vue";
import * as IotInfoApi from '@/api/pms/iotinfo'
defineOptions({ name: 'DeviceUpload' })
const queryFormRef = ref() // 搜索的表单
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { params } = useRoute() // 查询参数
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const activeName = ref('info') // Tag 激活的窗口
const id = params.id as unknown as number
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  fileName: null,
  createTime: [],
  deviceId: null,
  classId: null,
})
// SPU 表单数据
const formData = ref({
  id: undefined,
  deviceId: undefined,
  orgId: undefined,
  filename: undefined,
  fileType: undefined,
  filePath: undefined,
  remark: undefined,
  classId: undefined,
})
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
const deviceId = ref('')
const handleFileNodeClick = async (row) => {
  queryParams.classId = row.id
  await getList()
}
/** 获得详情 */
const getDetail = async () => {
  if (id) {
    formLoading.value = true
    try {
      formData.value = (await IotDeviceApi.getIotDevice(id)) as IotDeviceVO
    } finally {
      formLoading.value = false
    }
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.deviceId = deviceId.value
    const data = await IotInfoApi.IotInfoApi.getIotInfoPage(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()
}
/** 初始化 */
onMounted(async () => {
  await getDetail()

  deviceId.value = params.id as unknown as number

})
</script>
<style scoped></style>
