<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="单位性质" prop="orgNature">
        <el-select v-model="queryParams.orgNature" placeholder="请选择" clearable style="width: 180px">
          <el-option label="直属单位" value="1" />
          <el-option label="下级水行政" value="2" />
          <el-option label="流域机构" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="事故采集时间" prop="dateRange">
        <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" style="width: 260px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
      <el-table :height="tableHeight" v-loading="loading" :data="statList" border style="width:100%" :header-cell-style="{ 'text-align': 'center' }">
        <el-table-column label="序号" align="center" type="index" width="60" />
        <el-table-column label="单位名称" align="center" prop="orgname" min-width="160" :show-overflow-tooltip="true" />
        <el-table-column label="事故数量" align="center" prop="accicount" width="100" />
        <el-table-column label="死亡人数" align="center" prop="deathcount" width="100" />
        <el-table-column label="重伤人数" align="center" prop="injurycount" width="100" />
        <el-table-column label="直接经济损失（万元）" align="center" prop="econloss" width="160" />
        <el-table-column label="平均死亡人数" align="center" prop="avgdeath" width="120" />
        <el-table-column label="平均重伤人数" align="center" prop="avginjury" width="120" />
        <el-table-column label="平均经济损失（万元）" align="center" prop="avgeconloss" width="170" />
      </el-table>
    </div>

    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

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

const { tableHeight, containerRef } = useTableHeight()
const { proxy } = getCurrentInstance()

const loading = ref(false)
const showSearch = ref(true)
const statList = ref([])
const total = ref(0)
const dateRange = ref([])

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  orgNature: 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]
  }
  listAcciStat(params).then(res => {
    statList.value = res.rows || []
    total.value = res.total || 0
    loading.value = false
  }).catch(() => { loading.value = false })
}

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

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