<template>
  <div class="fx-row fx-bet-cen">
    <div class="fx-1">
      <el-button v-if="showDMAlgorithm" type="primary" @click="applyAlgorithm">智能匹配</el-button>
      <el-button v-if="showDMAlgorithmResults" type="primary" @click="goDetailPage">查看匹配明细</el-button>
      <el-button v-if="showForceAdjust" type="primary" @click="goDetailPage">查看/调剂</el-button>
    </div>
    <div>
      <el-button v-if="showFastPush" type="primary" @click="flushIntoDM">提前进入{{ nextStepName }}</el-button>
      <el-button v-if="showForceAdjustSend" type="primary" @click="jumpForceAdjust">进入强制调剂</el-button>
      <el-button v-else-if="showSend" type="primary" @click="pushGeneration">发送</el-button>
      <el-button v-if="showRankBalance" type="primary" @click="jumpRankBalance">排名均衡</el-button>
      <el-button v-if="showClassDispatch" type="primary" @click="terminateAndJumpDispatch">选科分班</el-button>
      <el-button v-else type="primary" @click="jumpDispatch">选科分班</el-button>
    </div>
    <el-dialog v-if="pushSettingFormVisible" :visible.sync="pushSettingFormVisible"
               :title="nextStepName+'设置'" width="350">
      <elective-generation-push-setting ref="settingForm"/>
      <div class="fx-row fx-end-cen mt15">
        <el-button @click="pushSettingFormVisible=false">取消</el-button>
        <el-button type="primary" @click="commitPushSetting">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  applyElectiveDMAlgorithm,
  flushIntoGenerationDM, jumpGenerationForceAdjust,
  jumpGenerationRankBalance, pushGenerationSetting,
  terminateGeneration
} from '@/api/webApi/elective/generation'
import config from '@/common/mx-config'
import ElectiveGenerationPushSetting from '@/views/elective/generation/components/elective-generation-push-setting'
import transferMixin from '@/components/mx-transfer-mixin'

export default {
  mixins: [transferMixin],
  name: 'elective-generation-commands',
  components: { ElectiveGenerationPushSetting },
  inject: ['refreshData'],
  props: ['chartBinding', 'disabled'],
  computed: {
    generation() {
      return this.chartBinding.generation || {}
    },
    status() {
      return this.generation?.status || {}
    },
    // buttons control
    showDMAlgorithm() {
      return this.status.allowDMAlgorithm
    },
    showDMAlgorithmResults() {
      return this.status.doneDMAlgorithm && !this.status.allowForce
    },
    showForceAdjust() {
      return this.status.doneDMAlgorithm && this.status.allowForce
    },
    showFastPush() {
      return this.status.enablePushNextDMGeneration
    },
    nextStepName() {
      const options = this.generation.options
      const next = this.generation.current + 1
      return Object.values(options).find(opt => opt.value == next).title
    },
    showForceAdjustSend() {
      return this.status.enableForceAdjustEntry
    },
    showSend() {
      return this.status.doneDMAlgorithm && !this.status.allMatched
    },
    showRankBalance() {
      const options = this.generation.options
      return this.status.allMatched && this.generation.current < options.rankBalance.value
    },
    showClassDispatch() {
      const options = this.generation.options
      return this.status.allMatched && this.generation.current <= options.rankBalance.value
    }
  },
  data() {
    return {
      pushSettingFormVisible: false
    }
  },
  methods: {
    notifyRootRefresh() {
      this.refreshData()
    },
    applyAlgorithm() {
      // 暂时只支持一种算法，不排队以后会支持多种
      const algorithm = config.electiveDMAlgorithm.rankFirst.value
      applyElectiveDMAlgorithm(algorithm).finally(() => {
        this.notifyRootRefresh()
      })
    },
    goDetailPage() {
      // 默认跳第1个可查询项 // 外抛让table子组件处理
      this.$emit('jumpDetail')
    },
    flushIntoDM() {
      flushIntoGenerationDM().finally(() => {
        this.notifyRootRefresh()
      })
    },
    pushGeneration() {
      // show dialog form for push settings
      // then call push api
      this.pushSettingFormVisible = true
    },
    async commitPushSetting() {
      const setting = await this.$refs.settingForm.validate()
      setting.roundId = this.status.roundId
      pushGenerationSetting(setting).finally(() => {
        this.notifyRootRefresh()
      })
    },
    jumpForceAdjust() {
      jumpGenerationForceAdjust().finally(() => {
        this.notifyRootRefresh()
      })
    },
    jumpRankBalance() {
      jumpGenerationRankBalance().finally(() => {
        this.notifyRootRefresh()
      })
    },
    terminateAndJumpDispatch() {
      terminateGeneration().then(res => {
        // this.refreshData()
        this.jumpDispatch()
      }).catch(_ => this.notifyRootRefresh())
    },
    jumpDispatch() {
      const path = '/evaluating/master-manage/permission/elective/dispatch/index'
      this.transferTo(path)
    }
  }
}
</script>

<style scoped>

</style>
