<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.StuId==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.MName" ref="MName"
            class="col-6 q-pr-lg q-pb-lg" label="管理者名称" :rules="[val => !!val || '请填写管理者姓名']" />
          <q-input type="tel" filled stack-label maxlength="100" :dense="false" v-model="objOption.MTel" ref="MTel"
            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.MHead + ')'}" 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>
      </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="setStudent" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
<script>
  import {
    getSchoolDropdown,
    saveManager,
    queryManagerInfo
  } from '../../../api/school/index'
  //部门
  import {
    getDeptTree
  } from '../../../api/system/dept'
  //岗位
  import {
    getPostList
  } from '../../../api/system/post'
  import {
    UploadSelfFile,
  } from '../../../api/common/common'
  import selectTree from '../../common/select-tree'
  export default {
    components: {},
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    components: {
      selectTree
    },
    data() {
      return {
        persistent: true,
        objOption: {
          MId: 0, //管理者编号
          School_Id: 0, //学校编号
          MName: "", //管理者姓名
          MTel: '', //管理者电话
          MHead: '', //头像
          RoleAuth: "", //角色
          Dept_Id: 0, //部门编号
          Post_Id: 0, //岗位编号
        },
        optionTitle: "",
        schoolList: [],
        saveLoading: false,
        DeptList: [], //部门列表
        PostList: [], //岗位列表
        returnString: [], //默认岗位
      }
    },
    created() {
      this.queryDeptTree();
      this.queryPostList();
      this.getSchool();
    },
    mounted() {
      this.initObj();
    },
    methods: {
      getChild(deptArray) {
        var tempStr = "";
        if (deptArray && deptArray != '') {
          tempStr = deptArray;
        }
        this.objOption.Dept_Id = tempStr;
      },
      //获取部门结构树
      queryDeptTree() {
        getDeptTree({}).then(res => {
          if (res.Code == 1) {
            this.DeptList = res.Data;
          }
        })
      },
      //获取岗位列表
      queryPostList() {
        this.PostList = [];
        var postMsg = {
          RB_Dept_Id: 0,
        };
        if (this.objOption.Dept_Id) {
          postMsg.RB_Dept_Id = this.objOption.Dept_Id;
        }
        getPostList(postMsg).then(res => {
          if (res.Code == 1) {
            this.PostList = res.Data;
          }
        })
      },
      initObj() {
        if (this.saveObj) {
          this.optionTitle = "修改管理者信息"
          queryManagerInfo({
            MId: this.saveObj.MId
          }).then(res => {
            this.objOption.MId = res.Data.MId;
            this.objOption.School_Id = res.Data.School_Id;
            this.objOption.MName = res.Data.MName;
            this.objOption.MTel = res.Data.MTel;
            this.objOption.MHead = res.Data.MHead;
            this.objOption.Post_Id = res.Data.Post_Id;
            this.objOption.Dept_Id = res.Data.Dept_Id;
            this.returnString.push(res.Data.Dept_Id.toString());
            this.tempRole = res.Data.RoleList;
            if (this.objOption.Dept_Id && this.objOption.Dept_Id > 0) {
              this.queryPostList();
            }
            this.$forceUpdate();
          })
        } 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('studentIcon', files[0], res => {
          if (res.resultCode == 1) {
            this.objOption.MHead = res.FileUrl;
          }
        })
      },
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      setStudent() {
        var authStr = "";
        if (this.tempRole && this.tempRole.length > 0) {
          this.tempRole.forEach(item => {
            authStr += "," + item;
          })
        }
        if (authStr != '') {
          authStr = authStr.substring(1, authStr.length);
        }
        this.objOption.RoleAuth = authStr;
        this.saveLoading = true
        saveManager(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
        })
      },
    },
    watch: {
      "objOption.Dept_Id": function (val) {
        this.queryPostList();
      }
    }
  }

</script>

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

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

</style>
