<template>
  <div class="app-container">
    <el-card class="box-card" style="margin-bottom: 10px;">
      <mx-condition ref="condition" :query-params="queryParams" :require-fields="requireFields" @query="handleQuery"
                    @invalid="handleInvalidQuery"
      ></mx-condition>
    </el-card>
    <p class="text-right mb10"><el-button @click="lockDispatch" type="primary">发布分班</el-button></p>
    <dispatch-table :loading="loading" :round="round" :settings="settings"></dispatch-table>
  </div>
</template>
<script>
import DispatchTable from "./components/dispatch-table.vue";
import { getRound,getSettings } from '@/api/webApi/elective/dispatch'
import MxCondition from '@/components/MxCondition/mx-condition'

export default {
  components: {
    DispatchTable,
    MxCondition
  },
  data() {
    return {
      requireFields: ['year', 'roundId'],
      queryParams: {
        year: '',
        roundId: ''
      },
      loading: false,
      settings: [],
      round: { },
    };
  },
  provide() {
    return {
      refreshData: this.handleQuery,
      parentQuery: this.queryParams
    }
  },
  methods: {
    handleQuery() {
      this.getRound()
      this.getSettings()
    },
    lockDispatch() {
      // 班级数累计
      const classCounts = this.round.roundGroups.reduce((prev,cur) => {
        return prev += cur.classCount
      },0)
      console.log(classCounts)
      if(classCounts > this.settings.length){
        this.$message.warning('还有组合未分班')
        return
      }

    },
    // 获取批次的setting
    getSettings() {
      getSettings({
        roundId:this.queryParams.roundId,
      }).then(res => {
        console.log(res)
        this.settings = res.data
      })
    },
    // 获取批次的组合
    getRound() {
      this.loading = true
      getRound({
        year:this.queryParams.year,
        round:this.queryParams.roundId,
      }).then(res => {
        this.round = res.data
        console.log(res)
      }).finally((res) => {
        this.loading= false
      })
    },
    handleInvalidQuery() {
      console.log('query取消')
      this.round = {}
    },
  },
};
</script>
<style scoped>
</style>
