import React, { Component } from 'react';

import { AbstractForm, ButtonBar, Button } from 'lib/components';

import TaxItems from 'components/TaxItems';
import Toast from 'components/Toast';

import form from 'lib/form';
import validator from 'lib/validator';

import { injectIntl } from 'react-intl';
import messages from './messages.js';
import commonMessages from '../../messages.js';


class CustomerContactForm extends AbstractForm {


	render() {
		let { valueOf, i18n, intl } = this.props;

 		return (
			<div className={ this.props.className }>

				<div className="row row-margin-10">
					<div className="col-xs-12 p10">
						<ButtonBar>
							<Button 
								title={ intl.formatMessage(commonMessages.buttonCancel) }
								onPress={ typeof this.props.onCancel == "function" ? () => this.props.onCancel() : null }
								/>
							<Button 
								title={ intl.formatMessage(commonMessages.buttonSave) }
								onPress={ () => this.props.submit(this.props.onSubmit) }
								/>
						</ButtonBar>
					</div>
				</div>

            	<div className="row row-margin-10">
            		<div className="col-xs-12 p10">
						<div className="form">
							<div className="row row-margin-10">
								<div className="col-md-12 p10">
									<div className="row row-margin-10">
										<div className="col-xs-12">
											{ this.renderField("name", intl.formatMessage(messages.name)) }
											{ this.renderField("email", intl.formatMessage(messages.email)) }
											{ this.renderField("phone", intl.formatMessage(messages.phone)) }
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		);
	}


}


export default form(injectIntl(CustomerContactForm), {
	onValidationFailed: Toast.validationFailed,
	validate: (item, error) => {
		let valid = true;

		if (!validator.required(item.name)) {
			error({ field: "name", code: messages.nameRequiredError.id });
			valid = false;
		}
		if (!validator.required(item.email) && !validator.required(item.phone)) {
			error({ field: ["email", "phone"], code: messages.emailOrPhoneRequiredError.id });
			valid = false;
		}
		if (item.email && !validator.email(item.email)) {
			error({ field: "email", code: messages.invalidEmailError.id });
			valid = false;
		}

		return valid;
	}
});
