<!--
  Description: 甲烷数据
  Author: 李亚光
  Date: 2024-11-07
 -->
<script lang="ts" setup name="GasDataPage">
import { ElLoading, ElMessage } from 'element-plus'
import { exportFile } from '@/utils/exportUtils'
import { exportGasList, getGasList } from '@/api/page/gas'
// 获取页面高度
const pageHeight = ref(window.innerHeight - 50 - 60 - 20 - 20 - 10 - 30 - 15 - 15 - 20)
window.addEventListener('resize', () => {
  pageHeight.value = window.innerHeight - 50 - 60 - 20 - 20 - 10 - 30 - 15 - 15 - 20
})
const loadingTable = ref(true)
const total = ref(0)
const list = ref<any[]>([])
const columns = ref([
  { text: '设备名称', value: 'device_name', align: 'center' },
  { text: '设备编号', value: 'device_code', align: 'center' },
  { text: '甲烷数据(%LEL)', value: 'gas_value', align: 'center' },
  { text: '采集时间', value: 'ts', align: 'center' },
])
//  查询条件
const listQuery = ref({
  limit: 20,
  offset: 1,
  start_time: '',
  end_time: '',
})
// 开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.value.start_time = ''
  listQuery.value.end_time = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.value.start_time = `${newVal[0]} 00:00:00`
      listQuery.value.end_time = `${newVal[1]} 23:59:59`
    }
  }
})
// 获取数据
const fetchData = () => {
  loadingTable.value = true
  getGasList(listQuery.value).then((res) => {
    console.log(res.data, '甲烷数据')
    list.value = res.data.items
    total.value = res.data.total
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
fetchData()
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.value = {
    limit: 20,
    offset: 1,
    start_time: '',
    end_time: '',
  }
  fetchData()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData()
}
// 导出
const exportList = () => {
  if (!list.value.length) {
    ElMessage.warning('暂无可导出数据')
    return
  }
  const loading = ElLoading.service({
    lock: true,
    background: 'rgba(255, 255, 255, 0.8)',
  })
  exportGasList({ ...listQuery.value, offset: undefined, limit: undefined }).then((res) => {
    exportFile(res.data, listQuery.value.start_time ? `${listQuery.value.start_time}~${listQuery.value.end_time} 甲烷数据列表.xlsx` : '甲烷数据列表.xlsx')
    loading.close()
  }).catch(() => {
    loading.close()
  })
}
</script>

<template>
  <!-- 布局 -->
  <app-container>
    <search-area :need-clear="true" @search="fetchData" @clear="reset">
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD" format="YYYY-MM-DD"
          range-separator="至" start-placeholder="采集开始时间" end-placeholder="采集结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <!-- 操作 -->
        <div>
          <el-button type="primary" @click="exportList">
            导出
          </el-button>
        </div>
      </template>
      <!-- 查询结果Table显示 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="loadingTable"
        :height="pageHeight" @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="90" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
