<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="隐患名称" prop="hiddName">
        <el-input v-model="queryParams.hiddName" placeholder="请输入隐患名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="隐患等级" prop="hiddGrad">
        <el-select v-model="queryParams.hiddGrad" placeholder="请选择隐患等级" clearable style="width: 240px">
          <el-option v-for="dict in hidd_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患状态" prop="hiddStat">
        <el-select v-model="queryParams.hiddStat" placeholder="请选择隐患状态" clearable style="width: 240px">
          <el-option v-for="dict in hidd_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属单位" prop="orgGuid">
        <el-input v-model="queryParams.orgGuid" placeholder="请输入所属单位" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </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="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hidd:objhidd:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-portal-index"></right-toolbar>
    </el-row>

    <el-row :gutter="20" class="mb8">
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>隐患总数</span>
            </div>
          </template>
          <div class="stat-value">{{ statData.totalHidd }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>重大隐患</span>
            </div>
          </template>
          <div class="stat-value" style="color: #F56C6C">{{ statData.majorHidd }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>一般隐患</span>
            </div>
          </template>
          <div class="stat-value" style="color: #E6A23C">{{ statData.normalHidd }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>已整改</span>
            </div>
          </template>
          <div class="stat-value" style="color: #67C23A">{{ statData.solvedHidd }}</div>
        </el-card>
      </el-col>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="objHiddList">
      <el-table-column label="隐患名称" align="center" prop="hiddName" v-if="columns.hiddName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="隐患等级" align="center" prop="hiddGrad" v-if="columns.hiddGrad.visible">
        <template #default="scope">
          <dict-tag :options="hidd_grad" :value="scope.row.hiddGrad" />
        </template>
      </el-table-column>
      <el-table-column label="隐患类别" align="center" prop="hiddClas" v-if="columns.hiddClas.visible">
        <template #default="scope">
          <dict-tag :options="hidd_clas" :value="scope.row.hiddClas" />
        </template>
      </el-table-column>
      <el-table-column label="隐患状态" align="center" prop="hiddStat" v-if="columns.hiddStat.visible">
        <template #default="scope">
          <dict-tag :options="hidd_stat" :value="scope.row.hiddStat" />
        </template>
      </el-table-column>
      <el-table-column label="涉及部位" align="center" prop="proPart" v-if="columns.proPart.visible" :show-overflow-tooltip="true" />
      <el-table-column label="采集时间" align="center" prop="collTime" v-if="columns.collTime.visible" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template #default="scope">
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    </div>

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

<script setup name="HiddPortal">
import { listObjHidd } from "@/api/slaj/hidd"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()
const { hidd_grad, hidd_stat, hidd_clas } = proxy.useDict("hidd_grad", "hidd_stat", "hidd_clas")

const objHiddList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)

const statData = ref({
  totalHidd: 0,
  majorHidd: 0,
  normalHidd: 0,
  solvedHidd: 0
})

const columns = ref({
  hiddName: { label: '隐患名称', visible: true },
  hiddGrad: { label: '隐患等级', visible: true },
  hiddClas: { label: '隐患类别', visible: true },
  hiddStat: { label: '隐患状态', visible: true },
  proPart: { label: '涉及部位', visible: true },
  collTime: { label: '采集时间', visible: true }
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hiddName: undefined,
    hiddGrad: undefined,
    hiddStat: undefined,
    orgGuid: undefined
  }
})

const { queryParams } = toRefs(data)

/** 查询隐患列表 */
function getList() {
  loading.value = true
  listObjHidd(queryParams.value).then(res => {
    loading.value = false
    objHiddList.value = res.rows
    total.value = res.total
    calculateStat()
  })
}

/** 计算统计数据 */
function calculateStat() {
  let totalHidd = 0
  let majorHidd = 0
  let normalHidd = 0
  let solvedHidd = 0
  objHiddList.value.forEach(item => {
    totalHidd++
    if (item.hiddGrad === '2') {
      majorHidd++
    } else {
      normalHidd++
    }
    if (item.hiddStat === '2') {
      solvedHidd++
    }
  })
  statData.value = {
    totalHidd,
    majorHidd,
    normalHidd,
    solvedHidd
  }
}

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

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

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

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

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

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.stat-value {
  font-size: 32px;
  font-weight: bold;
  text-align: center;
  color: #409EFF;
}
</style>
