import React from 'react';
import { Form, Input, Select,  Button, Checkbox, Radio, Tooltip, Icon } from 'antd';
const FormItem = Form.Item;

const T = React.PropTypes;

const Checkboxes = React.createClass({
	propTypes: {
		label: T.string,
		labelColSpan: T.number,
		wrapperColSpan: T.number,
		help: T.string,
		tip: T.string,
		value: T.string,
		form: T.object.isRequired,
		path: T.string.isRequired,
		initialValue: T.object
	},
	getDefaultProps() {
		return {
			label: '多选',
			labelColSpan: 6,
			wrapperColSpan: 14,
			initialValue: {}
		}
	},
	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.renderCheckboxes()}
		    </FormItem>
		)
	},

	renderCheckboxes() {
		const props = this.props;
		const { getFieldProps} = this.props.form;

		return  props.checkboxes.map((ck) => {

			const fieldProps = getFieldProps(`${props.path}.${ck.name}`, {
				valuePropName: 'checked',
				rules: ck.rules,
				initialValue: props.initialValue[ck.name],
				onChange: function() {
					props.onFieldChange({
						path: `${props.path}.${ck.name}`,
						args: arguments
					})
				}
			});

			return (
				<Checkbox 
					{...fieldProps}
					className="ant-checkbox-vertical"
					key={ck.id}>
					{ck.label}
				</Checkbox>
			)
		})
	}
});

export default Checkboxes;