<template>
  <ChartsContainer title="资质能力">
    <div style="height: 100%; overflow-y: auto;">
      <div class="content-item">
        <div class="item-label">资质等级：</div>
        <div class="item-value">{{ localSystemData.qualificationLevelSum }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">可上岗岗位数：</div>
        <div class="item-value">{{ localSystemData.availablePositionsSum }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">政治面貌：</div>
        <div class="item-value">{{ localSystemData.politicalStatusSum }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">学历：</div>
        <div class="item-value">{{ localSystemData.educationSum }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">平均年龄：</div>
        <div class="item-value">{{ localSystemData.averageAge }}岁&nbsp;最高{{ localSystemData.maxAge }}岁&nbsp;最低{{
          localSystemData.minAge }}岁</div>
      </div>
      <div class="content-item">
        <div class="item-label">通过政审人数：</div>
        <div class="item-value">{{ localSystemData.passedKoroughCountSum }}人</div>
      </div>
      <div class="content-item">
        <div class="item-label">身体健康人数：</div>
        <div class="item-value">{{ localSystemData.healthyCountSum }}人</div>
      </div>
      <div class="content-item">
        <div class="item-label">行政处罚人数：</div>
        <div class="item-value">{{ localSystemData.penalizedCountSum }}人</div>
      </div>
    </div>
  </ChartsContainer>
  <ChartsContainer title="工作履历">
    <div style="height: 100%;  overflow-y: auto;">
      <div class="content-item">
        <div class="item-label">平均工作年限：</div>
        <div class="item-value">{{ localSystemData && localSystemData.workYearsStats ? localSystemData.workYearsStats.averageWorkYears
          || 0 : 0 }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">平均安检工作年限：</div>
        <div class="item-value">{{ localSystemData && localSystemData.securityWorkYearsStats ?
          localSystemData.securityWorkYearsStats.averageWorkYears || 0 : 0 }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">曾担任过班组长人数：</div>
        <div class="item-value">{{ localSystemData && localSystemData.securityWorkPositionStats ?
          localSystemData.securityWorkPositionStats.teamLeaderCount || 0 : 0 }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">工作奖励次数：</div>
        <div class="item-value">{{ localSystemData && localSystemData.workRewardsStats ?
          localSystemData.workRewardsStats.totalPersonTimes || 0 : 0 }}</div>
      </div>
      <div class="content-item">
        <div class="item-label">工作处罚次数：</div>
        <div class="item-value">{{ localSystemData && localSystemData.workPenaltiesStats ?
          localSystemData.workPenaltiesStats.totalPersonTimes || 0 : 0 }}</div>
      </div>
    </div>
  </ChartsContainer>
</template>

<script setup>
import ChartsContainer from '../ChartsContainer.vue';
import { ref, inject } from 'vue';
import { getModuleMetrics } from '@/api/item/items'
import { useTimeOut } from '../pageItems/useTimeOut'
import { useDict } from '@/utils/dict'

const {
  sys_user_political_status,
  sys_user_qualification_level,
} = useDict('sys_user_political_status', 'sys_user_qualification_level')

const localSystemData = ref({})
const params = inject('provideParams')

const handleDeptData = async (newVal) => {
  // 检查newVal是否为null或undefined
  if (!newVal || Object.values(newVal).length === 0) {
    localSystemData.value = {};
    return;
  }
  
  const res = { ...newVal };
  const { qualificationLevelStats, positionCompetencyStats, politicalStatusStats, educationStats, ageStats, politicalReviewStats, physicalHealthStats, administrativePenaltyStats } = newVal

  // 检查qualificationLevelStats是否存在且是数组
  if (!qualificationLevelStats || !Array.isArray(qualificationLevelStats)) {
    return;
  }

  // 使用useDict获取资质等级字典
  const qualificationDict = sys_user_qualification_level.value || []
  let strArr = [];
  qualificationLevelStats.map(item => {
    strArr.push(`${item?.levelName} ${item.count}人`)
  })
  res.qualificationLevelSum = strArr.join('、')
  // 按照字典顺序遍历，确保从一级开始显示
  // for (const dictItem of qualificationDict) {
  //   const statItem = qualificationLevelStats.find(item => item.levelName === dictItem.value)
  //   if (statItem) {
  //     res.qualificationLevelSum = `${res.qualificationLevelSum ? res.qualificationLevelSum + '、' : ''}${dictItem.label} ${statItem.count}人`
  //   }
  // }

  let availablePositionsSum = []
  for (const item of positionCompetencyStats) {
    availablePositionsSum.push(`${item.postName} ${item.competentCount}人`)
  }
  res.availablePositionsSum = availablePositionsSum.join('、')

  let politicalStatusArr = []
  for (const item of politicalStatusStats) {
    if (item.politicalStatus) {
      let politicalStatus = sys_user_political_status.value.find(attr => attr.value === item.politicalStatus) || {}
      politicalStatusArr.push({
        politicalStatus: politicalStatus.label,
        totalCount: item.totalCount
      })
    }
  }
  let politicalStatusObj = politicalStatusArr.find(item => item.politicalStatus === "中共党员")
  if (politicalStatusObj) {
    res.politicalStatusSum = `${politicalStatusObj.politicalStatus} ${politicalStatusObj.totalCount}人`
  } else {
    res.politicalStatusSum = '-';
  }

  // 处理学历统计信息
  if (educationStats) {
    const educationParts = [];

    // 按学历层次从高到低排序显示
    if (educationStats.masterCount > 0) {
      educationParts.push(`硕士 ${educationStats.masterCount}人`);
    }
    if (educationStats.bachelorCount > 0) {
      educationParts.push(`本科 ${educationStats.bachelorCount}人`);
    }
    if (educationStats.collegeCount > 0) {
      educationParts.push(`专科 ${educationStats.collegeCount}人`);
    }
    if (educationStats.highSchoolCount > 0) {
      educationParts.push(`高中 ${educationStats.highSchoolCount}人`);
    }
    if (educationStats.middleSchoolCount > 0) {
      educationParts.push(`初中 ${educationStats.middleSchoolCount}人`);
    }
    if (educationStats.primarySchoolCount > 0) {
      educationParts.push(`小学 ${educationStats.primarySchoolCount}人`);
    }

    res.educationSum = educationParts.length > 0 ? educationParts.join('、') : '-';
  } else {
    res.educationSum = '-';
  }
  res.averageAge = ageStats && ageStats.averageAge ? ageStats.averageAge : '-'
  res.minAge = ageStats && ageStats.minAge ? ageStats.minAge : '-'
  res.maxAge = ageStats && ageStats.maxAge ? ageStats.maxAge : '-'
  res.passedKoroughCountSum = politicalReviewStats && politicalReviewStats.passedCount || 0;
  res.healthyCountSum = physicalHealthStats && physicalHealthStats.healthyCount || 0;
  res.penalizedCountSum = administrativePenaltyStats && administrativePenaltyStats.penalizedCount || 0;
  localSystemData.value = res;
}
useTimeOut(() => {
  params.value.deptId && sys_user_political_status.value && sys_user_qualification_level.value && getModuleMetrics({ deptId: params.value.deptId, userTypeStr: 'station' }).then(res => {
    const { moduleResults = { system: {} } } = res.data
    handleDeptData(moduleResults.system)
  })
}, [params, sys_user_political_status, sys_user_qualification_level])

</script>

<style lang="scss" scoped>
.content-item {
  display: flex;
  column-gap: 5px;
  margin-bottom: 8px;
  font-size: 16px;

  .item-label {
    color: #fff;
    font-weight: bold;
    width: 10em;
  }

  .item-value {
    color: #fefefe;
    flex: 1;
    text-align: end;
  }
}
</style>