<!--
  Description: 设备耗电分析  -废弃
  Author: 李亚光
  Date: 2025-05-28
 -->
<script name="DeviceElectricityList" lang="ts" setup>
import dayjs from 'dayjs'
import type { DateModelType } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDeviceBizDataListPage } from '@/api/data/query'
const props = defineProps({
  devcode: {
    type: String,
    default: '',
  },
})
const dataList = ref([])
const total = ref(0)
const loadingTable = ref(false)
const columns = ref<TableColumn[]>([
  { text: '电量', value: 'devcode', align: 'center' },
  { text: '采集时间', value: 'devcode', align: 'center' },
])
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const searchQuery = ref({
  beginTime: '',
  endTime: '',
  devcode: props.devcode,
  offset: 1,
  limit: 10
})
watch(dateRange, (val) => {
  if (val) {
    searchQuery.value.beginTime = dayjs(val[0]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[0]).format('YYYY-MM-DD')
    searchQuery.value.endTime = dayjs(val[1]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[1]).format('YYYY-MM-DD')
  }
  else {
    searchQuery.value.beginTime = ''
    searchQuery.value.endTime = ''
  }
})
const searchList = () => {
  loadingTable.value = true
  getDeviceBizDataListPage(searchQuery.value).then(res => {
    total.value = res.data.total
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.value.limit = val.size
  }
  if (val && val.page) {
    searchQuery.value.offset = val.page
  }
  searchList()
}
// 重置查询条件
const reset = () => {
  dateRange.value = ['', '']
  searchQuery.value = {
    beginTime: '',
    endTime: '',
    devcode: props.devcode,
    offset: 1,
    limit: 10
  }
  searchList()
}
onMounted(() => {
  searchQuery.value.beginTime = dayjs(new Date().valueOf() - 3600 * 24 * 6 * 1000).format('YYYY-MM-DD')
  searchQuery.value.endTime = dayjs(new Date().valueOf() + 3600 * 24 * 1000).format('YYYY-MM-DD')
  dateRange.value = [searchQuery.value.beginTime, searchQuery.value.endTime]
  setTimeout(() => {
    searchList()
  })
})

// 导出
const exportElectricityList = () => {

}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-date-picker v-model="dateRange" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" />
      </search-item>
    </search-area>
    <el-row :gutter="24">
      <el-col :span="10">
        <table-container title="设备数据列表">
          <!-- 表头区域 -->
          <template #btns-right>
            <el-button type="primary" @click="exportElectricityList">
              导出
            </el-button>
          </template>

          <!-- 表格区域 -->
          <normal-table :data="dataList" :columns="columns" :total="total" :query="searchQuery"
            :list-loading="loadingTable" @change="changePage">
            <template #preColumns>
              <el-table-column label="序号" width="65" align="center">
                <template #default="scope">
                  {{ (searchQuery.offset! - 1) * searchQuery.limit! + scope.$index + 1 }}
                </template>
              </el-table-column>
            </template>
          </normal-table>
        </table-container>
      </el-col>
      <el-col :span="14"></el-col>
    </el-row>
  </app-container>
</template>
