import React from "react";
import {
  toJS,
} from "mobx";
import { 
  inject, 
  observer,
} from "mobx-react";
import {
  WeaLocaleProvider,
  WeaLeftRightLayout,
  WeaTop,
  WeaTab,
  WeaRightMenu,
} from "ecCom";
import { 
  WeaTableNew,
} from "comsMobx";
import * as IntegrationUtil from "../../util/IntegrationUtil";
import NoRight from "../../public/NoRight";
import NonStand from "../../public/NonStand";
import OrganizationTree from '../../public/OrganizationTree';
import WebServiceCreate from './webServiceCreate';

const {WeaTable} = WeaTableNew;
const {getLabel} = WeaLocaleProvider;

@inject("webservice")
@observer
export default class Main extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showSearchAd: false,
    }
  }

  componentDidMount() {
    const { webservice } = this.props;
    webservice.doInit();
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.location.key !== nextProps.location.key) {
      const { webservice } = this.props;
      webservice.doInit();
    }
  }

  // 分权刷新
  refreshData = (id, type) => {
    const {
      webservice
    } = this.props;
    if("1" === type) {
      // 如果点击的是分部
      webservice.setState({
        subcompanyid: id, 
      });
    } else{
      webservice.setState({
        subcompanyid: "", 
      });
    }
    webservice.getConditions(webservice.getTableInfo);
  }

  getSearchs = () => {
    const { webservice } = this.props;
    const { status: { condition }, form } = webservice;
    return IntegrationUtil.getAdvancedItem(
      form,
      condition,
      this.handleSearchFun,
      false, 
      this && this.props && this.props.ecId || '', 
      "integrationWebservice"
    );
  }

  handleSearchFun = () => {
    const { webservice } = this.props;
    webservice.setState({
      subcompanyid: webservice.form.getFormParams().subcompanyid1, 
    });
    webservice.getTableInfo();
    this.setState({ showSearchAd: false });
  }

  getTabButtonsAd = () => {
    const { webservice } = this.props;
    const { form } = webservice;
    return IntegrationUtil.getSearchBtns(
      this.handleSearchFun, 
      () => form.reset(), 
      () => this.setState({ showSearchAd: false }),
      this && this.props && this.props.ecId || '', 
      "integrationWebservice"
    );
  }

  getColumns = (columns) => {
    const { webservice } = this.props;
    return columns.map(column => {
      if (column.dataIndex == 'customname') {
        return {
          ...column, 
          render: (text, record, index) => {
            return (<a onClick={() => webservice.edit(record)}>{record.customname}</a>);
          }
        }
      } else if (column.dataIndex == 'subcompanyid') {
        return {
          ...column, 
          render: (text, record, index) => {
            return (<a href={`${window.ecologyContentPath || ""}/spa/hrm/engine.html#/hrmengine/organization?showTree=false&isView=1&type=subcompany&id=${record.subcompanyid}`} target="_blank">{record.subcompanyidspan}</a>);
          }
        }
      } else {
        return column;
      }
    });
  }

  render() {
    const { 
      webservice 
    } = this.props, { 
      status, 
      form, 
      tableStore, 
      getRightMenuData, 
      getTopButton,
      onOperatesClick, 
      hasRight, 
      rightLoading, 
    } = webservice, {
      loading, 
      advanceHeight, 
      hasNonStandard, 
      nonStandLoading,
      collectParams,
      hasOrganization,
      subcompanyid,
    } = toJS(status), {
      showSearchAd,
    } = this.state;

    return (nonStandLoading
      ? null
      : hasNonStandard
        ? rightLoading 
          ? null
          : hasRight 
            ? <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@l9tfxc`}
                datas={getRightMenuData()}
                collectParams={collectParams}
              >
                <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@yi80f5`}
                  buttons={getTopButton()}
                  title={getLabel(33717, "WebService注册")}
                  icon={<i className={IntegrationUtil.GeneralTitleColor.icon} />}
                  iconBgcolor={IntegrationUtil.GeneralTitleColor.iconBgcolor}
                  showDropIcon={true}
                  dropMenuDatas={getRightMenuData()}
                  dropMenuProps={{collectParams}}
                  loading={loading}
                />
                <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@w2p5rj`}
                  showLeft={hasOrganization}
                  showBtn={hasOrganization}
                  leftCom={<OrganizationTree ecId={`${this && this.props && this.props.ecId || ''}_OrganizationTree@duqaq7`} 
                    ref="webservice"
                    selectedKeys={[subcompanyid]}
                    refreshData={this.refreshData}
                    rightStr={"intergration:webserivcesetting"}
                    {...(this.props.orgTreeParams || {})} 
                  />}
                >
                  <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@9zsyae`}
                    searchType={['base', 'advanced']}
                    advanceHeight={advanceHeight}
                    buttonsAd={this.getTabButtonsAd()}
                    showSearchAd={showSearchAd}
                    setShowSearchAd={showSearchAd => this.setState({showSearchAd})}
                    hideSearchAd={() => this.setState({showSearchAd: false})}
                    searchsAd={this.getSearchs()}
                    searchsBaseValue={form.getFormParams().customname || ''}
                    onSearch={this.handleSearchFun}
                    onSearchChange={value => form.updateFields({customname: {value}})}
                  />
                  <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@lv0t2u`}
                    hasOrder={true}
                    onOperatesClick={onOperatesClick}
                    comsWeaTableStore={tableStore}
                    needScroll={true}
                    getColumns={this.getColumns}
                  />
                </WeaLeftRightLayout>
                <WebServiceCreate ecId={`${this && this.props && this.props.ecId || ''}_WebServiceCreate@7g4xrn`} />
              </WeaRightMenu>
            : <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@ptbhon`} />
        : <NonStand ecId={`${this && this.props && this.props.ecId || ''}_NonStand@n61w6q`} />
    );
  }
}
