<template>
  <uni-collapse>
    <eikon-collapse-item title="资质能力" name="qualification" :open="true" :borderColor="'#B68FF7'">
      <div class="collapse-content">
        <template v-if="isPersonal">
          <div class="content-item">
            <div class="item-label">资质等级：</div>
          </div>
          <div class="content-item">
            <div class="item-value">{{ localSystemData.qualificationLevel }}</div>
          </div>
          <div class="content-item">
            <div class="item-label">可上岗岗位：</div>
          </div>
          <div class="content-item">
            <div class="item-value">{{ localSystemData.availablePositionsStr }}</div>
          </div>
          <div class="content-item">
            <div class="item-label">政治面貌：</div>
            <div class="item-value">{{ localSystemData.politicalStatus }}</div>
          </div>
          <div class="content-item">
            <div class="item-label">学历：</div>
            <div class="item-value">{{ localSystemData.education }}</div>
          </div>
          <div class="content-item">
            <div class="item-label">年龄：</div>
            <div class="item-value">{{ localSystemData.age }}岁</div>
          </div>
          <div class="content-item">
            <div class="item-label">是否通过政审：</div>
            <div class="item-value">{{ localSystemData.isPoliticalReviewPassed ? '是' : '否' }}</div>
          </div>
          <div class="content-item">
            <div class="item-label">健康异常情况：</div>
            <div class="item-value">{{ !localSystemData.isHealthy ? '健康' : localSystemData.isHealthy }}</div>
          </div>
          <div class="content-item">
            <div class="item-label">有无行政处罚：</div>
            <div class="item-value">{{ !localSystemData.hasAdministrativePenalty ? '无' :
              localSystemData.hasAdministrativePenalty }}</div>
          </div>
        </template>
        <template v-else>
          <div class="content-item">
            <div class="item-label">资质等级：</div>
          </div>
          <div class="content-item">
            <div class="item-value">{{ localSystemData.qualificationLevelSum }}</div>
          </div>
          <div class="content-item">
            <div class="item-label">可上岗岗位数：</div>
          </div>
          <div class="content-item">
            <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>
        </template>
      </div>
    </eikon-collapse-item>
  </uni-collapse>
</template>

<script>
import EikonCollapseItem from "./eikon-collapse-item.vue";
import { mapState, mapGetters } from 'vuex'
import useDictMixin from "@/utils/dict"
import { getDictLabelByValue } from "@/utils/common"
export default {
  name: "Qualification",
  components: {
    EikonCollapseItem
  },
  mixins: [useDictMixin],
  data() {
    return {
      localSystemData: {}
    }
  },
  computed: {
    ...mapState({
      systemData: state => state.eikonLevel.systemData,
    }),
    ...mapGetters('eikonLevel', ['isPersonal'])
  },
  watch: {
    systemData: {
      handler(newVal, oldVal) {
        if (this.isPersonal) {
          this.handlePersonalData(newVal)
        } else {
          this.handleDeptData(newVal)
        }
      },
    }
  },
  methods: {
    async handleDeptData(newVal) {
      // 检查newVal是否为null或undefined
      if (!newVal) {
        this.localSystemData = {};
        return;
      }

      const res = { ...newVal };
      const { qualificationLevelStats, positionCompetencyStats, politicalStatusStats, educationStats, ageStats, politicalReviewStats, physicalHealthStats, administrativePenaltyStats } = newVal

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

      // // 使用useDict获取资质等级字典
      // const dict = await this.useDict('sys_user_qualification_level')
      // const qualificationDict = dict['sys_user_qualification_level'] || []

      // // 按照字典顺序遍历，确保从一级开始显示
      // 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}人`
      //   }
      // }
     

      // 按照字典顺序遍历，确保从一级开始显示
      for (const dictItem of qualificationLevelStats) {
        res.qualificationLevelSum = `${res.qualificationLevelSum ? res.qualificationLevelSum + '、' : ''}${dictItem.levelName} ${dictItem.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 = await getDictLabelByValue('sys_user_political_status', item.politicalStatus)
          politicalStatusArr.push({
            politicalStatus,
            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.averageAge ? ageStats.averageAge : '-'
      res.minAge = ageStats.minAge ? ageStats.minAge : '-'
      res.maxAge = ageStats.maxAge ? ageStats.maxAge : '-'
      res.passedKoroughCountSum = politicalReviewStats && politicalReviewStats.passedCount || 0;
      res.healthyCountSum = physicalHealthStats && physicalHealthStats.healthyCount || 0;
      res.penalizedCountSum = administrativePenaltyStats && administrativePenaltyStats.penalizedCount || 0;
      this.localSystemData = res;
    },
    async handlePersonalData(newVal) {
      // 检查newVal是否为null或undefined
      if (!newVal) {
        this.localSystemData = {};
        return;
      }

      // 创建新对象而不是直接修改传入的对象
      const res = { ...newVal };
      //资质等级
      res.qualificationLevel = await getDictLabelByValue('sys_user_qualification_level', newVal
        && newVal.qualificationLevel)
      //政治面貌
      res.politicalStatus = await getDictLabelByValue('sys_user_political_status', newVal
        && newVal.politicalStatus)
      //学历
      res.education = await getDictLabelByValue('sys_user_schooling', newVal
        && newVal.education)
      res.availablePositionsStr = res.availablePositions && res.availablePositions.map(item => item.postName).join('、')
      res.passedKoroughCount =
        this.localSystemData = res;

    }
  }
}
</script>

<style lang="scss" scoped>
.collapse-content {
  padding: 20rpx;
}

.content-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;

  &:last-child {
    margin-bottom: 0;
  }
}

.item-label {
  font-size: 26rpx;
  color: #333333;
  font-weight: 500;
  min-width: 120rpx;
}

.item-value {
  font-size: 26rpx;
  color: #999999;
  font-weight: 500;
}
</style>