<template>
  <div class="app-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>堤防工程管理</span>
          <el-button type="primary" size="small" style="float:right" @click="handleAdd">新增堤防</el-button>
        </div>
      </template>

      <el-form :model="queryParams" :inline="true" size="small">
        <el-form-item label="堤防名称">
          <el-input v-model="queryParams.dikeName" placeholder="请输入堤防名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="起始位置">
          <el-input v-model="queryParams.startLoc" placeholder="请输入起始位置" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button type="info" icon="DataAnalysis" @click="handleStats">统计</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="dikeList" stripe>
        <el-table-column prop="dikeCode" label="编码" width="100" />
        <el-table-column prop="dikeName" label="堤防名称" min-width="160" />
        <el-table-column prop="startLoc" label="起始位置" width="140" />
        <el-table-column prop="dikeLen" label="长度(km)" width="100" />
        <el-table-column prop="dikeGrad" label="等级" width="80">
          <template #default="scope">
            {{ gradeMap[scope.row.dikeGrad] || scope.row.dikeGrad }}
          </template>
        </el-table-column>
        <el-table-column prop="dikeType" label="类型" width="100" />
        <el-table-column prop="engStat" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.engStat === '1' ? 'success' : 'warning'" size="small">
              {{ scope.row.engStat === '1' ? '正常' : scope.row.engStat === '0' ? '停用' : scope.row.engStat }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="updDate" label="更新日期" width="120" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button type="primary" link icon="View" @click="handleDetail(scope.row)">查看</el-button>
            <el-button type="primary" link icon="Edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" link icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <el-dialog :title="title" v-model="open" width="720px" append-to-body>
      <el-form :model="form" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="堤防编码">
              <el-input v-model="form.dikeCode" :disabled="title === '编辑堤防'" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="堤防名称">
              <el-input v-model="form.dikeName" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="起始位置">
              <el-input v-model="form.startLoc" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="终点位置">
              <el-input v-model="form.endLoc" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="长度(km)">
              <el-input-number v-model="form.dikeLen" :min="0" :step="0.1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级">
              <el-select v-model="form.dikeGrad" placeholder="请选择等级">
                <el-option label="一级" value="1" />
                <el-option label="二级" value="2" />
                <el-option label="三级" value="3" />
                <el-option label="四级" value="4" />
                <el-option label="五级" value="5" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工程状态">
              <el-select v-model="form.engStat" placeholder="请选择状态">
                <el-option label="正常" value="1" />
                <el-option label="停用" value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="堤防类型">
              <el-input v-model="form.dikeType" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>

    <el-dialog v-model="statsVisible" title="堤防类型分布统计" width="600px" 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="100" />
      </el-table>
    </el-dialog>
  </div>
</template>

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

const { proxy } = getCurrentInstance()
const loading = ref(true)
const total = ref(0)
const open = ref(false)
const title = ref('')

const dikeList = ref([])
const queryParams = reactive({
  pageNum: 1, pageSize: 10,
  dikeName: undefined, startLoc: undefined
})
const form = ref({})
const statsVisible = ref(false)
const statsLoading = ref(false)
const statsData = ref([])

const gradeMap = { '1': '一级', '2': '二级', '3': '三级', '4': '四级', '5': '五级' }

function getList() {
  loading.value = true
  getDikeList(queryParams).then(res => {
    dikeList.value = res.rows
    total.value = res.total
    loading.value = false
  })
}

function handleQuery() { queryParams.pageNum = 1; getList() }
function resetQuery() {
  queryParams.dikeName = undefined
  queryParams.startLoc = undefined
  handleQuery()
}

function handleDetail(row) {
  getDike(row.dikeCode).then(res => {
    form.value = res.data
    title.value = '堤防详情'
    open.value = true
  })
}

function handleEdit(row) {
  getDike(row.dikeCode).then(res => {
    form.value = res.data
    title.value = '编辑堤防'
    open.value = true
  })
}

function submitForm() {
  const method = form.value.dikeCode && title.value === '编辑堤防' ? updateDike : addDike
  method(form.value).then(() => {
    proxy.$modal.msgSuccess('操作成功')
    open.value = false
    getList()
  })
}

function cancel() { open.value = false }

function handleAdd() {
  form.value = { engStat: '1' }
  title.value = '新增堤防'
  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 })
}

function handleDelete(row) {
  proxy.$modal.confirm('确认删除堤防"' + row.dikeName + '"？').then(() => {
    delDike(row.dikeCode).then(() => {
      proxy.$modal.msgSuccess('删除成功')
      getList()
    })
  })
}

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

<style scoped>
.card-header { font-weight: 600; font-size: 16px; }
</style>
