<template>
  <q-dialog v-model="persistent" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.MenuId==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-input filled stack-label maxlength="20" :dense="false" v-model="objOption.DeptName" ref="DeptName"
            class="col-6 q-pr-lg q-pb-lg" label="部门名称" :rules="[val => !!val || '请填写部门名称']" />
          <q-input filled stack-label maxlength="50" :dense="false" v-model="objOption.DeptTel" ref="DeptTel"
            class="col-6 q-pr-lg q-pb-lg" label="部门电话" />
          <selectTree v-if="DeptTreeList&&DeptTreeList.length>0" :treeData='DeptTreeList' :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 :option-value="(item) => item === null ? null : item.MenuId" option-label="MenuName"
            v-model="objOption.ParentId" :options="PersionList" label="部门负责人" :dense="false" emit-value map-options
            class="col-6 q-pr-lg q-pb-lg" />
          <div class="col-6 q-pr-lg q-pb-lg q-pt-lg">
            <q-toggle size="md" label="状态" color="primary" :false-value="1" :true-value="0"
              v-model="objOption.Status" />
            <div class="text-grey-6 text-caption">注意：关闭后，部门将无法正常使用.</div>
          </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 q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveDept" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import selectTree from '../common/select-tree'
  import {
    getDeptTree,
    saveDeptInfo,
    getDeptInfo
  } from '../../api/system/dept'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    components: {
      selectTree
    },
    data() {
      return {
        persistent: true,
        objOption: {
          DeptId: 0, //部门编号
          ParentId: 0, //上级部门编号
          DeptName: "", //部门名称
          DeptTel: "", //部门联系电话
          ManagerIds: "", //部门负责人编号
          Status: 0, //状态(0-正常,1-禁用),
        },
        optionTitle: "",
        returnString: [],
        //部门树形列表
        DeptTreeList: [],
        saveLoading: false,
        PersionList: [],
      }
    },
    created() {
      this.queryDeptTree();
    },
    mounted() {
      this.initObj()
    },
    methods: {
      getChild(menuArray) {
        var tempStr = "";
        if (menuArray && menuArray != '') {
          tempStr = menuArray;
        }
        this.objOption.ParentId = tempStr;
      },
      //获取部门树形结构
      queryDeptTree() {
        getDeptTree({}).then(res => {
          if (res.Code == 1) {
            this.DeptTreeList = res.Data;
          }
        })
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.DeptId > 0) {
          getDeptInfo({
            DeptId: this.saveObj.DeptId
          }).then(res => {
            this.objOption.DeptId = res.Data.DeptId;
            this.objOption.ParentId = res.Data.ParentId.toString();
            this.returnString.push(this.objOption.ParentId);
            this.objOption.DeptName = res.Data.DeptName;
            this.objOption.DeptTel = res.Data.DeptTel;
            this.objOption.ManagerIds = res.Data.ManagerIds;
            this.objOption.Status = res.Data.Status;
          })
          this.optionTitle = "修改部门信息"
        } else {
          this.optionTitle = "新增部门"
          this.objOption.DeptId = 0;
          this.objOption.ParentId = 0;
          this.objOption.DeptName = "";
          this.objOption.DeptTel = "";
          this.objOption.ManagerIds = "";
          this.objOption.Status = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存部门
      saveDept() {
        this.saveLoading = true
        saveDeptInfo(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>
