<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" v-show="showSearch">
      <el-form-item label="项目名称">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable style="width:200px" @keyup.enter="getList" />
      </el-form-item>
      <el-form-item label="建设阶段">
        <el-input v-model="queryParams.buildStage" placeholder="请输入建设阶段" clearable style="width:200px" @keyup.enter="getList" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getList">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="projectList" border>
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="项目名称" prop="sProjectName" :show-overflow-tooltip="true" min-width="180" />
      <el-table-column label="建设单位" prop="sDept" :show-overflow-tooltip="true" width="160" />
      <el-table-column label="法人代表" prop="projectLegalPerson" width="120" />
      <el-table-column label="联系人" prop="sLinkman" width="100" />
      <el-table-column label="联系电话" prop="sPhone" width="130" />
      <el-table-column label="建设阶段" prop="sPBuildStage" width="110" />
      <el-table-column label="申请日期" prop="sPgoaDate" width="120" />
      <el-table-column label="项目地点" prop="sLocation" :show-overflow-tooltip="true" width="160" />
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="pageNum" v-model:limit="pageSize" @pagination="getList" />

    <!-- 编辑对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="700px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="项目名称" prop="sProjectName"><el-input v-model="form.sProjectName" placeholder="请输入项目名称" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="建设单位" prop="sDept"><el-input v-model="form.sDept" placeholder="请输入建设单位" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="法人代表" prop="projectLegalPerson"><el-input v-model="form.projectLegalPerson" placeholder="请输入法人代表" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="联系人" prop="sLinkman"><el-input v-model="form.sLinkman" placeholder="请输入联系人" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="联系电话" prop="sPhone"><el-input v-model="form.sPhone" placeholder="请输入联系电话" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="建设阶段" prop="sPBuildStage"><el-input v-model="form.sPBuildStage" placeholder="请输入建设阶段" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="项目性质" prop="sType"><el-input v-model="form.sType" placeholder="请输入项目性质" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="申请日期" prop="sPgoaDate"><el-date-picker v-model="form.sPgoaDate" type="date" placeholder="选择申请日期" style="width: 100%" /></el-form-item></el-col>
          <el-col :span="24"><el-form-item label="项目地点" prop="sLocation"><el-input v-model="form.sLocation" placeholder="请输入项目地点" /></el-form-item></el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { listProject, addProject, updateProject, delProject } from '@/api/hzz/riverproject'
import { ElMessage, ElMessageBox } from 'element-plus'

const showSearch = ref(true)
const loading = ref(false)
const total = ref(0)
const pageNum = ref(1)
const pageSize = ref(10)
const projectList = ref([])
const queryParams = ref({ projectName: '', buildStage: '' })

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formRef = ref(null)
const form = reactive({})
const rules = { sProjectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }] }

const getList = async () => {
  loading.value = true
  try {
    const res = await listProject({ ...queryParams.value, pageNum: pageNum.value, pageSize: pageSize.value })
    projectList.value = res.rows || []
    total.value = res.total || 0
  } finally { loading.value = false }
}

const resetQuery = () => { queryParams.value = { projectName: '', buildStage: '' }; getList() }

const handleAdd = () => { dialogTitle.value = '新增涉河项目'; Object.keys(form).forEach(k => delete form[k]); dialogVisible.value = true }
const handleEdit = (row) => { dialogTitle.value = '修改涉河项目'; Object.assign(form, row); dialogVisible.value = true }
const handleDetail = (row) => { dialogTitle.value = '项目详情'; Object.assign(form, row); dialogVisible.value = true }
const handleDelete = async (row) => {
  await ElMessageBox.confirm('确认删除该项目？', '提示', { type: 'warning' })
  await delProject(row.GUID)
  ElMessage.success('删除成功')
  getList()
}

const submitForm = async () => {
  if (!formRef.value) return
  await formRef.value.validate()
  if (form.GUID) { await updateProject(form) } else { await addProject(form) }
  ElMessage.success('操作成功')
  dialogVisible.value = false
  getList()
}

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