<template>
  <q-dialog v-model="persistent" persistent content-class="bg-grey-1" 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">
          <div class="col-6">
            <selectSearch classStr="col-12 q-pr-lg q-pb-lg" :Data='schoolList' optionValue="SId" optionLabel="SName"
              :densetype="false" :vModel="objOption.School_Id" label="所属校区" @returnData="returnData" :useInput="true"
              :reactiveRules="false">
            </selectSearch>
          </div>
          <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-select filled stack-label option-value="value" option-label="label" v-model="objOption.DeptTier"
            ref="DeptTier" :options="DeptTierList" label="部门层级" :dense="false" class="col-6 q-pr-lg q-pb-lg" emit-value
            map-options :rules="[val => !!val || '部门层级']" />
          <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 class="col-6 q-pb-lg q-pr-lg" multiple clearable filled stack-label use-input use-chips
            option-value="Id" option-label="EmployeeName" v-model="tempManager" ref="ManagerId" :options="PersionList"
            label="部门负责人" :dense="false" emit-value map-options @filter="filterFn">
            <template v-slot:no-option>
              <q-item>
                <q-item-section class="text-grey">
                  未找到相关数据
                </q-item-section>
              </q-item>
            </template>
          </q-select>
          <q-input filled stack-label maxlength="30" :dense="false" v-model="objOption.DeptTel" ref="DeptTel"
            class="col-6 q-pr-lg q-pb-lg" label="部门电话" />
          <q-input filled stack-label maxlength="10" :dense="false" v-model="objOption.DeptSort" ref="DeptSort"
            class="col-6 q-pr-lg q-pb-lg" label="排序" />
          <q-select filled stack-label option-value="value" option-label="label" v-model="objOption.IsCompany "
            ref="DeptTier" :options="IsCompanyL" label="是否校区" :dense="false" class="col-6 q-pr-lg q-pb-lg" emit-value
            map-options />
          <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>
          <q-select class="col-6 q-pb-lg q-pr-lg" multiple clearable filled stack-label use-input use-chips
            option-value="PostId" option-label="PostName" v-model="tempPost" ref="PostId" :options="PostList"
            label="关联岗位" :dense="false" emit-value map-options>
          </q-select>
        </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="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 selectSearch from '../../components/common/select-search'

  import {
    getDeptTree,
    saveDeptInfo,
    getDeptInfo
  } from '../../api/system/dept'
  import {
    getSchoolDropdown,
  } from '../../api/school/index'
  import {
    getDeptTierList
  } from '../../api/system/index'
  import {
    queryEmployee
  } from '../../api/users/user'
  import {
    getPostList
  } from '../../api/system/post'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    components: {
      selectTree,
      selectSearch
    },
    data() {
      return {
        persistent: true,
        objOption: {
          DeptId: 0, //部门编号
          ParentId: 0, //上级部门编号
          DeptName: "", //部门名称
          DeptTel: "", //部门联系电话
          ManagerIds: "", //部门负责人编号
          Status: 0, //状态(0-正常,1-禁用),
          School_Id: 0, //校区编号
          DeptTier: 0, //部门层级
          DeptSort: 0, //部门排序
          IsCompany: 0, //是否校区1是  0否
        },
        optionTitle: "",
        returnString: [], //部门树默认值
        DeptTreeList: [], //部门树形列表
        saveLoading: false,
        PersionList: [], //员工列表
        AllemployeeList: [], //所有员工
        DeptTierList: [], //部门层级列表
        schoolList: [], //校区列表
        tempManager: [], //负责人编号数组
        IsCompanyL: [{
            label: '是',
            value: 1
          },
          {
            label: '否',
            value: 0
          },
        ],
        PostList: [], //岗位列表
        tempPost: [], //选中的岗位列表
      }
    },
    created() {
      this.queryPostList();
      this.queryDeptTier();
      this.queryDeptTree();
      this.getEmployee();
      this.getSchool();
    },
    mounted() {
      this.initObj()
    },
    methods: {
      //获取岗位列表
      queryPostList() {
        getPostList({}).then(res => {
          if (res.Code == 1) {
            this.PostList = res.Data;
          }
        })
      },
      //筛选员工
      filterFn(val, update) {
        update(() => {
          if (val === '') {
            this.PersionList = JSON.parse(JSON.stringify(this.AllemployeeList))
          } else {
            const needle = val.toLowerCase()
            this.PersionList = this.AllemployeeList.filter(v => v.EmployeeName.toLowerCase().indexOf(needle) > -1)
          }
        })
      },
      //获取员工列表
      getEmployee() {
        var qMsg = {
          EmployeeName: ""
        }
        queryEmployee(qMsg).then(res => {
          if (res.Code == 1) {
            var jsonData = res.Data;
            if (jsonData && jsonData.length > 0) {
              this.AllemployeeList = JSON.parse(JSON.stringify(jsonData));
              this.PersionList = JSON.parse(JSON.stringify(jsonData));;
            }
          }
        })
      },
      //部门层级列表
      queryDeptTier() {
        this.DeptTierList = getDeptTierList();
      },
      //获取校区
      getSchool() {
        getSchoolDropdown({}).then(res => {
          if (res.Code == 1) {
            var tempSchoolArray = res.Data;
            if (!tempSchoolArray) {
              tempSchoolArray = [];
            }
            tempSchoolArray.unshift({
              SId: 0,
              SName: "不限"
            });
            this.schoolList = tempSchoolArray;
            this.$forceUpdate();
          }
        })
      },
      //获取选中的部门
      getChild(menuArray) {
        var tempStr = "0";
        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.objOption.DeptSort = res.Data.DeptSort;
            this.objOption.DeptTier = res.Data.DeptTier;
            this.objOption.School_Id = res.Data.School_Id;
            this.tempManager = res.Data.ManagerList;
            this.tempPost = res.Data.ChoosePostList;
          })
          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;
          this.objOption.DeptSort = 0;
          this.objOption.DeptTier = 0;
          this.objOption.School_Id = 0;
          this.tempManager = [];
          this.tempPost = [];
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false;
      },
      //保存部门
      saveDept() {
        if (this.objOption.IsCompany == 1 && this.objOption.School_Id == 0) {
          this.$q.notify({
            type: 'warning',
            position: 'center',
            timeout: 1500,
            message: `请选择所属校区`
          })
          return
        }
        this.saveLoading = true;
        var tempStr = "";
        if (this.tempManager && this.tempManager.length > 0) {
          this.tempManager.forEach(item => {
            tempStr += "," + item;
          });
        }
        if (tempStr && tempStr != '') {
          //去掉第一个字符
          tempStr = tempStr.substr(1);
        }
        this.objOption.ChoosePostList = this.tempPost;
        this.objOption.ManagerIds = tempStr;
        saveDeptInfo(this.objOption).then(res => {
          this.saveLoading = false
          if (res.Code == 1) {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            this.$emit("success")
          }
          this.closeSaveForm()
        }).catch(() => {
          this.saveLoading = false
        })
      },
      returnData(data) {
        this.objOption.School_Id = data;
      }
    },
  }

</script>
