import React from 'react';
import {
  inject,
  observer
} from 'mobx-react';
import {
  WeaInput,
  WeaInputNumber,
} from 'ecCom';
import * as mobx from 'mobx';
import isEmpty from 'lodash/isEmpty';
const toJS = mobx.toJS;
import {
  WeaTableNew
} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import * as PublicFunc from '../../public/pulic-func';
import {
  i18n
} from '../../public/i18n';

@inject('hrmOrganization')

@observer
class OrganizationList extends React.Component {
  constructor(props) {
    super(props);
  }

  showInfo(params) {
    const {
      hrmOrganization
    } = this.props;
    hrmOrganization.typeValue = params.id;
    if (params.type == "subcompanyname" || params.type == "subcompanydesc") {
      hrmOrganization.type = "subcompany";
    } else if (params.type == "departmentmark" || params.type == "departmentname") {
      hrmOrganization.type = "department";
    }
    hrmOrganization.updateTabkey('1');
  }

  doEdit(id) {
    const {
      hrmOrganization
    } = this.props;
    const {
      typeSearchList
    } = hrmOrganization;
    hrmOrganization.dialogIsAdd = false;
    if (typeSearchList == "subcompany") {
      hrmOrganization.dialogFunc = 'editSubCompany';
      hrmOrganization.getSubCompanyForm({
        id
      });
    } else if (typeSearchList == "department") {
      hrmOrganization.dialogFunc = 'editDepartment';
      hrmOrganization.getDepartmentForm({
        id
      });
    }
  }

  doCancel(id) {
    const {
      hrmOrganization
    } = this.props;
    const {
      typeSearchList
    } = hrmOrganization;
    if (typeSearchList == "subcompany") {
      hrmOrganization.doSubCompanyCancel({
        id
      });
    } else if (typeSearchList == "department") {
      hrmOrganization.doDepartmentCancel({
        id
      });
    }
  }

  doISCanceled(id) {
    const {
      hrmOrganization
    } = this.props;
    const {
      typeSearchList
    } = hrmOrganization;
    if (typeSearchList == "subcompany") {
      hrmOrganization.doSubCompanyISCanceled({
        id
      });
    } else if (typeSearchList == "department") {
      hrmOrganization.doDepartmentISCanceled({
        id
      });
    }
  }

  doDel(id) {
    const {
      hrmOrganization,
    } = this.props;
    const {
      typeSearchList
    } = hrmOrganization;
    if (typeSearchList == "subcompany") {
      hrmOrganization.delSubCompany({
        id
      });
    } else if (typeSearchList == "department") {
      hrmOrganization.delDepartment({
        id
      });
    }
  }

  doDel(id) {
    const {
      hrmOrganization,
    } = this.props;
    hrmOrganization.doDelete({id});
    // const {
    //   typeSearchList
    // } = hrmOrganization;
    // if (typeSearchList == "subcompany") {
    //   hrmOrganization.delSubCompany({
    //     id
    //   });
    // } else if (typeSearchList == "department") {
    //   hrmOrganization.delDepartment({
    //     id
    //   });
    // }
  }

