<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="statYear">
        <el-date-picker v-model="queryParams.statYear" type="year" placeholder="选择年份" value-format="YYYY" />
      </el-form-item>
      <el-form-item label="统计月份" prop="statMonth">
        <el-select v-model="queryParams.statMonth" placeholder="请选择统计月份" clearable style="width: 150px">
          <el-option v-for="item in 12" :key="item" :label="item + '月'" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="单位性质" prop="unitType">
        <el-select v-model="queryParams.unitType" 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="orgName">
        <el-input v-model="queryParams.orgName" placeholder="请输入机构名称" clearable style="width: 200px" @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:count:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-count-index"></right-toolbar>
    </el-row>

    <el-row :gutter="20" class="mb8">
      <el-col :span="4">
        <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="4">
        <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-col :span="4">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>整改中</span>
            </div>
          </template>
          <div class="stat-value" style="color: #E6A23C">{{ statData.solvingHidd }}</div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>未整改</span>
            </div>
          </template>
          <div class="stat-value" style="color: #F56C6C">{{ statData.unsolvedHidd }}</div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>逾期未整改</span>
            </div>
          </template>
          <div class="stat-value" style="color: #F56C6C">{{ statData.overTimeHidd }}</div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>综合整改率</span>
            </div>
          </template>
          <div class="stat-value" style="color: #409EFF">{{ statData.avgSolveRate }}%</div>
        </el-card>
      </el-col>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="hiddCountList">
      <el-table-column label="机构名称" align="center" prop="orgName" :show-overflow-tooltip="true" min-width="150" />
      <el-table-column label="隐患总数" align="center" prop="totalHidd" width="90" />
      <el-table-column label="重大隐患数" align="center" prop="zdHidd" width="100" />
      <el-table-column label="已整改重大" align="center" prop="zgZdHidd" width="100" />
      <el-table-column label="整改中重大" align="center" prop="zdSolvingHidd" width="100" />
      <el-table-column label="未整改重大" align="center" prop="zdUnsolvedHidd" width="100" />
      <el-table-column label="逾期重大" align="center" prop="zdOverTimeHidd" width="90">
        <template #default="scope">
          <span :style="{ color: scope.row.zdOverTimeHidd > 0 ? '#F56C6C' : '' }">
            {{ scope.row.zdOverTimeHidd }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="重大整改率" align="center" prop="zdZgl" width="100">
        <template #default="scope">
          <span :style="{ color: scope.row.zdZgl >= 80 ? '#67C23A' : scope.row.zdZgl >= 60 ? '#E6A23C' : '#F56C6C' }">
            {{ scope.row.zdZgl }}%
          </span>
        </template>
      </el-table-column>
      <el-table-column label="一般隐患数" align="center" prop="ybHidd" width="100" />
      <el-table-column label="已整改一般" align="center" prop="zgYbHidd" width="100" />
      <el-table-column label="整改中一般" align="center" prop="ybSolvingHidd" width="100" />
      <el-table-column label="未整改一般" align="center" prop="ybUnsolvedHidd" width="100" />
      <el-table-column label="逾期一般" align="center" prop="ybOverTimeHidd" width="90">
        <template #default="scope">
          <span :style="{ color: scope.row.ybOverTimeHidd > 0 ? '#F56C6C' : '' }">
            {{ scope.row.ybOverTimeHidd }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="一般整改率" align="center" prop="ybZgl" width="100">
        <template #default="scope">
          <span :style="{ color: scope.row.ybZgl >= 80 ? '#67C23A' : scope.row.ybZgl >= 60 ? '#E6A23C' : '#F56C6C' }">
            {{ scope.row.ybZgl }}%
          </span>
        </template>
      </el-table-column>
      <el-table-column label="综合整改率" align="center" prop="hiddZgl" width="120">
        <template #default="scope">
          <el-progress :percentage="scope.row.hiddZgl" :color="scope.row.hiddZgl >= 80 ? '#67C23A' : scope.row.hiddZgl >= 60 ? '#E6A23C' : '#F56C6C'"></el-progress>
        </template>
      </el-table-column>
      <el-table-column label="排查率" align="center" prop="pcl" width="80">
        <template #default="scope">
          <span>{{ scope.row.pcl }}%</span>
        </template>
      </el-table-column>
      <el-table-column label="工程总数" align="center" prop="engNum" width="90" />
      <el-table-column label="有隐患单位" align="center" prop="hiddOrg" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80" fixed="right">
        <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="TBusiHiddCount">
import { listTBusiHiddCount } from "@/api/slaj/hidd"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()

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

const statData = ref({
  totalHidd: 0,
  solvedHidd: 0,
  solvingHidd: 0,
  unsolvedHidd: 0,
  overTimeHidd: 0,
  avgSolveRate: 0
})

const columns = ref({
  orgName: { label: '机构名称', visible: true },
  totalHidd: { label: '隐患总数', visible: true },
  zdHidd: { label: '重大隐患数', visible: true },
  zgZdHidd: { label: '已整改重大', visible: true },
  zdSolvingHidd: { label: '整改中重大', visible: true },
  zdUnsolvedHidd: { label: '未整改重大', visible: true },
  zdOverTimeHidd: { label: '逾期重大', visible: true },
  zdZgl: { label: '重大整改率', visible: true },
  ybHidd: { label: '一般隐患数', visible: true },
  zgYbHidd: { label: '已整改一般', visible: true },
  ybSolvingHidd: { label: '整改中一般', visible: true },
  ybUnsolvedHidd: { label: '未整改一般', visible: true },
  ybOverTimeHidd: { label: '逾期一般', visible: true },
  ybZgl: { label: '一般整改率', visible: true },
  hiddZgl: { label: '综合整改率', visible: true },
  pcl: { label: '排查率', visible: true },
  engNum: { label: '工程总数', visible: true },
  hiddOrg: { label: '有隐患单位', visible: true }
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    statYear: undefined,
    statMonth: undefined,
    orgName: undefined,
    unitType: undefined
  }
})

const { queryParams } = toRefs(data)

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

/** 计算统计数据 */
function calculateStat() {
  let totalHidd = 0
  let solvedHidd = 0
  let solvingHidd = 0
  let unsolvedHidd = 0
  let overTimeHidd = 0
  let totalZgl = 0
  let count = 0
  hiddCountList.value.forEach(item => {
    totalHidd += item.totalHidd || 0
    solvedHidd += item.solvedHidd || 0
    solvingHidd += item.solvingHidd || 0
    unsolvedHidd += item.unsolvedHidd || 0
    overTimeHidd += (item.zdOverTimeHidd || 0) + (item.ybOverTimeHidd || 0)
    if (item.hiddZgl) {
      totalZgl += item.hiddZgl
      count++
    }
  })
  statData.value = {
    totalHidd,
    solvedHidd,
    solvingHidd,
    unsolvedHidd,
    overTimeHidd,
    avgSolveRate: count > 0 ? Math.round(totalZgl / count) : 0
  }
}

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

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

/** 详情按钮操作 */
function handleDetail(row) {
  proxy.$router.push({ path: '/slaj/hidd/detailCount', query: { orgGuid: row.orgGuid, statYear: row.statYear, statMonth: row.statMonth } })
}

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

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

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