import {
  observable,
  action
} from 'mobx';
import {
  WeaForm
} from 'comsMobx';
import * as APIOrg from '../apis/organization';
import * as APIOrgVirtual from '../apis/organizationvirtual';
import {
  message,
  Modal
} from 'antd';
import {
  WeaTableNew
} from 'comsMobx'
import * as mobx from 'mobx';
import isEmpty from 'lodash/isEmpty';
import findIndex from 'lodash/findIndex';
import {
  WeaLogView
} from 'comsMobx'
const toJS = mobx.toJS;
const {
  TableStore
} = WeaTableNew;
const confirm = Modal.confirm;
const {
  LogStore
} = WeaLogView;
import {
  i18n
} from '../public/i18n';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export class HrmOrganization {
  @observable hideSaveBtn=false;
  @observable logStore = new LogStore();
  @observable parentId = "";
  @observable parentTitle = "";
  @observable title = "";
  @observable dialogTitle = "";
  @observable verified = false;
  @observable hasRight = false;
  @observable form = new WeaForm();
  @observable formView = new WeaForm();
  @observable formCondition = new WeaForm();
  @observable table = new TableStore();
  @observable condition = [];
  @observable showSearchAd = false;
  @observable searchParamsAd = {};
  @observable formFieldView = [];
  @observable formField = [];
  @observable loading = false;
  @observable type = 'company';
  @observable typeValue = '';
  @observable tabs = [];
  @observable tabKey = '1';
  @observable visible = false;
  @observable visibleAddToVirtual = false;
  @observable visibleSetManagerVirtual = false;
  @observable visibleImportResource = false;
  @observable visibleImportResourceLog = false;
  @observable logVisible = false;
  @observable dialogIsAdd = false;
  @observable dialogId = '';
  @observable dialogFunc = '';
  @observable isEditor = false;
  @observable showCanceled = false;
  @observable typeSearchList = "";
  @observable quickSearchName = "";
  @observable batchOperateType = "";
  @observable changeRightMenu = true;
  @observable rightMenu = [];
  @observable isVirtual = false;
  @observable companysId = '1';
  @observable batchEditKeyWord = '';
  @observable batchEditVal = new Map();
  @observable date;
  @observable showTree = true;
  orgTree;
  @observable isSingle = false;
  @observable setManagerIds = "";
  @observable isView = '';
  @observable API = APIOrg;

  @action
  initParam() {
    this.type = 'company';
    this.typeValue = this.companysId;
    this.tabKey = '1';
    this.showCanceled = false;
    this.isSingle = false;
  }

  initData = () =>{
    let params = {};
    params.id = this.typeValue;
    params.type = this.type;

    this.getTabs(params);
    this.getRightMenu(params);
  }

  initForm() {
    if (this.isEditor) {
      this.form.updateIsFormInit(false);
      this.form = new WeaForm();
      this.formField = [];
    } else {
      this.formView.updateIsFormInit(false);
      this.formView = new WeaForm();
      this.formFieldView = [];
    }
  }

  getHasRight(params = {}) {
    //this.initParam();
    params.isView = this.isView;
    if(this.showTree){
      this.API.getHasRight(params).then((data) => {
        if (data.status == '1') {
          this.hasRight = data.hasRight;
          if (this.hasRight) {
            this.initData();
          }
          this.verified = true;
        } else {
          message.warning(data.message);
        }
      }, error => {
        message.warning(error.message);
      });
    }else{
      this.hasRight = true;
      this.verified = true;
      this.initData();
    }
  }

  getTabs(params = {}) {
    params.id = this.typeValue;
    params.type = this.type;
    this.API.getTabs(params).then(data => {
      if (data.status == '1') {
        this.tabs = data.tabs;
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  getRightMenu(params = {}) {
    params.type = this.type;
    params.id = this.typeValue;
    if (this.tabKey == '1') {
      params.pageid = this.type;
    } else {
      params.pageid = this.typeSearchList + "list";
    }

    this.API.getRightMenu(params).then(data => {
      if (data.status == '1') {
        this.rightMenu = data.rightMenu.filter(d => d.menuFun !== 'departmentRoles');
        let batchfirst = data.rightMenu.filter(d => d.isBatch === '1');
        this.batchOperateType = isEmpty(batchfirst) ? '' : batchfirst[0].menuFun;
        this.getData();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  getTitleInfo(titleInfo = {}) {
    this.title = titleInfo.title;
    this.parentId = titleInfo.parentId;
    this.parentTitle = titleInfo.parentTitle;
  }

  getCompanyFormField(params = {}) {
    this.loading = true;
    this.initForm();
    this.API.getCompanyFormField(params).then((data) => {
      if (this.isEditor) {
        this.form.initFormFields(data.formField,()=>{
          this.loading = false;
        });
        this.formField = data.formField;
        this.dialogId = data.id;
        this.updateVisible(true);
      } else {
        this.formView.initFormFields(data.formField,()=>{
          this.loading = false;
        });
        this.getTitleInfo(data.titleInfo);
        this.formFieldView = data.formField;
      }
      
    });
  }

  getSubCompanyFormFieldView(params = {}) {
    this.loading = true;
    this.initForm();
    this.API.getSubCompanyFormFieldView(params).then((data) => {
      this.getTitleInfo(data.titleInfo);
      this.formView.initFormFields(data.formField,()=>{
        this.loading = false;
      });
      this.formFieldView = data.formField;
    });
  }

  getSubCompanyFormField(params = {}) {
    this.loading = true;
    this.initForm();
    if (this.isVirtual) params.virtualtype = this.companysId;
    this.API.getSubCompanyFormField(params).then((data) => {
      this.form.initFormFields(data.formField,()=>{
        this.loading = false;
      });
      this.formField = data.formField;
      this.dialogId = data.id;
      this.updateVisible(true);
    });
  }

  getDepartmentFormFieldView(params = {}) {
    this.loading = true;
    this.initForm();
    this.API.getDepartmentFormFieldView(params).then((data) => {
      this.getTitleInfo(data.titleInfo);
      this.formView.initFormFields(data.formField,()=>{
        this.loading = false;
      });
      this.formFieldView = data.formField;
      this.canceled = data.canceled;
      const{departmentmark} = this.formView.getFormParams();
      this.departmentInfo = {
        id:data.id,
        name:departmentmark
      }
    });
  }

  getDepartmentFormField(params = {}) {
    this.loading = true;
    this.initForm();
    if (this.isVirtual) params.virtualtype = this.companysId;
    this.API.getDepartmentFormField(params).then((data) => {
      this.form.initFormFields(data.formField,()=>{
        this.loading = false;
      });
      this.formField = data.formField;
      this.dialogId = data.id;
      this.updateVisible(true);
    });
  }

  getSubCompanySearchCondition(params = {}) {
    this.loading = true;
    this.formCondition = new WeaForm();
    this.API.getSubCompanySearchCondition(params).then((data) => {
      this.condition = data.conditions;
      this.formCondition.initFormFields(data.conditions,()=>{
        this.loading = false;
      });
    });
  }

  getDepartmentSearchCondition(params = {}) {
    this.loading = true;
    this.formCondition = new WeaForm();
    this.API.getDepartmentSearchCondition(params).then((data) => {
      this.condition = data.conditions;
      this.formCondition.initFormFields(data.conditions,()=>{
        this.loading = false;
      });
    });
  }

  getResourceSearchCondition(params = {}) {
    this.loading = true;
    this.formCondition = new WeaForm();
    this.API.getResourceSearchCondition(params).then((data) => {
      this.condition = data.conditions;
      this.formCondition.initFormFields(data.conditions,()=>{
        this.loading = false;
      });
    });
  }

  getSubCompanySearchList(params = {}) {
    const formParams = this.formCondition.getFormParams();
    params = { ...params,
      ...formParams,
      isView:this.isView
    };
    params.batchOperateType = this.batchOperateType;
    params.showCanceled = this.showCanceled ? '1' : '0';
    this.table = new TableStore();
    this.API.getSubCompanySearchList(params).then((data) => {
      this.table.getDatas(data.sessionkey, 1);
    });

  }

  getDepartmentSearchList(params = {}) {
    const formParams = this.formCondition.getFormParams();
    params = { ...params,
      ...formParams,
      isView:this.isView
    };
    params.batchOperateType = this.batchOperateType;
    params.showCanceled = this.showCanceled ? '1' : '0';
    this.table = new TableStore();
    this.API.getDepartmentSearchList(params).then((data) => {
      this.table.getDatas(data.sessionkey, 1);
    });
  }

  getResourceSearchList(params = {}) {
    const formParams = this.formCondition.getFormParams();
    params = { ...params,
      ...formParams,
      isView:this.isView
    };
    params.batchOperateType = this.batchOperateType;
    this.table = new TableStore();
    this.API.getResourceSearchList(params).then((data) => {
      this.table.getDatas(data.sessionkey, 1);
      this.defaultPasswordEnable = data.defaultPasswordEnable;
    });
  }

  doSearch(params = {}) {
    params.type = this.type;
    params.id = this.typeValue;
    if (this.typeSearchList == 'subcompany') {
      this.getSubCompanySearchList(params);
    } else if (this.typeSearchList == 'department') {
      this.getDepartmentSearchList(params);
    } else if (this.typeSearchList == 'resource') {
      this.getResourceSearchList(params);
    }
  }

  setShowSearchAd(bool) {
    this.showSearchAd = bool;
  }

  changeShowAll() {
    this.showCanceled = !this.showCanceled;
    this.orgTree && this.orgTree.fetchData({showCanceled:this.showCanceled ? '1' : '0',companysId:this.companysId})
    this.getData();
  }

  updateTabkey(key) {
    this.tabKey = key;
    this.batchEditKeyWord = '';
    this.batchEditVal = new Map();
    if (this.tabKey == '2') {
      if (this.type == 'company' || this.type == 'subcompany') {
        this.typeSearchList = "subcompany";
      } else if (this.type == 'department') {
        this.typeSearchList = "department";
      }
    } else if (this.tabKey == '3') {
      if (this.type == 'subcompany') {
        this.typeSearchList = "department";
      } else if (this.type == 'department') {
        this.typeSearchList = "resource";
      }
    }
    this.quickSearchName = this.typeSearchList + 'name';
    this.initData();
  }

  updateVisible(bool) {
    this.hideSaveBtn=false;
    this.visible = bool;
  }

  updateImportVisible(bool) {
    this.visibleImportResource = bool;
  }

  updateVisibleImportLogDialog(bool) {
    this.visibleImportResourceLog = bool;
  }

  selectVirtual(v) {
    this.isVirtual = v && v < 0;
    this.initParam();
    this.companysId = v;
    this.typeValue = v;
    this.API = APIOrg;
    if (this.isVirtual) {
      this.API = APIOrgVirtual;
    }
  }

  changeVirtual() {
    this.isVirtual = true;
    this.API = APIOrgVirtual;
  }

  getCompanyForm(params = {}) {
    this.isEditor = true;
    params.viewattr = 2;
    if (this.dialogIsAdd) {
      this.API = APIOrgVirtual;
      this.dialogTitle = i18n.label.addVirtualOrg();
    } else {
      this.dialogTitle = this.isVirtual ? i18n.label.editVirtualOrg() : i18n.label.editCompany();
    }
    this.getCompanyFormField(params);
  }

  getSubCompanyForm(params = {}) {
    this.isEditor = true;
    params.viewattr = 2;
    this.dialogTitle = this.dialogIsAdd ? i18n.label.addSubCompany() : i18n.label.editSubCompany();
    if (this.dialogIsAdd) {
      params.id = this.typeValue;
    }
    this.getSubCompanyFormField(params);
  }

  getDepartmentForm(params = {}) {
    this.isEditor = true;
    params.viewattr = 2;
    params.type = this.type;
    this.dialogTitle = this.dialogIsAdd ? i18n.label.addDepartment() : i18n.label.editDepartment();
    if (this.dialogIsAdd) {
      params.id = this.typeValue;
    }
    this.getDepartmentFormField(params);
  }

  editCompany() {
    const formField = this.formField;
    let formParams = this.form.getFormParams();
    this.form.validateForm().then(f => {
      if (f.isValid) {
        this.hideSaveBtn=true;
        this.API.editCompany({ ...{
            id: this.dialogId
          },
          ...formParams
        }).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.updateVisible(false);
            this.getData();
            this.orgTree && this.orgTree.fetchData();
          } else {
            message.warning(data.message);
          }
          this.hideSaveBtn=false
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  editSubCompany() {
    this.hideSaveBtn=true;
    const formField = this.formField;
    let formParams = this.form.getFormParams();
    this.form.validateForm().then(f => {
      if (f.isValid) {
        if (formParams.supsubcomid == this.dialogId) {
          message.warning(i18n.message.supSubComError());
          return;
        }
        this.API.editSubCompany({ ...{
            id: this.dialogId
          },
          ...formParams
        }).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.updateVisible(false);
            this.getData();
            // this.orgTree && this.orgTree.fetchData();
          } else {
            message.warning(data.message);
          }
          this.hideSaveBtn=false
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  editDepartment() {
    this.hideSaveBtn=true;
    const formField = this.formField;
    let formParams = this.form.getFormParams();
    this.form.validateForm().then(f => {
      if (f.isValid) {
        if (formParams.supdepid == this.dialogId) {
          message.warning(i18n.message.supDeptComError());
          return;
        }
        this.API.editDepartment({ ...{
            id: this.dialogId
          },
          ...formParams
        }).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.updateVisible(false);
           // this.orgTree && this.orgTree.fetchData();
            this.getData();
          } else {
            message.warning(data.message);
          }
          this.hideSaveBtn=false
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  addCompany() {
    const formField = this.formField;
    let formParams = this.form.getFormParams();
    this.form.validateForm().then(f => {
      if (f.isValid) {
        this.API.addCompany(formParams).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.updateVisible(false);
            this.selectVirtual(data.id);
            this.initData();
           this.orgTree && this.orgTree.resetDropMenu(this.companysId);
          } else {
            message.warning(data.message);
          }
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  addSubCompany() {
    const formField = this.formField;
    let formParams = this.form.getFormParams();
    if (this.isVirtual) {
      formParams.companyid = this.companysId;
    }
    this.form.validateForm().then(f => {
      if (f.isValid) {
        this.API.addSubCompany(formParams).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.updateVisible(false);
            this.initParam();
            this.type = 'subcompany';
            this.typeValue = data.id;
            this.orgTree && this.orgTree.fetchData();
            this.initData();
          } else {
            message.warning(data.message);
          }
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  addDepartment() {
    const formField = this.formField;
    let formParams = this.form.getFormParams();
    this.form.validateForm().then(f => {
      if (f.isValid) {
        this.API.addDepartment(formParams).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.updateVisible(false);
            this.initParam();
            this.type = 'department';
            this.typeValue = data.id;
           this.orgTree && this.orgTree.fetchData();
            this.initData();
          } else {
            message.warning(data.message);
          }
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  delCompany(params = {}) {
    this.API.delCompany(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.deleteSuccess());
        this.initParam();
        this.initData();
        this.selectVirtual(1);
        this.orgTree && this.orgTree.resetDropMenu();
        this.orgTree.orgTree.reset(() => {
          this.orgTree.orgTree.fetchData();
        });
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  delSubCompany(params = {}) {
    this.API.delSubCompany(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.deleteSuccess());
       this.orgTree && this.orgTree.fetchData();
        if (params.batchDelete) {
          this.initParam();
          this.initData();
        } else {
          this.getData();
        }
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  delDepartment(params = {}) {
    this.API.delDepartment(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.deleteSuccess());
       this.orgTree && this.orgTree.fetchData();
        if (params.batchDelete) {
          this.initParam();
          this.initData();
        } else {
          this.getData();
        }
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  delResource(params = {}) {
    APIOrgVirtual.delResource(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.deleteSuccess());
        this.getData();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  doDelete = (params = {}) => {
    let _this = this;
    let type = '';
    if (!!!params.id) {
      type = this.type;
      params.id = this.typeValue;
      params.batchDelete = true;
    } else {
      type = this.typeSearchList;
    }
    confirm({
      title: i18n.confirm.defaultTitle(),
      content: i18n.confirm.delete(),
      onOk() {
        if (type == 'company') {
          _this.delCompany(params)
        } else if (type == 'subcompany') {
          _this.delSubCompany(params)
        } else if (type == 'department') {
          _this.delDepartment(params)
        } else if (type == 'resource') {
          _this.delResource(params)
        }
      },
      onCancel() {
        return false;
      },
    });
  }

  doCancel(params = {}) {
    let _this = this;
    confirm({
      title: i18n.confirm.defaultTitle(),
      content: i18n.confirm.cancel(),
      onOk() {
        if (_this.type == 'subcompany') {
          _this.doSubCompanyCancel({
            id: _this.typeValue
          })
        } else if (_this.type == 'department') {
          _this.doDepartmentCancel({
            id: _this.typeValue
          })
        }
      },
      onCancel() {
        return false;
      },
    });
  }

  doISCanceled(params = {}) {
    let _this = this;
    confirm({
      title: i18n.confirm.defaultTitle(),
      content: i18n.confirm.doISCancel(),
      onOk() {
        if (_this.type == 'subcompany') {
          _this.doSubCompanyISCanceled({
            id: _this.typeValue
          })
        } else if (_this.type == 'department') {
          _this.doDepartmentISCanceled({
            id: _this.typeValue
          })
        }
      },
      onCancel() {
        return false;
      },
    });
  }

  doSubCompanyCancel(params = {}) {
    this.API.doSubCompanyCancel(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.cancelSuccess());
       this.orgTree && this.orgTree.fetchData();
        this.getRightMenu();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  doSubCompanyISCanceled(params = {}) {
    this.API.doSubCompanyISCanceled(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.doISCancelSuccess());
       this.orgTree && this.orgTree.fetchData();
        this.getRightMenu();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  doDepartmentCancel(params = {}) {
    this.API.doDepartmentCancel(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.cancelSuccess());
       this.orgTree && this.orgTree.fetchData();
        this.getRightMenu();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  doDepartmentISCanceled(params = {}) {
    this.API.doDepartmentISCanceled(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.doISCancelSuccess());
        this.orgTree &&this.orgTree.fetchData();
        this.getRightMenu();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  updateBatchOperateType(key) {
    this.batchOperateType = key;
    this.changeRightMenu = false;
    this.getData();
  }

  doBatchOperate() {
    let func = this.batchOperateType;
    this[func] && this[func]();
  }

  batchCancel() {
    let keys = toJS(this.table.selectedRowKeys);
    if (isEmpty(keys)) {
      message.warning(i18n.confirm.defaultTitle());
      return false;
    }

    let _this = this;
    confirm({
      title: i18n.confirm.defaultTitle(),
      content: i18n.confirm.cancelSelected(),
      onOk() {
        if (_this.typeSearchList == 'subcompany') {
          _this.doSubCompanyCancel({
            id: keys
          })
        } else if (_this.typeSearchList == 'department') {
          _this.doDepartmentCancel({
            id: keys
          })
        }
      },
      onCancel() {
        return false;
      },
    });
  }

  batchDoIsCanceled() {
    let keys = toJS(this.table.selectedRowKeys);
    if (isEmpty(keys)) {
      message.warning(i18n.confirm.defaultTitle());
      return false;
    }

    let _this = this;
    confirm({
      title: i18n.confirm.defaultTitle(),
      content: i18n.confirm.doISCancelSelected(),
      onOk() {
        if (_this.typeSearchList == 'subcompany') {
          _this.doSubCompanyISCanceled({
            id: keys
          })
        } else if (_this.typeSearchList == 'department') {
          _this.doDepartmentISCanceled({
            id: keys
          })
        }
      },
      onCancel() {
        return false;
      },
    });
  }

  batchDel() {
    let keys = toJS(this.table.selectedRowKeys);
    if (isEmpty(keys)) {
      message.warning(i18n.confirm.selectedEmpty());
      return false;
    }

    let _this = this;
    confirm({
      title: i18n.confirm.defaultTitle(),
      content: i18n.confirm.batchDeleteConfirm(),
      onOk() {
        if (_this.typeSearchList == 'subcompany') {
          _this.delSubCompany({
            id: keys
          })
        } else if (_this.typeSearchList == 'department') {
          _this.delDepartment({
            id: keys
          })
        } else if (_this.typeSearchList == 'resource') {
          _this.delResource({
            id: keys
          })
        }
      },
      onCancel() {
        return false;
      },
    });
  }

  setBatchEditVal(type, id, val) {
    let datas = toJS(this.table.datas);
		const idx = findIndex(datas, {id});
		datas[idx][type] = val;
		this.table.tableUpdate({datas});

    this.batchEditKeyWord = type;
    // if (type == 'showorder' && val < 0) {
    //   message.warning('显示顺序不能小于0！');
    //   return;
    // }
    this.batchEditVal.set(id, val);
  }

  batchCode() {
    this.saveBatchEdit();
  }

  batchOrder() {
    this.saveBatchEdit();
  }

  batchSetManager(params = {}) {
    if (!!!params.ids) {
      params.ids = this.table.selectedRowKeys;
    }
    this.setManagerIds = params.ids;
    params.virtualtype = this.companysId;
    this.loading = true;
    this.isEditor = true;
    this.initForm();
    APIOrgVirtual.getBatchSetManagerFormField(params).then((data) => {
      this.form.initFormFields(data.formField,()=>{
        this.loading = false;
      });
      this.formField = data.formField;
      this.visibleSetManagerVirtual = true;
    });
  }

  saveBatchSetManager() {
    let params = this.form.getFormParams();
    params.ids = this.setManagerIds;
    this.form.validateForm().then(f => {
      if (f.isValid) {
        APIOrgVirtual.saveBatchSetManager(params).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.visibleSetManagerVirtual = false;
            this.getData();
          } else {
            message.warning(data.message);
          }
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  addResourceVirtual(params = {}) {
    params.ids = this.table.selectedRowKeys;
    this.loading = true;
    this.isEditor = true;
    this.initForm();
    this.API.getResourceVirtualDepartmentSetFormField(params).then((data) => {
      this.form.initFormFields(data.formField,()=>{
        this.loading = false;
      });
      this.formField = data.formField;
      this.visibleAddToVirtual = true;
    });
  }

  saveResourceVirtualDepartmentSet() {
    let params = this.form.getFormParams();
    params.resourceids = this.table.selectedRowKeys;
    this.form.validateForm().then(f => {
      if (f.isValid) {
        this.API.saveResourceVirtualDepartmentSet(params).then(data => {
          if (data.status == '1') {
            message.success(i18n.message.saveSuccess());
            this.visibleAddToVirtual = false;
            this.getData();
          } else {
            message.warning(data.message);
          }
        }, error => {
          message.warning(error.message);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    })
  }

  saveBatchEdit(params = {}) {
    params.ids = [...this.batchEditVal.keys()].join(','); //Object.keys([...this.batchEditVal]).join(',');
    params.values = [...this.batchEditVal.values()].join(',');
    params.typeSearchList = this.typeSearchList;
    params.batchEditKeyWord = this.batchEditKeyWord;
    if (params.ids == '' || params.values == '') return;

    let values = params.values.split(',');
    let array = new Array();
    let idx = 0;
    for (let i = 0; values != null && i < values.length; i++) {
      if (values[i] != "") {
        array[idx++] = values[i];
      }
    }

    array = array.sort();
    for (let i = 0; i < array.length; i++) {
      if (array[i] == array[i + 1]) {
        let msg = "";
        if (this.batchEditKeyWord == 'subcompanycode') {
          msg = `${i18n.confirm.subCompanyCodeExist()}(${array[i+1]})`;
        } else if (this.batchEditKeyWord == 'departmentcode') {
          msg = `${i18n.confirm.departmentCodeExist()}(${array[i+1]})`;
        } else if (this.batchEditKeyWord == 'workcode') {
          msg = `${i18n.confirm.workCodeExist()}(${array[i+1]})`;
        }
        if (msg != '') {
          message.warning(msg);
          return false;
        }
      }
    }

    this.API.saveBatchEdit(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.saveSuccess());
        this.getData();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  saveAddToGroup(params = {}) {
    params.ids = this.table.selectedRowKeys;
    if (params.ids == '' || params.groupids == '') return;
    this.API.saveAddToGroup(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.saveSuccess());
        this.getData();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  addResourceToVirtual(ids) {
    let params = {};
    params.resourceids = ids;
    params.departmentid = this.typeValue;
    if (params.ids == '') return;
    APIOrgVirtual.addResourceToVirtual(params).then(data => {
      if (data.status == '1') {
        message.success(i18n.message.saveSuccess());
        this.getData();
      } else {
        message.warning(data.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  getData(params = {}) {
    this.isEditor = false;
    params.id = this.typeValue;
    params.type = this.type;
    // if(this.changeRightMenu){
    //   this.getRightMenu(params);
    // }
    // this.changeRightMenu = true;
    if (this.type == "company") {
      if (this.tabKey == "1") {
        this.getCompanyFormField(params);
      } else {
        this.getSubCompanySearchCondition(params);
        this.getSubCompanySearchList(params);
      }
    } else if (this.type == "subcompany") {
      if (this.tabKey == "1") {
        this.getSubCompanyFormFieldView(params);
      } else if (this.tabKey == "2") {
        this.getSubCompanySearchCondition(params);
        this.getSubCompanySearchList(params);
      } else if (this.tabKey == "3") {
        this.getDepartmentSearchCondition(params);
        this.getDepartmentSearchList(params);
      }
    } else if (this.type == "department") {
      if (this.tabKey == "1") {
        this.getDepartmentFormFieldView(params);
      } else if (this.tabKey == "2") {
        this.getDepartmentSearchCondition(params);
        this.getDepartmentSearchList(params);
      } else if (this.tabKey == "3") {
        this.getResourceSearchCondition(params);
        this.getResourceSearchList(params);
      }
    }
  }

  setDate(date) {
    this.date = date;
  }

  batchResetPwd () {
    var _this = this;
    confirm({
      title: i18n.confirm.defaultTitle(),
      content: (this.defaultPasswordEnable === "0") ? getLabel(515424,'未开启初始密码，请前往账户中心的安全设置中开启。') :getLabel(515423,'确定要将选中用户的密码重置为初始密码吗？' ),
      onOk() {
        if (_this.defaultPasswordEnable === "1") {//开启
          const params = {
            resourceIds:_this.table.selectedRowKeys
          };
          APIOrg.saveBatchDefaultPwd(params).then(datas=>{
            const {status} = datas;
            if (status === "1") {
              message.success(datas.message);
              const params={
                type:_this.type,
                id:_this.typeValue
              }
              _this.getResourceSearchList(params);
            }else{
              message.warning(datas.message)
            }
          })
        } else {
          window.open("/spa/hrm/engine.html#/hrmengine/securitySetting");
        }
      },
      onCancel() {
        return false
      },
    });
  }
}