<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">
        进入{{ generation.options.forceAdjust.title }}
      </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>-->
      <!--<el-button type="primary" @click="jumpReport" class="ml10">选科数据</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.enablePushForceAdjust
    },
    showSend() {
      return this.status.doneDMAlgorithm && !this.status.allEnrolled
    },
    showRankBalance() {
      return false // TODO: not implement.
      const options = this.generation.options
      return this.status.allEnrolled && this.generation.current < options.rankBalance.value
    },
    showClassDispatch() {
      const options = this.generation.options
      // return this.status.allEnrolled && this.generation.current <= options.rankBalance.value
      return this.status.allEnrolled && this.generation.current >= options.forceAdjust.value
    }
  },
  data() {
    return {
      pushSettingFormVisible: false
    }
  },
  methods: {
    notifyRootRefresh(syncStep = false) {
      this.refreshData(syncStep)
    },
    applyAlgorithm() {
      // 暂时只支持一种算法，不排队以后会支持多种
      const algorithm = config.electiveDMAlgorithm.rankFirst.value
      applyElectiveDMAlgorithm(algorithm).finally(() => {
        this.notifyRootRefresh()
      })
    },
    goDetailPage() {
      // 默认跳第1个可查询项 // 外抛让table子组件处理
      this.$emit('jumpDetail')
    },
    flushIntoDM() {
      flushIntoGenerationDM().finally(() => {
        this.notifyRootRefresh(true)
      })
    },
    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(true)
      })
    },
    jumpForceAdjust() {
      jumpGenerationForceAdjust().finally(() => {
        this.notifyRootRefresh(true)
      })
    },
    async jumpRankBalance() {
      jumpGenerationRankBalance().finally(() => {
        this.notifyRootRefresh(true)
      })
    },
    async terminateAndJumpDispatch() {
      await this.$confirm('确认进入选科分班？该操作将锁定所有选科录取状态，且不可逆')
      terminateGeneration().then(res => {
        this.jumpDispatch()
      }).finally(() => this.notifyRootRefresh())
    },
    jumpDispatch() {
      const path = '/new-gaokao/selectCourse/elective/dispatch/index'
      this.transferTo(path)
    },
    jumpReport() {
      // 跳转选科数据
      // TODO: 页面还没有定义好
      this.msgInfo('更多选科数据，敬请期待')
    }
  }
}
</script>

<style scoped>

</style>
