<!--
  Description: 配电网支路
  Author: 李亚光
  Date: 2023-04-21
 -->
<script lang="ts" setup name="networkBranch">
import { ElMessage, ElMessageBox } from 'element-plus'
import editList from './branchAdd.vue'
import { electricityBranchPage } from '@/api/api/index'
const props = defineProps({
  bak: {
    type: Function,
    required: true,
  },
  id: {
    type: String,
    required: true,
  },
})
const bak = () => {
  props.bak(1)
}
// 查询参数
const searchQuery = reactive({
  offset: 1,
  limit: 20,
  electricityId: '',
})
// 查询时间段范围
const loadingTable = ref(true)
const columns = ref([
  { text: '支路首端节点', value: 'fbusId', align: 'center' },
  { text: '支路末端节点', value: 'tbusId', align: 'center' },
  { text: '支路电阻标幺值', value: 'r', align: 'center' },
  // { text: '支路电纳标幺值', value: 'b', align: 'center' },
  // { text: '长距离输电容量', value: 'ratea', align: 'center' },
  // { text: '短距离输电容量', value: 'rateb', align: 'center' },
  // { text: '紧急距离输电容量', value: 'ratec', align: 'center' },
  // { text: '支路变比', value: 'ratio', align: 'center' },
  // { text: '角度', value: 'angle', align: 'center' },
  { text: '支路是否运行', value: 'status2Name', align: 'center' },
  // { text: '支路最小相位角', value: 'angmin', align: 'center' },
  { text: '支路碳流率(kgCO2/h)', value: 'flowRate', align: 'center' },
]) // 表格
const total = ref<number>(0)
const list = ref<any[]>([])
// 获取数据列表
const fetchDataList = () => {
  loadingTable.value = true
  searchQuery.electricityId = props.id
  electricityBranchPage(searchQuery).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
    loadingTable.value = false
  })
}
fetchDataList()
// 搜索
const search = () => {
  fetchDataList()
}
// 重置搜索条件
const editRef = ref()
// 新增
const add = () => {
  editRef.value.initDialog('create', {}, props.id)
}
// 编辑
const edit = (row: any) => {
  editRef.value.initDialog('update', row, props.id)
}
// 详情
const detail = (row: any) => {
  editRef.value.initDialog('detail', row, props.id)
}
// 删除
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
}
</script>

<template>
  <div>
    <!-- 添加或编辑 -->
    <edit-list :id="props.id" 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>
        <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>
