<!--
  Description: 设备监测
  Author: 李亚光
  Date: 2023-04-21
 -->
<script lang="ts" setup name="devicemonitor">
import { ElMessage, ElMessageBox } from 'element-plus'
import editList from './components/deviceAdd.vue'
// 查询参数
const searchQuery = reactive({
  offset: 1,
  limit: 20,
  deviceNo: '',
  startTime: '',
  endTime: '',
})
// 查询时间段范围
const TimeRanges = ref()
const loadingTable = ref(true)
const columns = ref([
  // { text: '设备类型', value: 'deviceTypeName', align: 'center' },
  // { text: '设备编号', value: 'deviceNo', align: 'center' },
  // { text: '安装位置', value: 'installLocaltion', align: 'center' },
  // { text: '电量/V', value: 'electricalVoltage', align: 'center' },
  // { text: '功率/W', value: 'power', align: 'center' },
  // { text: '上传时间', value: 'uploadTime', align: 'center' },
  { text: '设备编号', value: '1', align: 'center' },
  { text: '安装位置', value: '2', align: 'center' },
  { text: '负荷有功功率', value: '3', align: 'center' },
  { text: '负荷无功功率', value: '4', align: 'center' },
  { text: '上报时间', value: '5', align: 'center' },
]) // 表格
const total = ref<number>(0)
const list = ref<any[]>([])
// 获取数据列表
const fetchDataList = () => {
  // loadingTable.value = true
  // getMonitorStation(searchQuery).then((res) => {
  //   list.value = res.data.rows
  //   total.value = res.data.total
  //   loadingTable.value = false
  // })
  list.value = [
    {
      1: 11103,
      2: '#12号楼',
      3: 1000,
      4: 1100,
      5: '2023-04-21 10:00:00',
    },
    {
      1: 11104,
      2: '#75号楼',
      3: 900,
      4: 900,
      5: '2023-04-21 11:00:00',
    },
    {
      1: 11105,
      2: '#79号楼',
      3: 900,
      4: 900,
      5: '2023-04-21 11:00:00',
    },
  ]
  total.value = list.value.length
  loadingTable.value = false
}
// 搜索
const search = () => {
  fetchDataList()
}
// 重置搜索条件
const reset = () => {
  searchQuery.deviceNo = ''
  searchQuery.startTime = ''
  searchQuery.endTime = ''
  search()
}
const editRef = ref()
// 新增
const add = () => {
  editRef.value.initDialog('add', { })
}
// 编辑
const edit = (row: any) => {
  editRef.value.initDialog('update', { })
}
// 删除
const del = (row: any) => {
  ElMessageBox.confirm(
    '确认删除选中的数据吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
    })
    .catch(() => {
    })
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.limit = val.size
  }
  if (val && val.page) {
    searchQuery.offset = val.page
  }
  search()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
onMounted(() => {
  search()
})
</script>

<template>
  <div>
    <!-- 添加或编辑 -->
    <edit-list ref="editRef" @refresh="search" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.deviceNo" placeholder="设备编号" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="TimeRanges"
          type="datetimerange"
          format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
          range-separator="至"
          start-placeholder="上传开始时间"
          end-placeholder="上传结束时间"
          clearable
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button icon="icon-add" title="新增" @click="add" />
      </template>
      <!-- 表格区域 -->
      <normal-table
        :data="list" :total="total" :columns="columns as any"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }" :list-loading="loadingTable"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button type="primary" link size="small" @click="edit(scope.row)">
                修改
              </el-button>
              <el-button type="primary" link size="small" @click="del(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </div>
</template>
