<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6">
        <org-tree @node-click="handleNodeClick" />
      </el-col>
      <el-col :span="18">
        <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>
            <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-treecount-index"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="treeCountList" row-key="orgGuid" default-expand-all :tree-props="{ children: 'children' }">
          <el-table-column label="机构名称" align="left" prop="orgName" />
          <el-table-column label="隐患总数" align="center" prop="totalHidd" />
          <el-table-column label="重大隐患" align="center" prop="majorHidd" />
          <el-table-column label="一般隐患" align="center" prop="normalHidd" />
          <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>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="HiddTreeCount">
import { listTBusiHiddCount } from "@/api/slaj/hidd"
import OrgTree from "@/components/OrgTree"

const { proxy } = getCurrentInstance()

const treeCountList = ref([])
const loading = ref(true)
const showSearch = ref(true)

const columns = ref({
  orgName: { label: '机构名称', visible: true },
  totalHidd: { label: '隐患总数', visible: true },
  majorHidd: { label: '重大隐患', visible: true },
  normalHidd: { label: '一般隐患', visible: true },
  solvedHidd: { label: '已整改', visible: true },
  solvingHidd: { label: '整改中', visible: true },
  unsolvedHidd: { label: '未整改', visible: true },
  solveRate: { label: '整改率', visible: true }
})

const data = reactive({
  queryParams: {
    statYear: undefined,
    statMonth: undefined,
    orgGuid: undefined
  }
})

const { queryParams } = toRefs(data)

/** 查询树统计列表 */
function getList() {
  loading.value = true
  listTBusiHiddCount(queryParams.value).then(res => {
    loading.value = false
    treeCountList.value = buildTree(res.rows || [])
  })
}

/** 构建树形数据 */
function buildTree(list) {
  const map = {}
  const tree = []
  list.forEach(item => {
    map[item.orgGuid] = { ...item, children: [] }
  })
  list.forEach(item => {
    if (item.parentOrgGuid && map[item.parentOrgGuid]) {
      map[item.parentOrgGuid].children.push(map[item.orgGuid])
    } else {
      tree.push(map[item.orgGuid])
    }
  })
  return tree
}

/** 搜索按钮操作 */
function handleQuery() {
  getList()
}

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

/** 组织树节点点击 */
function handleNodeClick(data) {
  queryParams.value.orgGuid = data.id
  handleQuery()
}

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

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

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
</style>
