/**
 * Created by baoyinghai on 11/17/16.
 */
import React from 'react';
import { VIEW, EDIT } from '../../constant';
import { Radio } from '../../../../../components/mxa';

const RadioGroup = Radio.Group;

const compRender = config => {
  const { record, props } = config;

  const _onChange = e => {
    const func = props.propsFunc && props.propsFunc[record.name];
    func && func(e.target.value);
  };
  return (
    <RadioGroup onChange={_onChange} disabled={record.isReadonly || (props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly)}>
      {record.displayComponent && record.displayComponent.dictionaryItems && record.displayComponent.dictionaryItems.map((i, index) =>
        (
          <Radio key={'select_' + i.code} value={i.code}>{i.value}</Radio>
        ))}
    </RadioGroup>
  );
};

class StaticDisplay extends React.Component {
  render() {
    let value = this.props.value;
    this.props.opts && this.props.opts.forEach(item => {
      if (item.code === this.props.value) {
        value = item.value;
      }
    });
    return (
      <span>{value}</span>
    );
  }
}

export default {
  [VIEW]: config => {
    const { record } = config;
    const opts = record.displayComponent && record.displayComponent.dictionaryItems && record.displayComponent.dictionaryItems;
    return (
      <StaticDisplay opts={opts} />
    );
  },
  [EDIT]: compRender
};