  reRenderColumns(columns) {
    const {
      hrmOrganization
    } = this.props;
    const {
      typeSearchList,
      batchOperateType,
      isVirtual,
    } = hrmOrganization;
    let _this = this
    let needRender = new Set();

    if (typeSearchList == 'subcompany') {
      needRender.add('subcompanyname');
      needRender.add('subcompanydesc');
      if (batchOperateType == 'batchOrder') {
        needRender.add('showorder');
      } else if (batchOperateType == 'batchCode') {
        needRender.add('subcompanycode');
      }
    } else if (typeSearchList == 'department') {
      needRender.add('departmentmark');
      needRender.add('departmentname');
      if (batchOperateType == 'batchOrder') {
        needRender.add('showorder');
      } else if (batchOperateType == 'batchCode') {
        needRender.add('departmentcode');
      }
    } else if (typeSearchList == 'resource') {
      needRender.add('lastname');
      if (batchOperateType == 'batchOrder') {
        needRender.add('dsporder');
      } else if (batchOperateType == 'batchCode') {
        needRender.add('workcode');
      }
    }

    columns.forEach((c, index) => {
      if (c.dataIndex == 'showorder' || c.dataIndex == 'dsporder'){
        c.render = function(text, record) {
          return <div>{Number(text).toFixed(2)}</div>
        } 
      }
      if (c.dataIndex == 'managerid') {
        c.render = function(text, record) {
          const {manageridspan,managerid} = record;
            return (
              <a
              href={`javaScript:openhrm(${managerid});`}
              onClick={e => window.pointerXY(e)}
              title={manageridspan}
            >
              {manageridspan}
            </a>
            )
          }
      }
      if (needRender.has(c.dataIndex)) {
        if (c.dataIndex == 'lastname') {
          c.render = function(text, record) {
            return (
              <a
              href={`javaScript:openhrm(${record.resourceid?record.resourceid:record.id});`}
              onClick={e => window.pointerXY(e)}
              title={record.lastname}
            >
              {record.lastname}
            </a>
            )
            // return <span style={{cursor:'pointer'}} className='wea-hrm-linkstyle' onClick={()=>_this.jumpToHrmCard(record.resourceid?record.resourceid:record.id)}>{record.lastname}</span>
          }
        } else if (c.dataIndex == 'subcompanycode' || c.dataIndex == 'departmentcode' || c.dataIndex == 'workcode') {
          c.render = function(text, record) {
            let rowData = toJS(record);
            return <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@za9lo4`} 
                   value={rowData[c.dataIndex]} 
                   onBlur={ (val) =>  hrmOrganization.setBatchEditVal(c.dataIndex,rowData.id,val) } 
                   style={{width:120}}
                 />
          }
        } else if (c.dataIndex == 'showorder' || c.dataIndex == 'dsporder') {
          c.render = function(text, record) {
            let rowData = toJS(record);
            //WeaInputNumber 不支持，先改回 WeaInput
            return <WeaInputNumber ecId={`${this && this.props && this.props.ecId || ''}_WeaInputNumber@2s4xfv`} precision = {2} step={1}
                  value={rowData[c.dataIndex]} 
                  onChange={ (val) => {hrmOrganization.setBatchEditVal(c.dataIndex,rowData.id,val)}} 
                  style={{width:120}}
                />
          }
        } else if (c.dataIndex == 'subcompanyname' ||  (c.dataIndex == 'departmentname' && !isVirtual) ||(c.dataIndex == 'departmentmark' && isVirtual) ) {
          c.render = function(text, record) {
            let params = {
              type: c.dataIndex,
              id: record.id
            }
            return <div>
                        <a onClick={() => _this.showInfo(params)}>{record[c.dataIndex]}</a>
                        {record.canceled=='1' && <span style={{color: '#FF0000'}}>({i18n.label.canceled()})</span>}
                        </div>
          }
        }
      }
    })

    return columns;
  }

  onOperatesClick(record, index, operate) {
    const func = operate.href ? operate.href.split(':')[1].split('(')[0] : '';
    const id = record.id ? record.id : '';
    this[func] && this[func](id);
  }

  showLog(id,resourceid){
    const {
      hrmOrganization
    } = this.props;
    const {
      isVirtual,
      typeSearchList,
    } = hrmOrganization;
    let params = {targetId:id};
    if (typeSearchList == 'subcompany') {
      params.logSmallType = i18n.smallType.HRM_ENGINE_ORGANIZATION_SUBCOMPANY;
    } else if (typeSearchList == 'department') {
      params.logSmallType = i18n.smallType.HRM_ENGINE_ORGANIZATION_DPEARTMENT;
    } else if (typeSearchList == 'resource') {
      params.logType = '3';
      params.logSmallType = i18n.smallType.HRM_ENGINE_ORGANIZATION_RESOURCE;
      if(isVirtual){
        params.logType = '4';
        params.targetId=resourceid;
        params.logSmallType = i18n.smallType.HRM_ENGINE_ORGANIZATION_VIRTUAL_RESOURCE;
      }
    }  
    window.setLogViewProps(params);
  }

  setManager(hrmId) {
    const {
      hrmOrganization
    } = this.props;
    hrmOrganization.batchSetManager({
      ids: hrmId
    });
  }

  doDelete(hrmId) {
    const {
      hrmOrganization
    } = this.props;
    hrmOrganization.doDelete({
      id: hrmId
    });
  }

  jumpToHrmCard(hrmId, ...rest) {
    PublicFunc.jumpToHrmCard(hrmId);
  }

  editHrmResource(hrmId) {
    window.open(`/spa/hrm/index_mobx.html#/main/hrm/card/cardInfo/${hrmId}?isEditor=true`);
  }

  addResource(id) {
    window.open(`/spa/hrm/index_mobx.html#/main/hrm/add?departmentid=${id}`);
  }

  render() {
    const {
      hrmOrganization
    } = this.props;
    const {
      table
    } = hrmOrganization;

    return (
      <div className='wea-hrm-organization-list'>
        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@6prh5b`}
          comsWeaTableStore={table}
          hasOrder={true}
          needScroll={true}
          getColumns={c => this.reRenderColumns(c)}
          onOperatesClick={(record, index, operate) => this.onOperatesClick(record, index, operate)}
        />
      </div>
    );
  }
}

export default OrganizationList