<template>
  <div 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: 240px">
          <el-option v-for="item in 12" :key="item" :label="item + '月'" :value="item" />
        </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: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="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: #67C23A">{{ statData.solvedHidd }}</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.solvingHidd }}</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.unsolvedHidd }}</div>
        </el-card>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="hiddCountList">
      <el-table-column label="机构名称" align="center" prop="orgGuid" :show-overflow-tooltip="true" />
      <el-table-column label="统计年份" align="center" prop="statYear" />
      <el-table-column label="统计月份" align="center" prop="statMonth" />
      <el-table-column label="隐患总数" align="center" prop="totalHidd" />
      <el-table-column label="已整改" align="center" prop="solvedHidd" />
      <el-table-column label="整改中" align="center" prop="solvingHidd" />
      <el-table-column label="未整改" align="center" prop="unsolvedHidd" />
      <el-table-column label="整改率" align="center" prop="solveRate">
        <template #default="scope">
          <el-progress :percentage="scope.row.solveRate" :color="scope.row.solveRate >= 80 ? '#67C23A' : scope.row.solveRate >= 60 ? '#E6A23C' : '#F56C6C'"></el-progress>
        </template>
      </el-table-column>
      <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>

    <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"

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
})

const columns = ref({
  orgGuid: { label: '机构名称', visible: true },
  statYear: { label: '统计年份', visible: true },
  statMonth: { label: '统计月份', visible: true },
  totalHidd: { label: '隐患总数', visible: true },
  solvedHidd: { label: '已整改', visible: true },
  solvingHidd: { label: '整改中', visible: true },
  unsolvedHidd: { label: '未整改', visible: true },
  solveRate: { label: '整改率', visible: true }
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    statYear: undefined,
    statMonth: undefined,
    orgGuid: 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
  hiddCountList.value.forEach(item => {
    totalHidd += item.totalHidd || 0
    solvedHidd += item.solvedHidd || 0
    solvingHidd += item.solvingHidd || 0
    unsolvedHidd += item.unsolvedHidd || 0
  })
  statData.value = {
    totalHidd,
    solvedHidd,
    solvingHidd,
    unsolvedHidd
  }
}

/** 搜索按钮操作 */
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: 32px;
  font-weight: bold;
  text-align: center;
  color: #409EFF;
}
</style>
