/**
 * Created by geweimin on 11/17/16.
 */
import React from 'react';
import Qs from 'qs';
import { autobind } from 'core-decorators';
import staticDisplay from './StaticDisplay';
import { VIEW, EDIT } from '../../constant';
import { showModal } from '../../../../pageContainer/ModalWrapper';
import { jumpToNewTab } from '../../../../utils/windowUtils';
import { convertTemplate } from '../../../../utils/StringUtils';
import { CONTAINER_PRE } from '../../../../entrance/routes';
import { Input, Button, Row, Col } from '../../../../../components/mxa';

class LinkText extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
    };
  }

  @autobind
  _onClick() {
    const buttonLink = this.props.buttonLink;
    const detailResult = this.props.detailResult;
    const jumpProps = {
      buttonCode: this.props.buttonCode
    };
    if (buttonLink.simpleLink) {
      const bindParameter = buttonLink.bindParameters;
      const params = {};
      bindParameter.forEach((item, index) => {
        if (buttonLink.key && index === 0) {
          params[buttonLink.key] = detailResult[item.name];
        }
        params[item.name] = detailResult[item.name];
      });
      this.jumpTab(buttonLink.actionLink, params, buttonLink.domainType, jumpProps);
    } else {
      const linkItems = buttonLink.actionLinks;
      let linkItem = null;
      linkItems.forEach((item, index) => {
        if (item.name === detailResult[buttonLink.parameter.name]) {
          linkItem = item;
        }
      });
      if (linkItem) {
        const params = {};
        linkItem.bindParameters.forEach((item, index) => {
          if (index === 0) {
            params[linkItem.key] = detailResult[item.name];
          } else {
            params[item.name] = detailResult[item.name];
          }
        });
        this.jumpTab(linkItem.link, params, linkItem.domainType, jumpProps);
      }
    }
  }

  @autobind
  jumpTab(actionLink, params, domainType, jumpProps) {
    jumpToNewTab(
      '/' + CONTAINER_PRE + convertTemplate(actionLink, params, this.props.actionLinkMappings) + '?p=' + btoa(Qs.stringify({ ...params, readonly: true, domainType, s: '1' })),
      jumpProps
    );
  }

  render() {
    const { disabled } = this.props;
    return (
      <Row>
        <Col span={18}>
          <Input type="primary" disabled={disabled} value={this.props.value} />
        </Col>
        <Col offset={1} span={5} style={{ display: 'flex', flexDirection: 'row-reverse' }}>
          <Button onClick={this._onClick}>查看详情</Button>
        </Col>
      </Row>
    );
  }
}

const compRender = config => {
  const { record, props } = config;
  const buttonLink = record.buttonLink;
  const detailResult = props.dataSource.detailResult;
  const fieldCode = props.dataSource.fieldCode;
  return (
    <LinkText buttonCode={fieldCode} buttonLink={buttonLink} detailResult={detailResult} disabled={record.isReadonly || (props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly)} />
  );
};

  // class StaticDisplay extends React.Component {
//   render() {
//     const value = this.props.value;
//     const format = this.props.format ? this.props.format : '0,0.0000';
//     return (
//       <span>{value && numeral(value).format(format)}</span>
//     );
//   }
// }

export default {
  // [VIEW]: config => {
  //   const { record } = config;
  //   const format = record.formValidate && record.formValidate.opt && record.formValidate.opt.format;
  //   return (
  //     <StaticDisplay format={format} />
  //   );
  // },
  [VIEW]: staticDisplay,
  [EDIT]: compRender
};
