<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-date-picker v-model="queryParams.startDate" type="date" placeholder="选择开始时间" value-format="YYYY-MM-DD HH:mm:ss" style="width:200px" />
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker v-model="queryParams.endDate" type="date" placeholder="选择结束时间" value-format="YYYY-MM-DD HH:mm:ss" style="width:200px" />
      </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="wpsList" border>
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="项目名称" prop="S_PROJECT_NAME" :show-overflow-tooltip="true" min-width="180" />
      <el-table-column label="督察情况" prop="SUPERVISION" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="开始时间" prop="START_DATE" width="160" />
      <el-table-column label="结束时间" prop="END_DATE" width="160" />
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template #default="scope">
          <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="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="formRules" label-width="100px">
        <el-form-item label="关联项目" prop="smId"><el-input v-model="form.smId" /></el-form-item>
        <el-form-item label="督察情况" prop="supervision"><el-input v-model="form.supervision" type="textarea" :rows="4" /></el-form-item>
        <el-form-item label="开始时间" prop="startDate"><el-date-picker v-model="form.startDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width:100%" /></el-form-item>
        <el-form-item label="结束时间" prop="endDate"><el-date-picker v-model="form.endDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width:100%" /></el-form-item>
      </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 { listWps, addWps, updateWps, delWps } from '@/api/hzz/wps'
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 wpsList = ref([])
const queryParams = ref({ projectName: '', startDate: '', endDate: '' })

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formRef = ref(null)
const form = reactive({})
const formRules = { supervision: [{ required: true, message: '督察情况不能为空', trigger: 'blur' }] }

const getList = async () => {
  loading.value = true
  try {
    const res = await listWps({ ...queryParams.value, pageNum: pageNum.value, pageSize: pageSize.value })
    wpsList.value = res.rows || []
    total.value = res.total || 0
  } finally { loading.value = false }
}
const resetQuery = () => { queryParams.value = { projectName: '', startDate: '', endDate: '' }; 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 handleDelete = async (row) => {
  await ElMessageBox.confirm('确认删除该督查记录？', '提示', { type: 'warning' })
  await delWps(row.GUID)
  ElMessage.success('删除成功')
  getList()
}
const submitForm = async () => {
  if (!formRef.value) return
  await formRef.value.validate()
  if (form.GUID) { await updateWps(form) } else { await addWps(form) }
  ElMessage.success('操作成功')
  dialogVisible.value = false
  getList()
}

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