<template>
  <div class="gc-page">
    <div class="gc-toolbar">
      <el-button type="success" icon="DataAnalysis" @click="handleStats">统计</el-button>
      <div class="gc-toolbar-right">
        <el-input v-model="queryParams.pustName" placeholder="请输入名称" clearable style="width: 160px" @keyup.enter="handleQuery" />
        <el-input v-model="queryParams.pustLoc" placeholder="请输入位置" clearable style="width: 160px" @keyup.enter="handleQuery" />
        <el-select v-model="queryParams.pustType" placeholder="请选择类别-" clearable style="width: 140px">
          <el-option label="排水泵站" value="1" />
          <el-option label="供水泵站" value="2" />
          <el-option label="供排结合泵站" value="3" />
        </el-select>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </div>
    </div>

    <el-table v-loading="loading" :data="list" stripe border>
      <el-table-column prop="pustName" label="泵站名称" min-width="180" />
      <el-table-column prop="pustLoc" label="泵站所在位置" min-width="220" />
      <el-table-column prop="insFlow" label="装机流量（m³/s）" width="150" />
      <el-table-column label="操作" width="100" fixed="right" align="center">
        <template #default="s">
          <el-button type="primary" link icon="View" @click="handleDetail(s.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="gc-pagination">
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- 查看详情弹窗 -->
    <el-dialog :title="form.pustName" v-model="open" width="720px" append-to-body>
      <el-form :model="form" label-width="130px" :disabled="true">
        <el-row>
          <el-col :span="12"><el-form-item label="泵站名称">{{ form.pustName }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="泵站代码">{{ form.pustCode }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="泵站经度">{{ form.pustLon }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="泵站纬度">{{ form.pustLat }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="泵站所在位置">{{ form.pustLoc }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="泵站类型">{{ pustTypeMap[form.pustType] }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="装机流量">{{ form.insFlow }} m³/s</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="装机功率">{{ form.insPow }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="水泵数量">{{ form.pumpNum }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="设计扬程">{{ form.designHead }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="工程等别">{{ engGradMap[form.engGrad] }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="工程规模">{{ form.engScale }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="工程建设情况">{{ form.engBuild }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="开工时间">{{ form.startDate }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="建成时间">{{ form.endDate }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="归口管理部门">{{ form.admDep }}</el-form-item></el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 统计弹窗 -->
    <el-dialog title="统计类别" v-model="statsVisible" width="500px" append-to-body>
      <el-table :data="statsData" stripe v-loading="statsLoading">
        <el-table-column prop="name" label="类别" min-width="120" />
        <el-table-column prop="value" label="数量" width="120" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup name="Pust">
import { getPustList } from '@/api/slgc/gc/index'
import { getPust, getPustCountType } from '@/api/slgc/pust/index'

const loading = ref(true)
const total = ref(0)
const open = ref(false)
const list = ref([])
const queryParams = reactive({ pageNum: 1, pageSize: 10, pustName: undefined, pustLoc: undefined, pustType: undefined })
const form = ref({})
const statsVisible = ref(false)
const statsLoading = ref(false)
const statsData = ref([])
const pustTypeMap = { '1': '排水泵站', '2': '供水泵站', '3': '供排结合泵站' }
const engGradMap = { '1': 'Ⅰ等', '2': 'Ⅱ等', '3': 'Ⅲ等', '4': 'Ⅳ等', '5': 'Ⅴ等' }

function getList() {
  loading.value = true
  getPustList(queryParams).then(res => {
    list.value = res.rows; total.value = res.total; loading.value = false
  })
}
function handleQuery() { queryParams.pageNum = 1; getList() }
function handleDetail(row) {
  getPust(row.pustCode).then(res => { form.value = res.data; open.value = true })
}
function handleStats() {
  statsVisible.value = true
  statsLoading.value = true
  getPustCountType().then(res => {
    statsData.value = res.data || []
    statsLoading.value = false
  }).catch(() => { statsLoading.value = false })
}

onMounted(() => { getList() })
</script>

<style scoped>
.gc-page {
  padding: 16px;
  background: #fff;
  min-height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
}
.gc-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.gc-toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.gc-pagination {
  margin-top: auto;
  padding-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>
