import React from 'react';
import { Form, Input, Select, Button, Checkbox, Radio, Tooltip, Icon } from 'antd';
const FormItem = Form.Item;

const RadioGroup = Radio.Group;
const T = React.PropTypes;

const Radios = React.createClass({
	propTypes: {
		label: T.string,
		labelColSpan: T.number,
		wrapperColSpan: T.number,
		help: T.string,
		tip: T.string,
		value: T.string,
		rules: T.array,
		radios: T.array,
		form: T.object.isRequired,
		path: T.string.isRequired
	},
	getDefaultProps() {
		return {
			label: '单选',
			labelColSpan: 6,
			wrapperColSpan: 14
		}
	},
	render() {

		const props = this.props;
		
		let itemProps = {
		    label: props.label,
		    labelCol: { span: props.labelColSpan },
		    wrapperCol: { span: props.wrapperColSpan }
		}

		if (props.help) {
			itemProps.help = props.help;
		}

		if (props.tip) {
			itemProps.label = (
				<span>
					{props.label}
					<Tooltip title={props.tip}>
					 	<Icon type="question-circle-o" />
					</Tooltip>
				</span>
			)
		}

		return (
			<FormItem {...itemProps}>
				{this.renderRadios()}
		    </FormItem>
		)
	},

	renderRadios() {
		const props = this.props;
		const { getFieldProps} = this.props.form;

		const fieldProps = getFieldProps(props.path, {
			rules: props.rules,
			initialValue: props.initialValue
		});

		if (props.defaultValue) {
			fieldProps.defaultValue = props.defaultValue
		}

		return (
			<RadioGroup {...fieldProps}>
				{props.radios.map(r => {
					return (
						<Radio 
							value={r.value} 
							key={r.value + r.label + props.id}>
							{r.label}
						</Radio>
					)
				})}
		    </RadioGroup>
		)
	}
});

export default Radios;