<template>
  <el-card v-if="visible" shadow="never" class="mt20" :header="title">
    <div v-if="showForceAdjustTip">
      <template v-if="chartBinding.generation.status.disenrollCount">
        贵校在二次补录阶段任有
        <span class="f-red bold">{{ chartBinding.generation.status.disenrollCount }}</span>
        位学生未被录，贵校可以通过系统调剂功能，任意调剂学生去学校开设的任何组合中
      </template>
      <template v-else>
        贵校学生已经全部录取完毕。在{{ this.chartBinding.generation.activeOpt.title }}阶段，贵校任可以通过系统调剂功能，任意调剂学生去学校开设的任何组合中
      </template>
    </div>
    <div v-else>
      <template v-if="showAlgorithmTip">
        请先运行智能匹配算法，以获取阶段录取信息
      </template>
      <template v-else-if="hasIndicateGroups.length">
        <el-collapse v-model="collapseModel">
          <el-collapse-item v-for="g in hasIndicateGroups" :key="g.groupId" :name="g.groupId" :title="g.groupName">
            <div>
              补录计划人数
              <span class="f-warning bold">{{ getIndicateCount(g) }}</span>
              人，报名人数
              <span class="f-333 bold">{{ getActualCount(g) }}</span>
              <under-over-text :value="getActualCount(g)-getIndicateCount(g)"></under-over-text>
              ，实际录取
              <span class="f-primary bold">{{ getEnrollCount(g) }}</span>
              人。
            </div>
            <template v-if="prevDMData">
              <div>
                专业成绩符合学生中，按照系统推荐报名
                <span class="f-primary bold">{{ getMatchedAgree(g) }}</span>
                人，拒绝填报
                <span class="f-red bold">{{ getMatchedReject(g) }}</span>
                人，未填报
                <span class="f-red bold">{{ getMatchedNonaction(g) }}</span>
                人，改出
                <span class="f-warning bold">{{ getMatchedApplyOut(g) }}</span>
                人，改进
                <span class="f-primary bold">{{ getMatchedApplyIn(g) }}</span>
                人。
              </div>
              <div>
                成绩符合专业不符合学生中，按照系统推荐报名
                <span class="f-primary bold">{{ getNonMatchedAgree(g) }}</span>
                人，拒绝填报
                <span class="f-red bold">{{ getNonMatchedReject(g) }}</span>
                人，未填报
                <span class="f-red bold">{{ getNonMatchedNonaction(g) }}</span>
                人，改出
                <span class="f-warning bold">{{ getNonMatchedApplyOut(g) }}</span>
                人，改进
                <span class="f-primary bold">{{ getNonMatchedApplyIn(g) }}</span>
                人。
              </div>
            </template>
          </el-collapse-item>
        </el-collapse>
      </template>
      <template v-else>
        所有组合均已录取完毕
      </template>
    </div>
  </el-card>
</template>

<script>
import config from '@/common/mx-config'
import UnderOverText from '@/views/elective/generation/components/under-over-text'

