<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.dikeName" placeholder="请输入名称" clearable style="width: 160px" @keyup.enter="handleQuery" />
        <el-input v-model="queryParams.startLoc" placeholder="请输入地址(起点终点都可)" clearable style="width: 200px" @keyup.enter="handleQuery" />
        <el-select v-model="queryParams.dikePatt" placeholder="请选择类别-" clearable style="width: 140px">
          <el-option label="土堤" value="1" />
          <el-option label="砌石堤" value="2" />
          <el-option label="土石混合堤" value="3" />
          <el-option label="钢筋混凝土防洪墙" value="4" />
          <el-option label="其他" value="9" />
        </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="dikeName" label="堤防名称" min-width="180" />
      <el-table-column prop="startLoc" label="起始所在位置" min-width="200" />
      <el-table-column prop="endLoc" label="终点所在位置" min-width="200" />
      <el-table-column prop="dikeType" label="堤防类型" width="120" />
      <el-table-column prop="dikeGrad" label="堤防级别" width="100">
        <template #default="scope">{{ gradeMap[scope.row.dikeGrad] || scope.row.dikeGrad }}</template>
      </el-table-column>
      <el-table-column prop="dikePatt" label="堤防型式" width="120">
        <template #default="scope">{{ dikePattMap[scope.row.dikePatt] || scope.row.dikePatt }}</template>
      </el-table-column>
      <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.dikeName" v-model="open" width="720px" append-to-body>
      <el-form :model="form" label-width="110px" :disabled="true">
        <el-row>
          <el-col :span="12"><el-form-item label="堤防编码">{{ form.dikeCode }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="堤防名称">{{ form.dikeName }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="起始位置">{{ form.startLoc }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="终点位置">{{ form.endLoc }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="长度(km)">{{ form.dikeLen }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="等级">{{ gradeMap[form.dikeGrad] }}</el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="堤防型式">{{ dikePattMap[form.dikePatt] }}</el-form-item></el-col>
          <el-col :span="12"><el-form-item label="堤防类型">{{ form.dikeType }}</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-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="Dike">
import { getDikeList } from '@/api/slgc/gc/index'
import { getDike, getDikeCountType } from '@/api/slgc/dike/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, dikeName: undefined, startLoc: undefined, dikePatt: undefined })
const form = ref({})
const statsVisible = ref(false)
const statsLoading = ref(false)
const statsData = ref([])
const gradeMap = { '1': '一级', '2': '二级', '3': '三级', '4': '四级', '5': '五级' }
const dikePattMap = { '1': '土堤', '2': '砌石堤', '3': '土石混合堤', '4': '钢筋混凝土防洪墙', '9': '其他' }
const runStatMap = { '1': '在用良好', '2': '在用故障', '3': '停用' }

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