<template>
  <div class="gc-page">
    <div class="gc-toolbar">
      <div class="gc-toolbar-left">
        <el-button type="success" icon="DataAnalysis" @click="handleStats">统计</el-button>
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
        <el-button type="warning" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleBatchDelete">删除</el-button>
        <el-button type="info" icon="Upload" @click="importRef.open()">导入</el-button>
        <el-button type="success" icon="Download" @click="handleExport">导出</el-button>
      </div>
      <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%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <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="s">{{ gradeMap[s.row.dikeGrad] || s.row.dikeGrad }}</template>
      </el-table-column>
      <el-table-column prop="dikePatt" label="堤防型式" width="120">
        <template #default="s">{{ dikePattMap[s.row.dikePatt] || s.row.dikePatt }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right" align="center">
        <template #default="s">
          <el-button type="primary" link icon="View" @click="handleDetail(s.row)">查看</el-button>
          <el-button type="warning" link icon="Edit" @click="handleUpdate(s.row)">修改</el-button>
          <el-button type="danger" link icon="Delete" @click="handleDelete(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.dikeName" v-model="open" width="900px" append-to-body class="gc-detail-dialog">
      <el-descriptions :column="3" border>
        <el-descriptions-item label="堤防名称">{{ form.dikeName }}</el-descriptions-item>
        <el-descriptions-item label="堤防代码">{{ form.dikeCode }}</el-descriptions-item>
        <el-descriptions-item label="堤防级别">{{ gradeMap[form.dikeGrad] }}</el-descriptions-item>
        <el-descriptions-item label="起点所在位置" :span="2">{{ form.startLoc }}</el-descriptions-item>
        <el-descriptions-item label="终点所在位置">{{ form.endLoc }}</el-descriptions-item>
        <el-descriptions-item label="堤防类型">{{ form.dikeType }}</el-descriptions-item>
        <el-descriptions-item label="堤防型式">{{ dikePattMap[form.dikePatt] }}</el-descriptions-item>
        <el-descriptions-item label="堤防长度">{{ form.dikeLen }} m</el-descriptions-item>
        <el-descriptions-item label="堤防起点桩号">{{ form.dikeStartNum }}</el-descriptions-item>
        <el-descriptions-item label="堤防终点桩号">{{ form.dikeEndNum }}</el-descriptions-item>
        <el-descriptions-item label="起点堤顶高程">{{ form.startDikeTopEl }} m</el-descriptions-item>
        <el-descriptions-item label="终点堤顶高程">{{ form.endDikeTopEl }} m</el-descriptions-item>
        <el-descriptions-item label="堤防最小高度">{{ form.dikeHeigMin }} m</el-descriptions-item>
        <el-descriptions-item label="堤防最大高度">{{ form.dikeHeigMax }} m</el-descriptions-item>
        <el-descriptions-item label="堤顶最小宽度">{{ form.dikeTopWidMin }} m</el-descriptions-item>
        <el-descriptions-item label="堤顶最大宽度">{{ form.dikeTopWidMax }} m</el-descriptions-item>
        <el-descriptions-item label="工程任务">{{ form.engTask }}</el-descriptions-item>
        <el-descriptions-item label="工程建设情况">{{ engStatMap[form.engStat] }}</el-descriptions-item>
        <el-descriptions-item label="开工时间">{{ form.startDate }}</el-descriptions-item>
        <el-descriptions-item label="建成时间">{{ form.compDate }}</el-descriptions-item>
        <el-descriptions-item label="归口管理部门">{{ form.admDep }}</el-descriptions-item>
        <el-descriptions-item label="运行状况">{{ runStatMap[form.runStat] }}</el-descriptions-item>
        <el-descriptions-item label="属性采集时间">{{ form.collDate }}</el-descriptions-item>
        <el-descriptions-item label="属性更新时间">{{ form.updDate }}</el-descriptions-item>
        <el-descriptions-item label="起点经度">{{ form.startLong }}</el-descriptions-item>
        <el-descriptions-item label="起点纬度">{{ form.startLat }}</el-descriptions-item>
        <el-descriptions-item label="终点经度">{{ form.endLong }}</el-descriptions-item>
        <el-descriptions-item label="终点纬度">{{ form.endLat }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="3">{{ form.note }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="dialogTitle" v-model="addEditOpen" width="900px" append-to-body>
      <el-form ref="addEditFormRef" :model="addEditForm" :rules="addEditRules" label-width="130px" class="gc-edit-form">
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="堤防名称" prop="dikeName"><el-input v-model="addEditForm.dikeName" maxlength="50" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="堤防代码" prop="dikeCode"><el-input v-model="addEditForm.dikeCode" maxlength="50" :disabled="isEdit" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="堤防级别"><el-select v-model="addEditForm.dikeGrad" placeholder="请选择" style="width:100%"><el-option v-for="(v,k) in gradeMap" :key="k" :label="v" :value="k" /></el-select></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="起点所在位置"><el-input v-model="addEditForm.startLoc" maxlength="50" /></el-form-item></el-col>
          <el-col :span="16"><el-form-item label="终点所在位置"><el-input v-model="addEditForm.endLoc" maxlength="50" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="堤防类型"><el-select v-model="addEditForm.dikeType" placeholder="请选择" style="width:100%"><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-form-item></el-col>
          <el-col :span="8"><el-form-item label="堤防型式"><el-select v-model="addEditForm.dikePatt" placeholder="请选择" style="width:100%"><el-option v-for="(v,k) in dikePattMap" :key="k" :label="v" :value="k" /></el-select></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="堤防长度"><el-input-number v-model="addEditForm.dikeLen" :precision="2" :controls="false" style="width:100%" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="堤防起点桩号"><el-input v-model="addEditForm.dikeStartNum" maxlength="50" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="堤防终点桩号"><el-input v-model="addEditForm.dikeEndNum" maxlength="50" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="起点经度"><el-input-number v-model="addEditForm.startLong" :precision="4" :controls="false" style="width:100%" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="起点纬度"><el-input-number v-model="addEditForm.startLat" :precision="4" :controls="false" style="width:100%" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="终点经度"><el-input-number v-model="addEditForm.endLong" :precision="4" :controls="false" style="width:100%" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="终点纬度"><el-input-number v-model="addEditForm.endLat" :precision="4" :controls="false" style="width:100%" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="起点堤顶高程"><el-input-number v-model="addEditForm.startDikeTopEl" :precision="2" :controls="false" style="width:100%" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="终点堤顶高程"><el-input-number v-model="addEditForm.endDikeTopEl" :precision="2" :controls="false" style="width:100%" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="最小堤高"><el-input-number v-model="addEditForm.dikeHeigMin" :precision="2" :controls="false" style="width:100%" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="最大堤高"><el-input-number v-model="addEditForm.dikeHeigMax" :precision="2" :controls="false" style="width:100%" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="最小堤顶宽"><el-input-number v-model="addEditForm.dikeTopWidMin" :precision="2" :controls="false" style="width:100%" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="最大堤顶宽"><el-input-number v-model="addEditForm.dikeTopWidMax" :precision="2" :controls="false" style="width:100%" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="工程建设情况"><el-select v-model="addEditForm.engStat" placeholder="请选择" style="width:100%"><el-option v-for="(v,k) in engStatMap" :key="k" :label="v" :value="k" /></el-select></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="开工时间"><el-date-picker v-model="addEditForm.startDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择" style="width:100%" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="建成时间"><el-date-picker v-model="addEditForm.compDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择" style="width:100%" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8"><el-form-item label="归口管理部门"><el-input v-model="addEditForm.admDep" maxlength="50" /></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="运行状况"><el-select v-model="addEditForm.runStat" placeholder="请选择" style="width:100%"><el-option v-for="(v,k) in runStatMap" :key="k" :label="v" :value="k" /></el-select></el-form-item></el-col>
          <el-col :span="8"><el-form-item label="工程任务"><el-input v-model="addEditForm.engTask" maxlength="50" /></el-form-item></el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="24"><el-form-item label="备注"><el-input v-model="addEditForm.note" maxlength="200" type="textarea" :rows="2" /></el-form-item></el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="addEditOpen = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </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"><template #default="s">{{ s.row.NAME || s.row.name }}</template></el-table-column>
        <el-table-column prop="value" label="数量" width="120"><template #default="s">{{ s.row.VALUE ?? s.row.value }}</template></el-table-column>
      </el-table>
    </el-dialog>

    <excel-import-dialog ref="importRef" title="堤防信息导入" action="/slgc/dike/importData" template-action="/slgc/dike/importTemplate" template-file-name="dike_template" @success="getList" />
  </div>
</template>

<script setup name="Dike">
import { getDikeList } from '@/api/slgc/gc/index'
import { getDike, getDikeCountType, addDike, updateDike, delDike, exportDike } from '@/api/slgc/dike/index'
import ExcelImportDialog from '@/components/ExcelImportDialog/index.vue'

const { proxy } = getCurrentInstance()
const loading = ref(true)
const total = ref(0)
const open = ref(false)
const addEditOpen = ref(false)
const list = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const queryParams = reactive({ pageNum: 1, pageSize: 10, dikeName: undefined, startLoc: undefined, dikePatt: undefined })
const form = ref({})
const addEditForm = ref({})
const addEditFormRef = ref(null)
const importRef = ref(null)
const isEdit = ref(false)
const dialogTitle = 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 engStatMap = { '1': '已建', '2': '在建', '3': '规划' }
const runStatMap = { '1': '在用良好', '2': '在用故障', '3': '停用' }
const addEditRules = {
  dikeName: [{ required: true, message: '请输入堤防名称', trigger: 'blur' }],
  dikeCode: [{ required: true, message: '请输入堤防代码', trigger: 'blur' }]
}

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 handleSelectionChange(selection) {
  ids.value = selection.map(item => item.dikeCode)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
function handleDetail(row) { getDike(row.dikeCode).then(res => { form.value = res.data; open.value = true }) }
function handleAdd() { isEdit.value = false; dialogTitle.value = '新增堤防信息'; addEditForm.value = {}; addEditOpen.value = true }
function handleUpdate(row) {
  isEdit.value = true; dialogTitle.value = '修改堤防信息'
  getDike(row.dikeCode || ids.value[0]).then(res => { addEditForm.value = res.data; addEditOpen.value = true })
}
function submitForm() {
  addEditFormRef.value.validate(valid => {
    if (!valid) return
    const fn = isEdit.value ? updateDike : addDike
    fn(addEditForm.value).then(() => { proxy.$modal.msgSuccess(isEdit.value ? '修改成功' : '新增成功'); addEditOpen.value = false; getList() })
  })
}
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除该条堤防数据？').then(() => {
    return delDike(row.dikeCode)
  }).then(() => { getList(); proxy.$modal.msgSuccess('删除成功') }).catch(() => {})
}
function handleBatchDelete() {
  if (!ids.value.length) return
  proxy.$modal.confirm('是否确认删除选中的 ' + ids.value.length + ' 条堤防数据？').then(() => {
    return delDike(ids.value.join(','))
  }).then(() => { getList(); proxy.$modal.msgSuccess('删除成功') }).catch(() => {})
}
function handleExport() {
  exportDike(queryParams).then(res => {
    const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = '堤防信息数据.xlsx'
    link.click()
    window.URL.revokeObjectURL(url)
  })
}
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; flex-wrap: wrap; gap: 8px; }
.gc-toolbar-left, .gc-toolbar-right { display: flex; align-items: center; gap: 8px; }
.gc-pagination { margin-top: auto; padding-top: 16px; display: flex; justify-content: flex-end; }
:deep(.gc-detail-dialog .el-descriptions__label) { font-weight: 500; color: #303133; background-color: #f5f7fa; }
:deep(.gc-detail-dialog .el-descriptions__content) { color: #606266; }
.gc-edit-form :deep(.el-form-item) { margin-bottom: 18px; }
</style>