export default {
  name: 'elective-generation-enroll-info',
  components: { UnderOverText },
  props: ['chartBinding'],
  data() {
    return {
      collapseModel: []
    }
  },
  computed: {
    options() {
      return config.electiveGenerationOptions
    },
    title() {
      const activeTitle = this.chartBinding.generation?.activeOpt?.title
      if (this.chartBinding.generation?.active < this.options.forceAdjust.value) {
        const shortTitle = activeTitle.substring(0, activeTitle.length - 2)
        return '贵校' + shortTitle + '阶段录取情况'
      }
      return activeTitle + '阶段说明'
    },
    visible() {
      return !!this.chartBinding.generation?.activeOpt?.decisionMaking && this.chartBinding.tableData
    },
    showForceAdjustTip() {
      return this.chartBinding.generation.activeOpt == this.options.forceAdjust
    },
    showAlgorithmTip() {
      return this.chartBinding.generation.current == this.chartBinding.generation.active
        && !this.chartBinding.generation.status.doneDMAlgorithm
    },
    activeDMData() {
      const target = this.chartBinding.generation.active
      return this.chartBinding.tableData.find(t => t.generation == target)
    },
    prevApplyData() {
      const target = this.chartBinding.generation.active - 1
      return this.chartBinding.tableData.find(t => t.generation == target)
    },
    prevDMData() {
      const target = this.chartBinding.generation.active - 2
      if (target < this.options.primary.value) return null
      return this.chartBinding.tableData.find(t => t.generation == target)
    },
    hasIndicateGroups() {
      // 要解析前一阶段有指标的组合，进行致本阶段的报名与录取情况
      if (!this.prevDMData) return this.chartBinding.generation.roundGroups // 初录结果，全量返回
      const indicateData = this.prevDMData.categories.find(c => c.category == 'indicateCount')
      const adjustData = this.prevDMData.categories.find(c => c.category == 'adjustCount')
      if (!indicateData) return []
      return this.chartBinding.generation.roundGroups.filter(g => {
        const groupVal = indicateData.values.find(v => v.groupId == g.groupId)
        const groupAdVal = adjustData?.values?.find(v => v.groupId == g.groupId)
        const isBT = this.chartBinding.generation.activeOpt == this.options.backTrackingDM
        return isBT ? groupVal.value < 0 && groupAdVal.value > 0 : groupVal.value < 0
      })
    }
  },
  watch: {
    'hasIndicateGroups': function() {
      this.collapseModel = this.hasIndicateGroups.map(g => g.groupId)
    }
  },
  methods: {
    getCategoryValue(tableData, category, group) {
      const data = tableData.categories.find(c => c.category == category)
      return data?.values.find(v => v.groupId == group.groupId)
    },
    getIndicateCount(group) {
      // 录取指标
      if (!this.prevDMData) return group.expectedCount
      const val = this.getCategoryValue(this.prevDMData, 'indicateCount', group)
      return Math.abs(val.value)
    },
    getActualCount(group) {
      if (!this.prevDMData) {
        // 报名人数，只取第一志愿
        const isMultiplePreference = this.prevApplyData.categories.some(sub => Array.isArray(sub))
        const tableData = isMultiplePreference ? { categories: this.prevApplyData.categories.first() } : this.prevApplyData
        const val = this.getCategoryValue(tableData, 'actualCount', group)
        return val.value
      } else {
        // 取报名代所有细分合并
        const subCategories = ['matchedApproved', 'matchedRankout', 'nonmatchedApproved', 'nonmatchedRankout']
        return subCategories.sum(c => this.getCategoryValue(this.prevApplyData, c, group)?.value)
      }
    },
    getEnrollCount(group) {
      return this.getCategoryValue(this.activeDMData, 'approvedCount', group)?.value || '-'
    },
    getMatchedAgree(group) {
      return this.getCategoryValue(this.prevApplyData, 'matchedApproved', group)?.value || '-'
    },
    getNonMatchedAgree(group) {
      return this.getCategoryValue(this.prevApplyData, 'nonmatchedApproved', group)?.value || '-'
    },
    getMatchedReject(group) {
      return this.getCategoryValue(this.prevApplyData, 'matchedNotOptional', group)?.value || '-'
    },
    getNonMatchedReject(group) {
      return this.getCategoryValue(this.prevApplyData, 'nonmatchedNotOptional', group)?.value || '-'
    },
    getMatchedNonaction(group) {
      return this.getCategoryValue(this.prevApplyData, 'matchedNonaction', group)?.value || '-'
    },
    getNonMatchedNonaction(group) {
      return this.getCategoryValue(this.prevApplyData, 'nonmatchedNonaction', group)?.value || '-'
    },
    getMatchedApplyIn(group) {
      return this.getCategoryValue(this.prevApplyData, 'matchedRankout', group)?.value || '-'
    },
    getNonMatchedApplyIn(group) {
      return this.getCategoryValue(this.prevApplyData, 'nonmatchedRankout', group)?.value || '-'
    },
    getMatchedApplyOut(group) {
      return this.getCategoryValue(this.prevApplyData, 'matchedRejected', group)?.value || '-'
    },
    getNonMatchedApplyOut(group) {
      return this.getCategoryValue(this.prevApplyData, 'nonmatchedRejected', group)?.value || '-'
    }
  }
}
</script>

<style scoped>

</style>
