<template>
  <div>
    <xy-dialog ref="dialog" :width="30" :is-show.sync="isShow" :type="'form'" title="设定班主任"
      :form="form" :rules='rules' @submit="submit">
      <template v-slot:teacher_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>班主任：
          </div>
          <div class="xy-table-item-content">
            <el-select @change="changeTeachers" v-model="teachers" multiple style="width:100%" placeholder="请选择班主任" clearable>
              <el-option v-for="item in teacher_options" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>

    </xy-dialog>

  </div>
</template>

<script>

  import {show,save} from "@/api/course/index.js"
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        id: '',
        teacher_options: [],
        form: {
          teacher_id: '',
        },
        teachers:[],
        rules: {
          teacher_id: [{
            required: true,
            message: '请选择班主任'
          }]
        }
      }
    },
    created() {
    },
    methods: {
      setTeachers(e){
        this.teacher_options = e
      },
      changeTeachers(e){
        this.teachers = e
        this.form.teacher_id = this.teachers.join(",")
      },
      submit() {
        if(this.teachers.length>0){
          this.form.teacher_id = this.teachers.join(",")
        }
        save({
          ...this.form
        }).then(res => {
          this.$message({
            type: 'success',
            message: '设置班主任成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      getDetail() {
        show({
          id: this.id
        }).then(res => {
          this.form = this.base.deepCopy(res, this.form)
          res.teacher_detail.map(item=>{
            this.teachers.push(item.id)
          })
          // this.teachers = this.form.teacher_id?this.form.teacher_id.split(","):[],
          // this.$nextTick(function(){
          //   this.teachers = this.form.teacher_id?this.form.teacher_id.split(","):[]
          // })
        })
      },
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
            this.getDetail()
        } else {
          this.id = ''
          this.teachers = []
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .teacher_id{
    flex-basis: 100%;
  }
</style>
