<template>
  <div>
    <el-dialog title="新建班级" :visible.sync="open" width="700px">
      <el-form ref="form" v-loading="loadingChange" :model="form" :rules="rules" label-width="120px" class="add-form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择课程:" prop="courseId">
              <el-button v-if="chooseCourseInfo.courseId === undefined" type="primary" plain icon="el-icon-reading" size="small" @click="chooseCourse">选择课程</el-button>
              <el-input v-else v-model="chooseCourseInfo.courseName" disabled readonly>
                <el-button v-if="canChangeCourse" slot="append" icon="el-icon-reading" @click="chooseCourse" >重选</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班级名称:" prop="claName">
              <div class="input-with-color-picker-container">
                <el-input v-model="form.claName" placeholder="请输入班级名称" />
                  <el-tooltip class="item" effect="dark" content="选择班级颜色,便于课表中区分各班级" placement="right">
                    <el-color-picker v-model="form.claColor" :predefine="colorArray" />
                  </el-tooltip>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任课教练:" prop="staffId">
              <staff-select v-model="form.staffId" :dept-id="chooseDepartId"  placeholder="请选择上课教练" />

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="招生状态:" prop="recruitStatus">
              <el-select
                v-model="form.recruitStatus"
                placeholder="请选择招生状态"
                clearable
                filterable
                default-first-option
                :loading="loadingSelect"
              >
                <el-option
                  v-for="dict in recruitStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="满班人数:" prop="capacity">
              <el-input-number v-model="form.capacity" controls-position="right" :min="0" placeholder="请输入满班人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开班日期:" prop="openDate">
              <el-date-picker
                v-model="form.openDate"
                clearable
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择开班日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="memo">
              <el-input v-model="form.memo" type="textarea" placeholder="请输入备注内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结班日期:" prop="closeDate">
              <el-date-picker
                v-model="form.closeDate"
                clearable
                size="medium"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择结班日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loadingChange" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <choose-course ref="chooseCourse" @chooseComplete="chooseCourseComplete" />
  </div>
</template>
<script>
import chooseCourse from '@/components/sc/course/chooseCourse'
import { addCla, updateCla } from '@/api/school/sc/cla'
import staffSelect from '@/components/system/staff/staffSelect'
import moment from 'moment'
export default {
  components: {
    staffSelect,
    chooseCourse
  },
  props: {
    colorArray: {
      type: Array,
      default: function() {
        return ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C']
      }
    }
  },
  data() {
    return {
      open: false,
      loadingChange: false,
      loadingSelect: false,
      form: {
        openDate: undefined,
        claColor: '#409EFF',
        capacity:20,
        recruitStatus:'1'
      },
      rules: {
        courseId: [
          { required: true, message: '请选择课程', trigger: 'blur' }
        ],
        claName: [
          { required: true, message: '请输入班级名称', trigger: 'blur' }
        ],
      },
      recruitStatusOptions: [],
      // 已选择的课程信息
      chooseCourseInfo: {},
      // 是否允许变更所属课程
      canChangeCourse: true,
      // 已选择的校区
      chooseDepartId: undefined
    }
  },
  created() {
    this.recruitStatusOptions = [
      {
        "dictLabel": "开放",
        "dictValue": "1"
      },
      {
        "dictLabel": "满班后停止",
        "dictValue": "2"
      },
      {
        "dictLabel": "停止",
        "dictValue": "0"
      }
    ]
  },
  methods: {
    submitForm: function() {
      this.form.courseId = this.chooseCourseInfo.courseId
      this.form.departId = this.chooseDepartId
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.claId !== undefined) {
            updateCla(this.form).then(response => {
              this.loadingChange = false
              if (response.respCode === '0000') {
                this.msgSuccess('修改成功')
                this.open = false
                this.$emit('ok')
              } else {
                this.msgError(response.respMsg)
              }
            }).catch(() => {
              this.loadingChange = false
            })
          } else {
            this.loadingChange = true
            addCla(this.form).then(response => {
              this.loadingChange = false
              if (response.respCode === '0000') {
                this.msgSuccess('新增成功')
                this.open = false
                this.$emit('ok')
              } else {
                this.msgError(response.respMsg)
              }
            }).catch(() => {
              this.loadingChange = false
            })
          }
        }
      })
    },
    // 选择课程
    chooseCourse: function() {
      this.$refs.chooseCourse.open = true
    },
    chooseCourseComplete({ courseInfo, departId }) {
      this.chooseCourseInfo = courseInfo
      this.chooseDepartId = departId
    },
    reset() {
      this.chooseCourseInfo = {}
      this.chooseDepartId = undefined
      this.form = {
        openDate: moment(new Date()).format('YYYY-MM-DD'),
        claColor: '#409EFF',
        capacity:20,
        recruitStatus:'1'
      }
      this.resetForm('form')
    }
  }
}
</script>
