<!-- 配电网 -->
<script lang="ts" setup name="network">
import { ElMessage, ElMessageBox } from 'element-plus'
import editList from './networkAdd.vue'
import detailDialog from './detailDialog.vue'
import networkBranch from './components/networkBranch.vue'
import networkBusbar from './components/networkBusbar.vue'
import { deleteElectricityList, getElectricityList } from '@/api/api/index'
// 控制表格切换
// 1:配电网 2：配电网母线 3：配电网支线
const showTable = ref(1)
// 切换显示
const changeShow = (index: number) => {
  showTable.value = index
}
// 查询参数
const searchQuery = reactive({
  offset: 1,
  limit: 20,
  electricityName: '', // 名称
  startTime: '',
  endTime: '',
})
// 查询时间段范围
const TimeRanges = ref()
// 监听时间段范围
watch(() => TimeRanges.value, (newVal: string[]) => {
  if (newVal && newVal.length) {
    searchQuery.startTime = newVal[0]
    searchQuery.endTime = newVal[1]
  }
}, { deep: true })
// table表格loading
const loadingTable = ref(true)
// table列表
const columns = ref([
  { text: '配电网名称', value: 'electricityName', align: 'center' },
  // { text: '配电网区域', value: 'electricityArea', align: 'center' },
  // { text: '上传人', value: 'uploaderName', align: 'center' },
  // { text: '上传时间', value: 'uploadTime', align: 'center' },
  { text: '基准功率(MVA)', value: 'basePower', align: 'center' },
  { text: '系统节点数', value: 'nodeNum', align: 'center' },
  { text: '发电机数', value: 'genNum', align: 'center' },
  { text: '系统负荷数', value: 'loadNum', align: 'center' },
  { text: '节点之间支路数', value: 'branchNum', align: 'center' },
])
const total = ref<number>(0)
// table列表数据
const list = ref<any[]>([])
// 获取数据列表
const fetchDataList = () => {
  loadingTable.value = true
  getElectricityList(searchQuery).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')
}
// 编辑
const edit = (row: any) => {
  editRef.value.initDialog('update', row)
}
// 删除
const del = (row: any) => {
  ElMessageBox.confirm(
    '确认删除选中的数据吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      deleteElectricityList(row.id).then((res) => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        search()
      })
    })
    .catch(() => {
    })
}
// 重置
const reset = () => {
  searchQuery.electricityName = ''
  searchQuery.offset = 1
  searchQuery.limit = 20
  searchQuery.startTime = ''
  searchQuery.endTime = ''
  search()
}
const detailRef = ref()
// 查看详情
const detail = (row: any) => {
  // if (row.electricityDetail) {
  //   detailRef.value.initDialog(row)
  // }
  // else {
  //   ElMessage({
  //     type: 'info',
  //     message: '暂无内容',
  //   })
  // }
  editRef.value.initDialog('detail', row)
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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()
})
// 母线参数
const networdId = ref('')
const bushbar = (row: any) => {
  showTable.value = 2
  networdId.value = row.id
}
// 之路参数
const branch = (row: any) => {
  showTable.value = 3
  networdId.value = row.id
}
</script>

<template>
  <div>
    <!-- 配电网分支参数 -->
    <network-branch v-if="showTable === 3" :id="networdId" ref="branchRef" :bak="changeShow" />
    <!-- 配电网母线参数 -->
    <network-busbar v-if="showTable === 2" :id="networdId" ref="busbarRef" :bak="changeShow" />
    <!-- 配电网 -->
    <div v-if="showTable === 1">
      <edit-list ref="editRef" @refresh="search" />
      <detail-dialog ref="detailRef" />
      <!-- 筛选条件 -->
      <search-area :need-clear="true" @search="search" @clear="reset">
        <search-item>
          <el-input v-model="searchQuery.electricityName" 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" @multiSelect="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="母线参数" align="center" width="120">
              <template #default="{ row }">
                <el-button size="small" type="primary" link @click="bushbar(row)">
                  母线参数
                </el-button>
              </template>
            </el-table-column>
            <el-table-column label="支线参数" align="center" width="120">
              <template #default="{ row }">
                <el-button size="small" type="primary" link @click="branch(row)">
                  支线参数
                </el-button>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="120">
              <template #default="{ row }">
                <el-button size="small" type="primary" link @click="detail(row)">
                  查看
                </el-button>
                <el-button type="primary" link size="small" @click="edit(row)">
                  修改
                </el-button>
                <el-button type="primary" link size="small" @click="del(row)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </div>
  </div>
</template>

