<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
      <el-form-item label="组织机构" prop="sgdwName">
        <el-input v-model="queryParams.sgdwName" placeholder="请输入" clearable style="width: 150px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="事故级别" prop="acciGrad">
        <el-select v-model="queryParams.acciGrad" placeholder="请选择" clearable style="width: 130px">
          <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: 130px">
          <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: 130px">
          <el-option v-for="dict in rep_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="发生时间">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="objAcciList" border :header-cell-style="{ 'text-align': 'center' }">
      <el-table-column label="序号" align="center" type="index" width="60" />
      <el-table-column label="事故名称" align="center" prop="acciName" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="事故级别" align="center" prop="acciGrad" width="100">
        <template #default="scope">
          <dict-tag :options="acci_grad" :value="scope.row.acciGrad" />
        </template>
      </el-table-column>
      <el-table-column label="事故类型" align="center" prop="acciCate" width="100">
        <template #default="scope">
          <dict-tag :options="acci_cate" :value="scope.row.acciCate" />
        </template>
      </el-table-column>
      <el-table-column label="死亡人数" align="center" prop="casNum" width="90" />
      <el-table-column label="重伤人数" align="center" prop="serInjNum" width="90" />
      <el-table-column label="直接经济损失（万元）" align="center" prop="econLoss" width="160" />
      <el-table-column label="上报状态" align="center" prop="repStat" width="100">
        <template #default="scope">
          <dict-tag :options="rep_stat" :value="scope.row.repStat" />
        </template>
      </el-table-column>
      <el-table-column label="发生时间" align="center" prop="occuTime" width="120" />
      <el-table-column label="发生地点" align="center" prop="occuLoc" min-width="150" :show-overflow-tooltip="true" />
    </el-table>
    </div>

    <pagination style="margin-top: 8px;" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="ObjAcci">
import { listObjAcci } from "@/api/slaj/acci"
import { useTableHeight } from '@/hooks/useTableHeight'

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

const objAcciList = ref([])
const loading = ref(false)
const total = ref(0)
const dateRange = ref([])

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sgdwName: undefined,
  acciGrad: undefined,
  acciCate: undefined,
  repStat: undefined,
  beginTime: undefined,
  endTime: undefined
})

function getList() {
  loading.value = true
  const params = { ...queryParams }
  if (dateRange.value && dateRange.value.length === 2) {
    params.beginTime = dateRange.value[0]
    params.endTime = dateRange.value[1]
  }
  listObjAcci(params).then(res => {
    objAcciList.value = res.rows || []
    total.value = res.total || 0
    loading.value = false
  }).catch(() => { loading.value = false })
}

function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

function handleExport() {
  const params = { ...queryParams }
  if (dateRange.value && dateRange.value.length === 2) {
    params.beginTime = dateRange.value[0]
    params.endTime = dateRange.value[1]
  }
  proxy.download("acci/objacci/export", params, `事故数据_${new Date().getTime()}.xlsx`)
}

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