<template>
  <div class="app-container cphs-page">
    <!-- 搜索区 -->
    <div class="filter-panel">
      <div class="filter-form">
        <el-select v-model="searchForm.category" placeholder="事件类别" clearable style="width:160px">
          <el-option label="管道泄漏" value="管道泄漏" />
          <el-option label="塌陷" value="塌陷" />
          <el-option label="积水" value="积水" />
          <el-option label="设备故障" value="设备故障" />
        </el-select>
        <el-select v-model="searchForm.level" placeholder="事件等级" clearable style="width:140px">
          <el-option label="一般" value="一般" />
          <el-option label="较大" value="较大" />
          <el-option label="重大" value="重大" />
          <el-option label="特别重大" value="特别重大" />
        </el-select>
        <el-select v-model="searchForm.verifyStatus" placeholder="核实状态" clearable style="width:140px">
          <el-option label="待核实" value="待核实" />
          <el-option label="已核实" value="已核实" />
          <el-option label="不实事件" value="不实事件" />
        </el-select>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </div>
    </div>

    <!-- 数据表格 -->
    <div class="table-panel">
      <el-table :data="tableData" border stripe style="width:100%" v-loading="loading">
        <el-table-column prop="eventNo" label="事件编号" width="140" />
        <el-table-column prop="category" label="事件类别" width="110">
          <template #default="{ row }">{{ row.category || '-' }}</template>
        </el-table-column>
        <el-table-column prop="level" label="事件等级" width="100">
          <template #default="{ row }">
            <el-tag v-if="row.level" :type="levelTagType(row.level)" size="small">{{ row.level }}</el-tag>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="location" label="发生地点" show-overflow-tooltip />
        <el-table-column prop="judgeTime" label="初判时间" width="150" />
        <el-table-column prop="verifier" label="核实人" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="statusTagType(row.status)" size="small">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button v-if="row.status === '待核实'" type="primary" link size="small" @click="handleVerify(row)">核实</el-button>
            <el-button type="primary" link size="small" @click="handleDetail(row)">详情</el-button>
            <el-button type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-wrap">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- GIS 占位区 -->
    <div class="gis-panel">
      <div class="gis-placeholder">事件坐标 GIS 展示区</div>
    </div>

    <!-- 核实弹窗 -->
    <el-dialog v-model="verifyVisible" title="事件核实" width="600px" :close-on-click-modal="false">
      <el-form :model="verifyForm" :rules="verifyRules" ref="verifyFormRef" label-width="100px">
        <div class="section-title">事件基本信息</div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="事件编号">{{ verifyForm.eventNo }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发生地点">{{ verifyForm.location }}</el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="事件描述">
          <div class="desc-text">{{ verifyForm.description }}</div>
        </el-form-item>
        <div class="section-title">核实信息</div>
        <el-form-item label="事件类别" prop="category">
          <el-select v-model="verifyForm.category" placeholder="请选择" style="width:100%">
            <el-option label="管道泄漏" value="管道泄漏" />
            <el-option label="塌陷" value="塌陷" />
            <el-option label="积水" value="积水" />
            <el-option label="设备故障" value="设备故障" />
          </el-select>
        </el-form-item>
        <el-form-item label="事件等级" prop="level">
          <el-select v-model="verifyForm.level" placeholder="请选择" style="width:100%">
            <el-option label="一般" value="一般" />
            <el-option label="较大" value="较大" />
            <el-option label="重大" value="重大" />
            <el-option label="特别重大" value="特别重大" />
          </el-select>
        </el-form-item>
        <el-form-item label="核实意见" prop="opinion">
          <el-input v-model="verifyForm.opinion" type="textarea" :rows="3" placeholder="请输入核实意见" />
        </el-form-item>
        <el-form-item label="是否真实" prop="isReal">
          <el-radio-group v-model="verifyForm.isReal">
            <el-radio label="真实">真实</el-radio>
            <el-radio label="不实">不实</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="verifyVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmVerify">确认核实</el-button>
      </template>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog v-model="detailVisible" title="事件详情" width="600px" :close-on-click-modal="false">
      <div v-if="currentRow" class="detail-content">
        <el-form label-width="100px" size="default">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="接报编号">{{ currentRow.eventNo }}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="事件标识">{{ currentRow.eventNo }}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="事件来源">{{ currentRow.source || '-' }}</el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="事件类别">{{ currentRow.category }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="事件等级">{{ currentRow.level }}</el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="发生地点">{{ currentRow.location }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="初判时间">{{ currentRow.judgeTime }}</el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="事件描述">
            <div class="desc-text">{{ currentRow.description }}</div>
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="核实人">{{ currentRow.verifier || '-' }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="核实时间">{{ currentRow.verifyTime || '-' }}</el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="核实意见">
            <div class="desc-text">{{ currentRow.verifyOpinion || '-' }}</div>
          </el-form-item>
          <el-form-item label="状态">
            <el-tag :type="statusTagType(currentRow.status)" size="small">{{ currentRow.status }}</el-tag>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cphs">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getEventPage, deleteEventInfo, verifyEventInfo } from '@/api/dispatch'

// ==================== 数据状态 ====================
const tableData = ref([])
const total = ref(0)
const loading = ref(false)
const searchForm = ref({ category: '', level: '', verifyStatus: '' })
const pagination = ref({ page: 1, pageSize: 10 })

// ==================== 加载数据 ====================
async function loadData() {
  loading.value = true
  try {
    const params = { status: '待核实' }
    if (searchForm.value.category) params.category = searchForm.value.category
    if (searchForm.value.level) params.level = searchForm.value.level
    if (searchForm.value.verifyStatus) params.status = searchForm.value.verifyStatus
    const res = await getEventPage(pagination.value.page, pagination.value.pageSize, params)
    tableData.value = res.data?.rows || res.data?.records || res.data || []
    total.value = res.data?.total || 0
  } catch (error) {
    ElMessage.error('加载数据失败：' + (error.message || '未知错误'))
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})

// ==================== 搜索筛选 ====================
function handleSearch() {
  pagination.value.page = 1
  loadData()
}

function handleReset() {
  searchForm.value = { category: '', level: '', verifyStatus: '' }
  pagination.value.page = 1
  loadData()
}

// ==================== 分页 ====================
function handleSizeChange(val) {
  pagination.value.pageSize = val
  pagination.value.page = 1
  loadData()
}

function handleCurrentChange(val) {
  pagination.value.page = val
  loadData()
}

// ==================== 标签样式 ====================
function statusTagType(status) {
  if (status === '待核实') return 'warning'
  if (status === '已核实') return 'success'
  if (status === '不实事件') return 'info'
  return 'info'
}

function levelTagType(level) {
  if (level === '特别重大') return 'danger'
  if (level === '重大') return 'danger'
  if (level === '较大') return 'warning'
  return 'info'
}

// ==================== 核实 ====================
const verifyVisible = ref(false)
const verifyFormRef = ref(null)
const currentVerifyId = ref(null)
const verifyForm = ref({ eventNo: '', location: '', description: '', category: '', level: '', opinion: '', isReal: '真实' })
const verifyRules = {
  category: [{ required: true, message: '请选择事件类别', trigger: 'change' }],
  level: [{ required: true, message: '请选择事件等级', trigger: 'change' }],
  opinion: [{ required: true, message: '请输入核实意见', trigger: 'blur' }],
  isReal: [{ required: true, message: '请选择是否真实', trigger: 'change' }]
}

function handleVerify(row) {
  currentVerifyId.value = row.id
  verifyForm.value = {
    eventNo: row.eventNo,
    location: row.location,
    description: row.description,
    category: row.category,
    level: row.level,
    opinion: row.verifyOpinion || '',
    isReal: '真实'
  }
  verifyVisible.value = true
}

async function confirmVerify() {
  try {
    await verifyFormRef.value.validate()
  } catch {
    return
  }
  try {
    await verifyEventInfo({
      id: currentVerifyId.value,
      category: verifyForm.value.category,
      level: verifyForm.value.level,
      opinion: verifyForm.value.opinion,
      isReal: verifyForm.value.isReal
    })
    ElMessage.success('核实成功')
    verifyVisible.value = false
    loadData()
  } catch (error) {
    ElMessage.error('核实失败：' + (error.message || '未知错误'))
  }
}

// ==================== 删除 ====================
async function handleDelete(row) {
  try {
    await ElMessageBox.confirm('确定要删除该核实记录吗？', '删除确认', { type: 'warning' })
    await deleteEventInfo(row.id)
    ElMessage.success('删除成功')
    loadData()
  } catch (error) {
    if (error !== 'cancel' && error !== 'close') {
      ElMessage.error('删除失败：' + (error.message || '未知错误'))
    }
  }
}

// ==================== 详情 ====================
const detailVisible = ref(false)
const currentRow = ref(null)

function handleDetail(row) {
  currentRow.value = row
  detailVisible.value = true
}
</script>

<style scoped>
.cphs-page {
  padding: 16px;
  background: #f5f5f5;
  min-height: calc(100vh - 84px);
}

.filter-panel {
  background: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 12px 16px;
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.filter-form {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.table-panel {
  background: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 16px;
  margin-bottom: 12px;
}

.pagination-wrap {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}

.gis-panel {
  background: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 16px;
}

.gis-placeholder {
  height: 240px;
  background: #f5f7fa;
  border: 1px dashed #c0c4cc;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
  font-size: 16px;
}

.detail-content {
  padding: 0 8px;
}

.section-title {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 12px;
  padding-left: 8px;
  border-left: 3px solid #409EFF;
}

.desc-text {
  background: #f5f7fa;
  border-radius: 4px;
  padding: 10px;
  font-size: 13px;
  color: #606266;
  line-height: 1.6;
  max-height: 120px;
  overflow-y: auto;
}

:deep(.el-form-item__label) {
  color: #909399;
}

:deep(.el-form-item__content) {
  color: #303133;
}
</style>
