import React, { Component } from 'react';

import { AbstractForm, ButtonBar, Button } from 'lib/components';
import TaxItems from 'components/TaxItems';
import Attachments from 'lib/attachment';
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';


//FIXME translate
require("./InvoiceEmailForm.scss")

class InvoiceEmailForm extends AbstractForm {


	render() {
		let { attachments, intl, disabled } = this.props;
		let fieldProps = { disabled };

		return (
			<div className="form email-form">
				<div className="row row-margin-10">
					<div className="col-xs-12">
						<div className="row row-margin-10">
							{ this.renderField("from", intl.formatMessage(messages.from), fieldProps) }
							{ this.renderMultiField("to", intl.formatMessage(messages.to), fieldProps) }
							{ this.renderField("subject", intl.formatMessage(messages.subject), fieldProps) }
							{ this.renderField("title", intl.formatMessage(messages.title), fieldProps) }
							{ this.renderTextArea("message", intl.formatMessage(messages.message), fieldProps) }
						</div>
						<div className="row row-margin-10">
							<div className="col-sm-6">
								{ this.renderTextArea("signature", intl.formatMessage(messages.signature), fieldProps) }
							</div>
							<div className="col-sm-6">
								{ this.renderField("filename", intl.formatMessage(messages.filename), fieldProps) }
								{ this.renderCheckbox("addAttachments", intl.formatMessage(messages.addAttachments), fieldProps) }
								{ this.renderCheckbox("publicLink", intl.formatMessage(messages.publicLink), fieldProps) }
								{ this.renderCheckbox("requestReceipt", intl.formatMessage(messages.requestReceipt), fieldProps) }
								{ this.renderCheckbox("includeQrCode", intl.formatMessage(messages.includeQrCode), fieldProps) }
							</div>
						</div>

					</div>
				</div>
			</div>
		);
	}

}


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

		let to = email.to;
		if (!Array.isArray(to) || !to.length) {
			//this should never happen
			error({ field: "to", code: "common.validation.requiredField" })
			valid = false;
		} else {
			//validate each email address
			for (let i = 0; i < to.length; i++) {
				let s = to[i].trim();
				if (s && !validator.email(s)) {
					error({ field: "to", code: "common.validation.invalidEmail" })
					valid = false;
					break;
				}				
			}
		}

		if (!validator.required(email.subject)) {
			error({ field: "subject", code: "common.validation.requiredField" })
			valid = false;
		}

		if (!validator.required(email.title)) {
			error({ field: "title", code: "common.validation.requiredField" })
			valid = false;
		}

		if (!validator.required(email.message)) {
			error({ field: "message", code: "common.validation.requiredField" })
			valid = false;
		}

		if (!validator.required(email.filename)) {
			error({ field: "filename", code: "common.validation.requiredField" })
			valid = false;
		}

		return valid;
	}
});

