import {
  WeaOrgTree
} from 'ecCom'

import {
  inject,
  observer,
} from 'mobx-react'

import {
  i18n
} from '../../public/i18n';

@inject('hrmSignatureManagement')
@observer
export default class LeftTree extends React.Component {
  constructor(props) {
    super(props);
    this.tree = null;
    this.setRef = element => {
      this.tree = element;
    }
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.triggerRefresh !== nextProps.triggerRefresh) {
      this.init();
    }
  }

  componentDidMount() {
    this.init()
  }

  init = () => {
    if (this.tree) this.tree.reset(() => this.tree.fetchData());
  }

  treeNodeClick = (event, ids, nodeids, nodes) => {
    const {
      hrmSignatureManagement
    } = this.props, {
      handleLeftTreeClick
    } = hrmSignatureManagement;

    const type = nodes[0].type;


    const nodeid= ids[0];

    const pid = Array.isArray(nodes) && nodes[0].pid

    handleLeftTreeClick(nodeid,type,nodeids,pid);

  }

  render() {
    return (
      <div style={{height: '100%'}}>
            <WeaOrgTree ecId={`${this && this.props && this.props.ecId || ''}_WeaOrgTree@ud6rnm`} 
                ref={this.setRef}
                needSearch
                noCache={true}
                needDropMenu={true}
                isLoadAllSub={true}
                isLoadSubDepartment={true}
                inputLeftDom = {`<b>${i18n.label.organization()}</b>`}
                params={{rightStr:'SignatureAdd:Add'}}
                treeNodeClick={(event, ids, nodeids, nodes) => this.treeNodeClick(event, ids, nodeids, nodes)}
            />
        </div>
    )
  }
}