<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事故名称" prop="acciName">
        <el-input v-model="queryParams.acciName" placeholder="请输入事故名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="事故等级" prop="acciGrad">
        <el-select v-model="queryParams.acciGrad" placeholder="请选择事故等级" clearable style="width: 240px">
          <el-option v-for="dict in acci_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="事故类别" prop="acciCate">
        <el-select v-model="queryParams.acciCate" placeholder="请选择事故类别" clearable style="width: 240px">
          <el-option v-for="dict in acci_cate" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="报告状态" prop="repStat">
        <el-select v-model="queryParams.repStat" placeholder="请选择报告状态" clearable style="width: 240px">
          <el-option v-for="dict in rep_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- 业务说明: 查询范围 dataFrom 控制事故数据的所属维度。1=直属单位(本级直接管辖)，2=下级水行政(下级水利部门)，3=流域机构(流域管理范围) -->
      <el-form-item label="查询范围" prop="dataFrom">
        <el-select v-model="queryParams.dataFrom" placeholder="查询范围" clearable style="width: 240px">
          <el-option label="直属单位" value="1" />
          <el-option label="下级水行政" value="2" />
          <el-option label="流域机构" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="发生时间" style="width: 308px">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['acci:objacci:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['acci:objacci:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['acci:objacci:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['acci:objacci:export']">导出</el-button>
      </el-col>
      <!-- 业务说明: 批量上报 — 将表格中勾选的多条事故记录一次性提交至上级单位，后端批量更新事故状态为"已上报"（状态值2） -->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Top" :disabled="multiple" @click="handleBatchReport" v-hasPermi="['acci:objacci:edit']">批量上报</el-button>
      </el-col>
      <!-- 业务说明: 催报 — 督促下级单位上报事故月报，触发后端向目标单位发送通知/提醒，仅当选中单条记录时可操作 -->
      <el-col :span="1.5">
        <el-button type="info" plain icon="Bell" :disabled="single" @click="handlePushForward" v-hasPermi="['acci:objacci:edit']">催报</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="acci-objacci-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="objAcciList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事故名称" align="center" prop="acciName" v-if="columns.acciName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="事故等级" align="center" prop="acciGrad" v-if="columns.acciGrad.visible">
        <template #default="scope">
          <dict-tag :options="acci_grad" :value="scope.row.acciGrad" />
        </template>
      </el-table-column>
      <el-table-column label="事故类别" align="center" prop="acciCate" v-if="columns.acciCate.visible">
        <template #default="scope">
          <dict-tag :options="acci_cate" :value="scope.row.acciCate" />
        </template>
      </el-table-column>
      <el-table-column label="报告状态" align="center" prop="repStat" v-if="columns.repStat.visible">
        <template #default="scope">
          <dict-tag :options="rep_stat" :value="scope.row.repStat" />
        </template>
      </el-table-column>
      <el-table-column label="发生时间" align="center" prop="occuTime" v-if="columns.occuTime.visible" width="180" />
      <el-table-column label="发生地点" align="center" prop="occuLoc" v-if="columns.occuLoc.visible" :show-overflow-tooltip="true" />
      <el-table-column label="死亡/重伤/失踪" align="center" width="160" v-if="columns.casNum.visible">
        <template #default="scope">
          {{ scope.row.casNum || 0 }}/{{ scope.row.serInjNum || 0 }}/{{ scope.row.missNum || 0 }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['acci:objacci:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="催报" placement="top">
            <el-button link type="primary" icon="Bell" @click="handlePushForward(scope.row)" v-hasPermi="['acci:objacci:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['acci:objacci:remove']"></el-button>
          </el-tooltip>
        </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-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form :model="form" :rules="rules" ref="objAcciRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="事故名称" prop="acciName">
              <el-input v-model="form.acciName" placeholder="请输入事故名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="事故等级" prop="acciGrad">
              <el-select v-model="form.acciGrad" placeholder="请选择事故等级">
                <el-option v-for="dict in acci_grad" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="事故类别" prop="acciCate">
              <el-select v-model="form.acciCate" placeholder="请选择事故类别">
                <el-option v-for="dict in acci_cate" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报告状态" prop="repStat">
              <el-select v-model="form.repStat" placeholder="请选择报告状态">
                <el-option v-for="dict in rep_stat" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发生时间" prop="occuTime">
              <el-date-picker v-model="form.occuTime" type="date" placeholder="请选择发生时间" value-format="YYYY-MM-DD" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发生地点" prop="occuLoc">
              <el-input v-model="form.occuLoc" placeholder="请输入发生地点" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="死亡" prop="casNum">
              <el-input-number v-model="form.casNum" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="重伤" prop="serInjNum">
              <el-input-number v-model="form.serInjNum" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="失踪" prop="missNum">
              <el-input-number v-model="form.missNum" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="经济损失(万元)" prop="econLoss">
              <el-input-number v-model="form.econLoss" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="事故描述" prop="acciDesc">
              <el-input v-model="form.acciDesc" type="textarea" placeholder="请输入事故描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="note">
              <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ObjAcci">
import { listObjAcci, getObjAcci, delObjAcci, addObjAcci, updateObjAcci, pushForwardObjAcci, batchReportObjAcci } from "@/api/slaj/acci"

const { proxy } = getCurrentInstance()
const { acci_grad, acci_cate, rep_stat } = proxy.useDict("acci_grad", "acci_cate", "rep_stat")

const objAcciList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const dateRange = ref([])

const columns = ref({
  acciName: { label: '事故名称', visible: true },
  acciGrad: { label: '事故等级', visible: true },
  acciCate: { label: '事故类别', visible: true },
  repStat: { label: '报告状态', visible: true },
  occuTime: { label: '发生时间', visible: true },
  occuLoc: { label: '发生地点', visible: true },
  casNum: { label: '伤亡人数', visible: true },
  econLoss: { label: '经济损失(万元)', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    acciName: undefined,
    acciGrad: undefined,
    acciCate: undefined,
    repStat: undefined,
    dataFrom: undefined,
    orgGuid: undefined
  },
  rules: {
    acciName: [{ required: true, message: "事故名称不能为空", trigger: "blur" }],
    acciGrad: [{ required: true, message: "事故等级不能为空", trigger: "change" }],
    acciCate: [{ required: true, message: "事故类别不能为空", trigger: "change" }],
    repStat: [{ required: true, message: "报告状态不能为空", trigger: "change" }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询事故列表 */
function getList() {
  loading.value = true
  listObjAcci(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    objAcciList.value = res.rows
    total.value = res.total
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.guid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加事故"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const guid = row.guid || ids.value
  getObjAcci(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改事故"
  })
}

/** 详情按钮操作 */
function handleDetail(row) {
  const guid = row.guid || ids.value
  proxy.$router.push({ path: '/slaj/acci/detail', query: { guid: guid } })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["objAcciRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateObjAcci(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addObjAcci(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 业务说明: 催报 — 向目标单位发送催报通知，提醒其尽快完成事故月报的上报。后端 pushForwardObjAcci 接口会触发通知推送 */
function handlePushForward(row) {
  const guid = row.guid || ids.value
  proxy.$confirm('确认催报该事故?', '提示', { type: 'warning' }).then(function () {
    return pushForwardObjAcci({ guid: guid })
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("催报成功")
  }).catch(() => {})
}

/** 业务说明: 批量上报 — 将选中的多条事故记录批量提交至上级单位。后端 batchReportObjAcci 会统一将事故状态更新为"2=已上报" */
function handleBatchReport() {
  proxy.$confirm('确认将选中的事故上报至上级单位?', '提示', { type: 'warning' }).then(function () {
    return batchReportObjAcci({ guids: ids.value })
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("批量上报成功")
  }).catch(() => {})
}

/** 删除按钮操作 */
function handleDelete(row) {
  const guids = row.guid || ids.value
  proxy.$modal.confirm('是否确认删除事故编号为"' + guids + '"的数据项？').then(function () {
    return delObjAcci(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 取消按钮操作 */
function cancel() {
  open.value = false
  reset()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("acci/objacci/export", {
    ...queryParams.value
  }, `acci_${new Date().getTime()}.xlsx`)
}

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    acciName: undefined,
    acciGrad: undefined,
    acciCate: undefined,
    repStat: undefined,
    occuTime: undefined,
    occuLoc: undefined,
    casNum: undefined,
    serInjNum: undefined,
    missNum: undefined,
    econLoss: undefined,
    acciDesc: undefined,
    note: undefined,
    orgGuid: undefined
  }
  proxy.resetForm("objAcciRef")
}

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