<!--
  Description: 配电网母线
  Author: 李亚光
  Date: 2023-04-21
 -->
<script lang="ts" setup name="networkBusbar">
import { ElMessage, ElMessageBox } from 'element-plus'
import editList from './busbarAdd.vue'
import { electricityNodeDelete, electricityNodePage } from '@/api/api/index'
const props = defineProps({
  bak: {
    type: Function,
    default: () => { },
  },
  id: {
    type: String,
    required: true,
  },
})
const bak = () => {
  props.bak(1)
}
// 查询参数
const searchQuery = reactive({
  offset: 1,
  limit: 20,
})
const loadingTable = ref(true)
const columns = ref([
  { text: '节点名称', value: 'nodeName', align: 'center' },
  // { text: '节点编号', value: 'id', align: 'center' },
  { text: '节点类型', value: 'typeName', align: 'center' },
  // { text: '节点并联电导', value: 'gs', align: 'center' },
  // { text: '节点并联电纳', value: 'bs', align: 'center' },
  // { text: '母线断面号', value: 'area', align: 'center' },
  // { text: '节点电压初始幅值', value: 'vm', align: 'center' },
  // { text: '节点电压初始相位角', value: 'va', align: 'center' },
  // { text: '母线省损耗区域', value: 'zone', align: 'center' },
  // { text: '节点最大电压', value: 'vmax', align: 'center' },
  // { text: '节点最小电压', value: 'vmin', align: 'center' },
  { text: '负荷有功功率(MW)', value: 'pd', align: 'center' },
  { text: '负荷无功功率(MW)', value: 'qd', align: 'center' },
  { text: '节点碳势(gCO2/kWh)', value: 'en', align: 'center' },
  { text: '负荷碳流率(kgCO2/h)', value: 'flowRate', align: 'center' },
  { text: '采集时间', value: 'colTime', align: 'center' },
]) // 表格
const total = ref<number>(0)
const list = ref<any[]>([])
// 获取数据列表
const fetchDataList = () => {
  loadingTable.value = true
  electricityNodePage({ ...searchQuery, electricityId: props.id }).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
    loadingTable.value = false
  })
}
// 搜索
const search = () => {
  fetchDataList()
}
const editRef = ref()
// 新增
const add = () => {
  editRef.value.initDialog('create', {}, props.id)
}
// 编辑
const edit = (row: any) => {
  editRef.value.initDialog('update', row, '')
}
// 详情
const detail = (row: any) => {
  editRef.value.initDialog('detail', row, '')
}
// 删除
const del = (row: any) => {
  ElMessageBox.confirm(
    '确认删除选中的数据吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      electricityNodeDelete(row.id).then((res) => {
        ElMessage.success('操作成功')
        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()
})
</script>

<template>
  <div>
    <!-- 添加或编辑 -->
    <edit-list :id="id" ref="editRef" @refresh="search" />
    <table-container>
      <template #btns-right>
        <el-button type="primary" @click="bak">
          返回
        </el-button>
        <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="detail(scope.row)">
                查看
              </el-button>
              <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>
