<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.resName" placeholder="水库名称" clearable style="width: 160px" @keyup.enter="handleQuery" />
        <el-input v-model="queryParams.resLoc" placeholder="水库所在位置" clearable style="width: 160px" @keyup.enter="handleQuery" />
        <el-select v-model="queryParams.resType" 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 style="width: 100%">
      <el-table-column prop="resName" label="水库名称" min-width="160" />
      <el-table-column prop="resLoc" label="水库所在位置" min-width="200" />
      <el-table-column prop="resType" label="水库类型" width="120">
        <template #default="scope">{{ resTypeMap[scope.row.resType] || scope.row.resType }}</template>
      </el-table-column>
      <el-table-column prop="engGrad" label="工程等别" width="100">
        <template #default="scope">{{ engGradMap[scope.row.engGrad] || scope.row.engGrad }}</template>
      </el-table-column>
      <el-table-column prop="totCap" label="总库容（10⁴m³）" width="140" />
      <el-table-column label="操作" width="100" fixed="right" align="center">
        <template #default="scope">
          <el-button type="primary" link icon="View" @click="handleDetail(scope.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.resName" 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.resCode }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="水库名称">{{ form.resName }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="位置">{{ form.resLoc }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="类型">{{ resTypeMap[form.resType] }}</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="总库容（万m³）">{{ form.totCap }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="正常库容（万m³）">{{ form.normPoolStagCap }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="防洪库容（万m³）">{{ form.flcoCap }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="调洪库容（万m³）">{{ form.storFlCap }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="集水面积（km²）">{{ form.watShedArea }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="运行状况">{{ runStatMap[form.runStat] }}</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="Reservoir">
import { getReservoirList } from '@/api/slgc/gc/index'
import { getReservoir, getReservoirCountType } from '@/api/slgc/reservoir/index'

const { proxy } = getCurrentInstance()
const loading = ref(true)
const total = ref(0)
const open = ref(false)
const list = ref([])
const queryParams = reactive({ pageNum: 1, pageSize: 10, resName: undefined, resLoc: undefined, resType: undefined })
const form = ref({})
const statsVisible = ref(false)
const statsLoading = ref(false)
const statsData = ref([])
const resTypeMap = { '1': '山丘水库', '2': '平原水库', '3': '地下水库' }
const engGradMap = { '1': 'Ⅰ等', '2': 'Ⅱ等', '3': 'Ⅲ等', '4': 'Ⅳ等', '5': 'Ⅴ等' }
const runStatMap = { '1': '在用良好', '2': '在用故障', '3': '停用' }

function getList() {
  loading.value = true
  getReservoirList(queryParams).then(res => {
    list.value = res.rows; total.value = res.total; loading.value = false
  })
}
function handleQuery() { queryParams.pageNum = 1; getList() }
function handleDetail(row) {
  getReservoir(row.resCode).then(res => { form.value = res.data; open.value = true })
}
function handleStats() {
  statsVisible.value = true
  statsLoading.value = true
  getReservoirCountType().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>
