<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.AId==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.AssistName" ref="AssistName"
            class="col-6 q-pr-lg q-pb-lg" label="助教名称" :rules="[val => !!val || '请填写助教姓名']" />
          <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" emit-value
            map-options :rules="[val => !!val || '请选择所属校区']" @input="schoolChagne" />
          <q-input type="tel" filled stack-label maxlength="100" :dense="false" v-model="objOption.AssistTel"
            ref="AssistTel" class="col-6 q-pr-lg q-pb-lg" label="联系电话" :rules="[val => !!val || '请填写助教联系电话']" />
          <selectTree v-if="DeptList&&DeptList.length>0" :treeData='DeptList' :defaultArray="returnString"
            nodeKey="DeptId" :multiple="false" labelKey="DeptName" childrenKey="ChildList" tipText="选择部门"
            @getChild="getChild" classStr="col-6 q-pr-lg q-pb-lg"></selectTree>
          <q-select filled stack-label option-value="PostId" option-label="PostName" v-model="objOption.Post_Id"
            ref="Post_Id" :options="PostList" label="岗位" :dense="false" class="col-6 q-pb-lg q-pb-lg" emit-value
            map-options />
          <div class="col-6 q-pb-lg">
            <q-uploader style="display: inline-block;max-height: 320px;max-width: 100%; background-repeat:no-repeat"
              :style="{'background-image':'url(' + objOption.AssistIcon + ')'}" max-files="1" hide-upload-btn
              @rejected="onRejected" label="助教头像" :max-file-size="512*1024" accept=".jpg, image/*" auto-upload
              :factory="uploadFile" no-thumbnails>
            </q-uploader>
          </div>
        </div>
        <div class="text-caption q-my-md q-px-xs text-grey-6">助教介绍</div>
        <ext-editor :defaultMsg="objOption.AssistIntro" classStr="col-12" @getEditValue="getEditValue"></ext-editor>
      </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="closeSaveForm" />
        <q-btn label="立即提交" color="accent" class="q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveTeacher" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    getSchoolDropdown,
    saveAssist
  } from '../../../api/school/index'
  import {
    UploadSelfFile
  } from '../../../api/common/common'
  //部门
  import {
    getDeptTree
  } from '../../../api/system/dept'
  //岗位
  import {
    queryDeptPostList
  } from '../../../api/system/post'
  import extEditor from '../../common/ext-editor'
  import selectTree from '../../common/select-tree'
  export default {
    components: {
      extEditor,
      selectTree,
    },
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          AId: 0,
          School_Id: '',
          AssistName: "",
          AssistTel: '',
          AssistIcon: '',
          AssistIntro: '',
          Dept_Id: 0, //部门编号
          Post_Id: 0, //岗位编号
        },
        optionTitle: "",
        schoolList: [],
        saveLoading: false,
        DeptList: [], //部门列表
        PostList: [], //岗位列表
        returnString: [], //默认岗位
      }
    },
    created() {
      this.getSchool();
    },
    mounted() {
      this.initObj()
    },
    methods: {
      //学校改变
      schoolChagne() {
        this.objOption.Dept_Id = 0;
        this.returnString = [0];
        this.objOption.Post_Id = 0;
        this.PostList = [];
        this.queryDeptTree();
      },
      //部门改变
      deptChange() {
        this.objOption.Post_Id = 0;
        this.PostList = [];
      },
      getChild(deptArray) {
        var tempStr = "";
        if (deptArray && deptArray != '') {
          tempStr = deptArray;
        }
        this.objOption.Dept_Id = tempStr;
        if (this.saveObj) {
          if (this.objOption.Dept_Id != this.saveObj.Dept_Id) {
            this.deptChange();
            this.queryPostList();
          }
        } else {
          this.deptChange();
          this.queryPostList();
        }
      },
      //获取部门结构树
      queryDeptTree() {
        var msg = {
          School_Id: 0
        };
        if (this.objOption.School_Id && this.objOption.School_Id != '') {
          msg.School_Id = this.objOption.School_Id;
        }
        getDeptTree(msg).then(res => {
          if (res.Code == 1) {
            this.DeptList = res.Data;
          }
        })
      },
      //获取岗位列表
      queryPostList() {
        var postMsg = {
          QDeptIds: "",
        };
        if (this.objOption.Dept_Id) {
          postMsg.QDeptIds = this.objOption.Dept_Id;
        }
        queryDeptPostList(postMsg).then(res => {
          if (res.Code == 1) {
            this.PostList = res.Data;
            this.$forceUpdate();
          }
        })
      },
      //获取编辑器值
      getEditValue(obj) {
        this.objOption.AssistIntro = obj;
      },
      initObj() {
        if (this.saveObj) {
          this.optionTitle = "修改助教信息"
          this.objOption.AId = this.saveObj.AId;
          this.objOption.School_Id = this.saveObj.School_Id;
          this.objOption.AssistName = this.saveObj.AssistName;
          this.objOption.AssistTel = this.saveObj.AssistTel;
          this.objOption.AssistIcon = this.saveObj.AssistIcon;
          this.objOption.AssistIntro = this.saveObj.AssistIntro;
          this.objOption.Dept_Id = this.saveObj.Dept_Id;
          this.objOption.Post_Id = this.saveObj.Post_Id;
          if (this.objOption.School_Id) {
            this.queryDeptTree();
          }
          if (this.saveObj.Dept_Id) {
            this.queryPostList();
            this.returnString.push(this.saveObj.Dept_Id.toString());
          }
        } else {
          this.optionTitle = "新增助教"
        }
      },
      getSchool() {
        getSchoolDropdown({}).then(res => {
          this.schoolList = res.Data;
          this.$forceUpdate();
        })
      },
      onRejected(rejectedEntries) {
        this.$q.notify({
          type: 'negative',
          position: "top",
          message: `文件验证失败，请重新上传`
        })
      },
      uploadFile(files) {
        UploadSelfFile('assistIcon', files[0], res => {
          if (res.resultCode == 1) {
            this.objOption.AssistIcon = res.FileUrl;
          }
        })
      },
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      saveTeacher() {
        this.saveLoading = true
        this.$refs.School_Id.validate()
        this.$refs.AssistName.validate()
        this.$refs.AssistTel.validate()
        if (
          !this.$refs.School_Id.hasError &&
          !this.$refs.AssistName.hasError &&
          !this.$refs.AssistTel.hasError
        ) {
          let err_msg = ""
          if (this.objOption.AssistIcon == '') {
            err_msg = "请上传助教头像"
          }
          if (err_msg == "") {
            saveAssist(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
            })
          } else {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: err_msg
            })
            this.saveLoading = false
          }
        } else {
          this.saveLoading = false
        }
      },
    },
    watch: {},
  }

</script>

<style>
  .upload-assiatant-box .q-uploader__list {
    display: none;
  }

  .upload-assiatant-box .q-uploader {
    width: auto;
  }

</style>
