<style>

</style>
<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 500px;max-width:500px;">
      <q-card-section>
        <div class="text-h6">设置班级状态</div>
      </q-card-section>
      <div class="col row wrap q-mr-lg q-col-gutter-md" style="margin:20px 0;">
        <div class="col-12">
          <q-select filled option-value="Id" option-label="Name" class="q-pr-lg" v-model="statusMsg.ClassStatus"
            :options="classStatusList" emit-value map-options label="班级状态" />
        </div>
        <div class="col-12" v-if="setingObj.Teacher_Id==0&&statusMsg.ClassStatus==2">
          <q-select filled option-value="TId" option-label="TeacherName" class="q-pr-lg" v-model="statusMsg.Teacher_Id"
            :options="TeacherList" emit-value map-options label="带班老师" />
        </div>
        <div class="col-12" v-if="setingObj.ClassRoomId==0&&statusMsg.ClassStatus==2">
          <q-select filled stack-label option-value="RoomId" option-label="RoomName" v-model="statusMsg.ClassRoomId"
            ref="ClassRoomId" :options="ClassRoomList" label="关联教室" :dense="false" class="col-6 q-pb-lg" emit-value
            map-options />
        </div>
      </div>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeClassSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="setClassStatus(statusMsg.ClassStatus)" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    queryClassStatusList,
    saveClassStatus, //修改班级状态
  } from '../../api/course/class'
  import {
    getTeacherDropDownList,
    queryClassRoomList,
  } from '../../api/school/index';

  export default {
    props: {
      setingObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        saveLoading: false,
        statusMsg: {
          ClassId: 0,
          ClassStatus: 0,
          Teacher_Id: 0,
          ClassRoomId: 0
        },
        classStatusList: [],
        TeacherList: [],
        ClassRoomList: []
      }
    },
    created() {

    },
    watch: {

    },
    mounted() {
      this.statusMsg.ClassId = this.setingObj.ClassId;
      this.statusMsg.ClassStatus = this.setingObj.ClassStatus;

      this.getClassStatus();
      this.GetTeacherList();
      this.getClassRoomList();
    },
    methods: {
      //获取班级状态列表
      getClassStatus() {
        queryClassStatusList({}).then(res => {
          if (res.Code == 1) {
            this.classStatusList = [];
            let TempData = res.Data;
            TempData.forEach(x => {
              if (x.Id >= this.setingObj.ClassStatus) {
                this.classStatusList.push(x)
              }
            })
          }
        }).catch(() => {})
      },
      //获取教室下拉
      getClassRoomList() {
        queryClassRoomList({}).then(res => {
          if (res.Code == 1) {
            this.ClassRoomList = res.Data;
            this.ClassRoomList.unshift({
              RoomId: 0,
              RoomName: "不限"
            })
          }
        })
      },
      //获取教师下拉
      GetTeacherList() {
        getTeacherDropDownList({}).then(res => {
          if (res.Code == 1) {
            this.TeacherList = res.Data;
            this.TeacherList.unshift({
              TId: 0,
              TeacherName: "不限"
            })
          }
        })
      },
      closeClassSaveForm() {
        this.$emit('close');
        this.persistent = false;
      },
      //更新班级状态
      setClassStatus(ClassStatus) {
        if (this.setingObj.Teacher_Id == 0 && ClassStatus == 2) {
          if (this.statusMsg.Teacher_Id == 0) {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请选择上课老师`
            })
            return
          }
        }
        if (this.setingObj.ClassRoomId == 0 && ClassStatus == 2) {
          if (this.statusMsg.ClassRoomId == 0) {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请选择上课教室`
            })
            return
          }
        }
        this.saveLoading = true;
        saveClassStatus(this.statusMsg).then(res => {
          if (res.Code == 1) {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '设置成功!',
              position: 'top'
            })
            this.$emit('success');
            this.closeClassSaveForm();
            if (ClassStatus == 3 && res.Message != '') {
              this.$q.dialog({
                title: '提示信息',
                message: "注意，该班级下面任然存在未消耗完课时的学员，请核对学员课时，进行补课处理",
                cancel: true,
                persistent: true,
                ok: "查看名单",
                cancel: "我知道了",
              }).onOk(() => {
                this.OpenNewUrl('/stuMan/unfinishedStudent', {
                  ClassId: this.statusMsg.ClassId
                })
              }).onCancel(() => {

              });
            }
            if (ClassStatus == 2 && res.Data) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '数据保存成功！但存在上课冲突数据,请调整。',
                position: 'top'
              })
              this.$emit('getClassFlict', this.statusMsg.ClassId);
            }
          } else {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: res.Message,
              position: 'top'
            })
          }
          this.saveLoading = false;
        });
      },
    }
  }

</script>
