<template>
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="产线编码" prop="lineNode">
        <el-input
          v-model="queryParams.lineNode"
          placeholder="请输入产线编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产线名称" prop="lineName">
        <el-input
          v-model="queryParams.lineName"
          placeholder="请输入产线名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </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-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      row-key="id"
    >
      <el-table-column label="产线编码" align="left" prop="lineNode" min-width="140px" />
      <el-table-column label="产线名称" align="left" prop="lineName" min-width="160px" />
      <el-table-column label="设备编码" align="left" prop="deviceCode" min-width="140px" />
      <el-table-column label="设备名称" align="left" prop="deviceName" min-width="160px" />
      <el-table-column label="连接状态" align="center" prop="status" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.IOT_GATEWAY_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="最新采集时间"
        align="center"
        prop="collectionTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" fixed="right" width="150px">
        <template #default="scope">
          <el-button link type="primary" @click="handleSingleMonitor(scope.row)">单设备监控</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { DeviceApi, LineDeviceVO } from '@/api/iot/device'

defineOptions({ name: 'RealTimeMonitoring' })

const message = useMessage()
const router = useRouter()

const loading = ref(true)
const list = ref<LineDeviceVO[]>([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  lineNode: undefined,
  lineName: undefined,
  deviceCode: undefined,
  deviceName: undefined,
  status: undefined,
  collectionTime: undefined
})
const queryFormRef = ref() // 搜索的表单

const buildQueryParams = () => {
  const params: Record<string, any> = {
    pageNo: queryParams.pageNo,
    pageSize: queryParams.pageSize
  }
  const keys = ['id', 'lineNode', 'lineName', 'deviceCode', 'deviceName', 'status', 'collectionTime']
  for (const key of keys) {
    const value = (queryParams as any)[key]
    if (value === undefined || value === null || value === '') {
      continue
    }
    params[key] = value
  }
  return params
}

const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceApi.getLineDevicePage(buildQueryParams())
    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 handleSingleMonitor = async (row: LineDeviceVO) => {
  try {
    await router.push({
      name: 'MqttDataRecord',
      query: { deviceName: row.deviceName }
    })
  } catch {
    message.info('单设备监控页面未配置')
  }
}

let timer: any = null

onMounted(() => {
  getList()
  timer = setInterval(async () => {
    try {
      const data = await DeviceApi.getLineDevicePage(buildQueryParams())
      list.value = data.list
      total.value = data.total
    } catch {}
  }, 5000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>
