import {
  WeaOrgTree
} from 'ecCom'

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

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.treeRefresh !== nextProps.treeRefresh || this.props.reload !== nextProps.reload) {
      if (this.tree) this.tree.reset(() => this.tree.fetchData());
    }
  }

  treeNodeClick(e) {
    const {
      treeNodeClick
    } = this.props;

    let type = e.selectedNodes[0].props.type,
      id = e.selectedNodes[0].props.id,
      pid = this.getParentId(e.selectedNodes[0].key);
    treeNodeClick(type, id, pid);
  }

  getParentId(key) {
    return key.split('-').slice(-2, -1);
  }

  render() {
    return (
      <div style={{height: '100%'}}>
  		      <WeaOrgTree ecId={`${this && this.props && this.props.ecId || ''}_WeaOrgTree@cv42pc`} 
                ref={this.setRef}
                rootKey='rootManager' 
                loading  
                needSearch
                inputLeftDom = {`<b>${i18n.label.postSys()}</b>`} 
                dataUrl={'/api/hrm/job/getJobTree'} 
                treeNodeClick={e => this.treeNodeClick(e)}
            />
  		  </div>
    )
  }
}