<!-- 分布式/楼宇/储能 -->
<script lang="ts" setup name="network">
import { ElMessage, ElMessageBox } from 'element-plus'
import editList from './pageAdd.vue'
import { deleteMonitorStation, getMonitorStation } from '@/api/api/index'
const props = defineProps({
  // 根据type发送不同请求
  stationType: {
    type: String,
    default: '0',
  },
})
// 查询参数
const searchQuery = reactive({
  offset: 1,
  limit: 20,
  stationName: '',
  type: props.stationType,
})
// 查询时间段范围
const TimeRanges = ref()
// 监听时间段范围
watch(() => TimeRanges.value, (newVal: string[]) => {
  if (newVal && newVal.length) {
    searchQuery.startTime = newVal[0]
    searchQuery.endTime = newVal[1]
  }
}, { deep: true })
const loadingTable = ref(true)
const columns = ref([
  { text: '机组名称', value: 'stationName', align: 'center' },
  { text: '发电机组对应节点数', value: 'nodeId', align: 'center' },
  { text: '发电机有功(MW)', value: 'pg', align: 'center' },
  { text: '发电机无功(MW)', value: 'qg', align: 'center' },
  { text: '最大无功(MVAr)', value: 'qmax', align: 'center' },
  { text: '最小无功(MVAr)', value: 'qmin', align: 'center' },
  { text: '节点电压', value: 'vg', align: 'center' },
  { text: '发电机容量', value: 'mbase', align: 'center' },
  { text: '运行状态', value: 'sttusName', align: 'center' },
  { text: '最大有功(MW)', value: 'pmax', align: 'center' },
  { text: '最小有功(MW)', value: 'pmin', align: 'center' },
  { text: '碳排强度(gCO2·/kWh)', value: 'strength', 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
    console.log(res.data, '分布式光伏')
    loadingTable.value = false
  })
}
// 搜索
const search = () => {
  fetchDataList()
}
// 新增或编辑组件
const editRef = ref()
// 新增
const add = () => {
  editRef.value.initDialog('create', { stationType: props.stationType })
}
// 编辑
const edit = (row: any) => {
  editRef.value.initDialog('update', { ...row, stationType: props.stationType })
}
// 删除
const del = (row: any) => {
  console.log(row, 'row')
  ElMessageBox.confirm(
    '确认删除选中的数据吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      deleteMonitorStation(row.id).then((res) => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        search()
      })
    })
    .catch(() => {
    })
}
// 重置
const reset = () => {
  searchQuery.stationName = ''
  searchQuery.offset = 1
  searchQuery.limit = 20
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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()
})
watch(() => props.stationType, (newVal) => {
  searchQuery.type = newVal
  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.stationName" placeholder="机组名称" clearable class="w-50 m-2 normal-input" />
      </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>

