<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.ClassId==0?'新增班级信息':'修改班级信息'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-md q-px-xs text-grey-6">基本资料</div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.ClassName" ref="ClassName"
            class="col-6 q-pr-lg q-pb-lg" label="班级名称" :rules="[val => !!val || '请填写班级名称']" />
          <q-select filled stack-label option-value="CourseId" option-label="CourseName" v-model="objOption.CouseId"
            ref="CouseId" :options="courseList" label="所属课程" :dense="false" class="col-6 q-pb-lg" emit-value map-options
            :rules="[val => !!val || '请选择所属课程']" />
          <q-select filled stack-label option-value="TId" option-label="TeacherName" v-model="objOption.Teacher_Id"
            ref="Teacher_Id" :options="teacherList" label="讲师" :dense="false" class="col-6 q-pr-lg q-pb-lg" emit-value
            map-options :rules="[val => !!val || '请选择讲师']" />
          <q-select filled stack-label option-value="AId" option-label="AssistName" v-model="objOption.Assist_Id"
            ref="Assist_Id" :options="assistList" label="助教" :dense="false" class="col-6 q-pb-lg" emit-value map-options
            :rules="[val => !!val || '请选择助教']" />
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" color="dark" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent" class="q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveClassInfo" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    getTeacherDropDownList,
    getAssistDropDownList,
    saveClass,
    getClassInfo
  } from '../../../api/school/index'
  import {
    queryCourseDropdownList
  } from '../../../api/course/index'

  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          ClassId: 0, //班级编号
          ClassName: '', //班级名称
          CouseId: 0, //所属课程
          Teacher_Id: 0, //讲师编号
          Assist_Id: 0, //助教编号
        },
        optionTitle: "",
        courseList: [],
        teacherList: [],
        assistList: [],
        saveLoading: false,
      }
    },
    created() {
      this.getCourseDropdown();
      this.getTeacherDropdown();
      this.getAssistantDropdown();
    },
    mounted() {
      this.initObj()
    },
    methods: {
      initObj() {
        if (this.saveObj) {
          this.optionTitle = "修改班级信息"
          if (this.saveObj.ClassId > 0) {
            getClassInfo({
              ClassId: this.saveObj.ClassId
            }).then(res => {
              this.objOption.ClassId = res.Data.ClassId;
              this.objOption.ClassName = res.Data.ClassName;
              this.objOption.CouseId = res.Data.CouseId;
              this.objOption.Teacher_Id = res.Data.Teacher_Id;
              this.objOption.Assist_Id = res.Data.Assist_Id;
              this.$forceUpdate();
            })
          }
        } else {
          this.optionTitle = "新增班级"
          this.objOption.ClassId = 0;
          this.objOption.ClassName = "";
          this.objOption.CouseId = 0;
          this.objOption.Teacher_Id = 0;
          this.objOption.Assist_Id = 0;
        }
      },
      //获取课程下拉列表
      getCourseDropdown() {
        queryCourseDropdownList({}).then(res => {
          if (res.Code == 1) {
            this.courseList = res.Data;
          }
        });
      },
      //获取讲师下拉列表
      getTeacherDropdown() {
        getTeacherDropDownList({}).then(res => {
          if (res.Code == 1) {
            this.teacherList = res.Data;
          }
        })
      },
      //获取助教下拉列表
      getAssistantDropdown() {
        getAssistDropDownList({}).then(res => {
          if (res.Code == 1) {
            this.assistList = res.Data;
          }
        });
      },
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      saveClassInfo() {
        this.saveLoading = true
        saveClass(this.objOption).then(res => {
          this.saveLoading = false
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
          this.$emit("success")
          this.closeSaveForm()
        }).catch(() => {
          this.saveLoading = false
        })
      },
    },
  }

</script>
