<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <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 label="采集时间" prop="collectionTimeRange">
        <el-date-picker
          v-model="queryParams.collectionTimeRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-360px"
        />
      </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="collectionTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" fixed="right" width="150px">
        <template #default="scope">
          <el-button link type="primary" @click="handleSingleView(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>

  <HistorySingleDeviceDialog
    v-model="singleDialogVisible"
    :device-name="singleDeviceName"
    :collection-time="singleCollectionTime"
  />
</template>

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

defineOptions({ name: 'HistoryData' })

const loading = ref(true)
const list = ref<LineDeviceVO[]>([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined as string | number | undefined,
  lineNode: undefined as string | undefined,
  lineName: undefined as string | undefined,
  deviceCode: undefined as string | undefined,
  deviceName: undefined as string | undefined,
  status: undefined as string | number | undefined,
  collectionTimeRange: [] as string[]
})

const queryFormRef = ref()

const singleDialogVisible = ref(false)
const singleDeviceName = ref<string>('')
const singleCollectionTime = ref<string | number | undefined>()

const buildQueryParams = (): LineDevicePageParams => {
  const params: LineDevicePageParams = {
    pageNo: queryParams.pageNo,
    pageSize: queryParams.pageSize
  }

  if (queryParams.lineNode) {
    params.lineNode = queryParams.lineNode
  }
  if (queryParams.lineName) {
    params.lineName = queryParams.lineName
  }
  if (queryParams.deviceCode) {
    params.deviceCode = queryParams.deviceCode
  }
  if (queryParams.deviceName) {
    params.deviceName = queryParams.deviceName
  }

  if (Array.isArray(queryParams.collectionTimeRange) && queryParams.collectionTimeRange.length === 2) {
    params.collectionTimeStart = queryParams.collectionTimeRange[0]
    params.collectionTimeEnd = queryParams.collectionTimeRange[1]
  }

  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()
  queryParams.collectionTimeRange = []
  handleQuery()
}

const handleSingleView = (row: LineDeviceVO) => {
  singleDeviceName.value = row.deviceName || ''
  singleCollectionTime.value = (row as any).collectionTime
  singleDialogVisible.value = true
}

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

const activatedOnce = ref(false)
onActivated(() => {
  if (!activatedOnce.value) {
    activatedOnce.value = true
    return
  }
  getList()
})
</script>
