<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.TId==0?'新增教室信息':'修改教室信息'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-lg q-px-md text-grey-6">教室信息</div>
        <div class="row wrap">
          <q-select filled stack-label option-value="SId" option-label="SName" v-model="objOption.School_Id"
            ref="School_Id" :options="schoolList" label="所属校区" :dense="false" class="col-6 q-pb-lg q-pr-lg" emit-value
            map-options :rules="[val => !!val || '请选择所属校区']" />
          <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.RoomName" ref="RoomName"
            class="col-6 q-pr-lg" label="教室名称" :rules="[val => !!val || '请填写教室名称']" />
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeClassRoomSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveClassRoom" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    getSchoolDropdown,
    quertClassRoomInfo,
    saveClassRoom
  } from '../../../api/school/index'

  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          RoomId: 0,
          RoomName: "", //教室名称
          School_Id: 0, //校区编号
          Status:-1,//状态
        },
        optionTitle: "",
        schoolList: [],
        saveLoading: false,
      }
    },
    created() {
      this.getSchool();
    },
    mounted() {
      this.initObj();
    },
    methods: {
      initObj() {
        if (this.saveObj && this.saveObj.RoomId > 0) {
          this.optionTitle = "修改教室信息";
          quertClassRoomInfo({
            RoomId: this.saveObj.RoomId
          }).then(res => {
            if (res.Code == 1) {
              this.objOption.RoomId = res.Data.RoomId;
              this.objOption.RoomName = res.Data.RoomName;
              this.objOption.School_Id = res.Data.School_Id;
            }
          });
        } else {
          this.optionTitle = "新增教室";
          this.objOption.RoomId = 0;
          this.objOption.RoomName = "";
          this.objOption.School_Id = 0;
        }
      },
      //获取校区列表
      getSchool() {
        getSchoolDropdown({}).then(res => {
          var tempArray = res.Data;
          if(!tempArray)
          {
              tempArray=[];
          }
          var obj = {
            SName: '不限',
            SId: 0
          }
          tempArray.unshift(obj);
          this.schoolList = tempArray;
        })
      },
      closeClassRoomSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      saveClassRoom() {
        this.saveLoading = true
        this.$refs.School_Id.validate()
        this.$refs.RoomName.validate()
        if (
          !this.$refs.School_Id.hasError &&
          !this.$refs.RoomName.hasError
        ) {
          saveClassRoom(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.closeClassRoomSaveForm()
          }).catch(() => {
            this.saveLoading = false
          })
        } else {
          this.saveLoading = false
        }
      },
    }
  }

</script>

<style>
</style>
