<template>
  <ContentWrap>
    <el-form
      class="-mb-15px run-report-form"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
    >
      <el-form-item :label="t('DataCollection.RunReport.searchDeviceCodeLabel')" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          :placeholder="t('DataCollection.RunReport.searchDeviceCodePlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('DataCollection.RunReport.searchDeviceNameLabel')" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          :placeholder="t('DataCollection.RunReport.searchDeviceNamePlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('DataCollection.RunReport.searchTimeRangeLabel')" prop="timeRange">
        <el-date-picker
          v-model="queryParams.timeRange"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          :start-placeholder="t('DataCollection.RunReport.searchTimeRangeStartPlaceholder')"
          :end-placeholder="t('DataCollection.RunReport.searchTimeRangeEndPlaceholder')"
          :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 type="primary" @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('DataCollection.RunReport.searchButtonText') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('DataCollection.RunReport.resetButtonText') }}
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" />
          {{ t('DataCollection.RunReport.exportButtonText') }}
        </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="t('DataCollection.RunReport.tableDeviceCodeColumn')"
        align="left"
        prop="deviceCode"
        min-width="140px"
      />
      <el-table-column
        :label="t('DataCollection.RunReport.tableDeviceNameColumn')"
        align="left"
        prop="deviceName"
        min-width="160px"
      />
      <el-table-column
        :label="t('DataCollection.RunReport.tableRunningTimeColumn')"
        align="center"
        prop="totalRunningTime"
        min-width="140px"
      />
      <el-table-column
        :label="t('DataCollection.RunReport.tableStandbyTimeColumn')"
        align="center"
        prop="totalStandbyTime"
        min-width="140px"
      />
      <el-table-column
        :label="t('DataCollection.RunReport.tableFaultTimeColumn')"
        align="center"
        prop="totalFaultTime"
        min-width="140px"
      />
      <!-- <el-table-column
        :label="t('DataCollection.RunReport.tableWarningTimeColumn')"
        align="center"
        prop="totalWarningTime"
        min-width="140px"
      /> -->
      <el-table-column
        :label="t('DataCollection.RunReport.tableUtilizationRateColumn')"
        align="center"
        prop="utilizationRate"
        min-width="120px"
      />
      <el-table-column
        :label="t('DataCollection.RunReport.tablePowerOnRateColumn')"
        align="center"
        prop="powerOnRate"
        min-width="120px"
      />
      <el-table-column
        :label="t('DataCollection.RunReport.tableStartTimeColumn')"
        align="center"
        prop="startTime"
        min-width="120px"
      />
      <el-table-column
        :label="t('DataCollection.RunReport.tableEndTimeColumn')"
        align="center"
        prop="endTime"
        min-width="120px"
      />
    </el-table>
    <Pagination
:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import download from '@/utils/download'
import { DeviceOperationRecordApi, type DeviceOperationRecordVO, type DeviceOperationRecordPageParams } from '@/api/iot/deviceOperationRecord'

defineOptions({ name: 'IotRunReport' })

const { t } = useI18n()

const loading = ref(false)
const list = ref<DeviceOperationRecordVO[]>([])
const total = ref(0)
const exportLoading = ref(false)

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceCode: undefined as string | undefined,
  deviceName: undefined as string | undefined,
  timeRange: [
    dayjs().subtract(1, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
    dayjs().subtract(1, 'day').endOf('day').format('YYYY-MM-DD HH:mm:ss')
  ] as string[],
  startTime: undefined as string | undefined,
  endTime: undefined as string | undefined,
  ids: undefined as string | undefined
})

const queryFormRef = ref()
const getList = async () => {
  const range = (queryParams.timeRange || []) as string[]
  queryParams.startTime = range[0]
  queryParams.endTime = range[1]
  loading.value = true
  try {
    const params = queryParams as unknown as DeviceOperationRecordPageParams
    const data = await DeviceOperationRecordApi.getDeviceOperationRecordPage(params)
    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 handleExport = async () => {
  try {
    await useMessage().exportConfirm()
    exportLoading.value = true
    const range = (queryParams.timeRange || []) as string[]
    queryParams.startTime = range[0]
    queryParams.endTime = range[1]
    const params = queryParams as unknown as DeviceOperationRecordPageParams
    const data = await DeviceOperationRecordApi.exportDeviceOperationReport(params)
    download.excel(data, t('DataCollection.RunReport.exportFilename'))
  } catch {
  } finally {
    exportLoading.value = false
  }
}

onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss">
.run-report-form :deep(.el-form-item__label) {
  min-width: 100px;
}
</style>
