<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="80%"
  >
    <teacher-manage v-if="dialogVisible" :selectMode="true" v-model="selectedCode"
                    :headTeacherSettings="headTeacherSettings"></teacher-manage>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { saveTeacherSettings } from '@/api/webApi/elective/dispatch'
import TeacherManage from '@/views/permission/teacher-manage'

export default {
  // name: 'ChooseMasterTeacher',
  components: {
    TeacherManage
  },
  props: {
    roundId: '',
    headTeacherSettings: {}
  },
  data() {
    return {
      dialogVisible: false,
      selectedCode: '',
      activeModels: ''
    }
  },
  methods: {
    init(row) {
      this.dialogVisible = true
      this.activeModels = row
      this.selectedCode = row.newHeadteacherCode || row.headteacherCode
    },
    isRepeat() {
      // 判断是否重复？
      const headTeacherSettings = this.headTeacherSettings
      const format = Object.values(headTeacherSettings)
      const flag = format.some(item => {
        return item.customerCode == this.selectedCode
      })
      return flag
    },
    onConfirm() {
      // if (this.isRepeat()) {
      //   this.$message.warning(`该老师已分配给新班级`)
      //   return
      // }
      if (!this.selectedCode) {
        this.dialogVisible = false
        return
      }
      this.saveTeacherSettings()
    },
    saveTeacherSettings() {
      saveTeacherSettings({
        roundId: this.roundId,
        classId: this.activeModels.newClassId || this.activeModels.classId,
        customerCode: this.selectedCode || ''
      }).then(res => {
        this.$emit('completed')
        this.dialogVisible = false
      })
    }
  }
}
</script>
<style scoped>
::v-deep .el-dialog__body {
  padding: 0;
}
</style>
