import React, { Component } from 'react';
import { GoogleMap, Marker } from 'react-google-maps';
import { Link } from 'react-router';
import Form from 'react-auto-form';

import Helmet from 'react-helmet';
import { helmet } from 'core/config/constants';

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as EmailActions from 'jqestate.ru/actions/email';
import { company } from 'core/config/constants';

import UI from 'jqestate.ru/components/ui';
import 'jqestate.ru/styles/components/pages/feedback';

class FeedbackContainer extends Component {
  componentWillUnmount() {
    this.props.actions.flushEmail(`request_feedback_client`);
  }

  onSubmit(e, user) {
    e.preventDefault();

    const { email } = user;
    const template = {
      user,
      props: {
        company,
      },
    };

    this.props.actions.sendEmail(`jq_request_feedback_client`, email, { template });
    this.props.actions.sendEmail(`jq_request_feedback`, company.email, { template });
  }

  containerProps = {
    style: {
      height: `30rem`,
    },
  };

  position = {
    lat: 55.736646,
    lng: 37.247156,
  };

  options = {
    styles: [{
      featureType: `poi`,
      stylers: [{
        visibility: `off`,
      }],
    }],
    scrollwheel: false,
  };

  // isDisabled(...args) {
  //   console.log(this.refs.fullName.value);
  //   console.log(Object.keys(this.refs).filter(item => this.refs[item]));
  // }

  renderForm() {
    const form = this.props.state.emails[`jq_request_feedback_client`] || {};
    const { inProgress, success, error } = form;

    if (inProgress) {
      return <UI.Loading />;
    }

    if (success) {
      return <UI.Alert message="Письмо отправлено!" />;
    }

    return (
      <section>
        {error &&
          <UI.Alert message={error.message} />
        }

        <Form className="feedback-form" name="requestForm" onSubmit={::this.onSubmit}>
          <UI.Row>
            <div className="col-md-6">
              <div className="form-group">
                <UI.Form.Input kind="primary" block size="sm" type="text" ref="fullName" name="fullName" placeholder="Имя *" className="request-input" />
                {/* <p className="help-block">Это поле обязательно</p> */}
              </div>
              <div className="form-group">
                <UI.Form.Input kind="primary" block size="sm" type="email" ref="email" name="email" placeholder="E-mail *" className="request-input" />
                {/* <p className="help-block">Это поле обязательно</p> */}
              </div>
              <div className="form-group">
                <UI.Form.Input kind="primary" block size="sm" type="tel" ref="phoneNumber" name="phoneNumber" mask="+1 (111) 111-11-11" placeholder="+_ (___) ___ - __ - __" className="request-input" />
              </div>
              <div className="form-group">
                <textarea ref="message" name="message" rows="3" placeholder="Ваши мысли?" className="form-control form-control-block form-control-sm form-control-primary request-input" />
              </div>
              <UI.Button kind="accent" size="xlg" className="text-weight-200 text-uppercase" block>Отправить</UI.Button>
            </div>
          </UI.Row>
        </Form>
      </section>
    );
  }

  render() {
    return (
      <section className="feedback">
      <Helmet
        title={helmet.pages.feedback.title}
        meta={[
          { name: `description`, content: helmet.pages.feedback.description },
          { name: `keywords`, content: helmet.pages.feedback.keywords },
        ]}
      />
        <div className="hidden-xs">
          <GoogleMap ref="map" defaultZoom={15} defaultCenter={this.position} defaultOptions={this.options} containerProps={this.containerProps} googleMapsApi={typeof google !== `undefined` ? google.maps : null}>
            <Marker position={this.position} key="Офис JQ Estate" />
          </GoogleMap>
        </div>

        <UI.Container>
          <UI.Row>
            <div className="col-sm-6 feedback-block" itemScope="" itemType="http://schema.org/LocalBusiness">
              <span className="hidden" itemProp="name">Рублёвка.ру</span>
              <h4 className="heading-brand feedback-heading-brand" itemProp="telephone">
                <a className="feedback-heading-brand-link-phone" href="tel:+74951200000">+7 (495) 120-00-00</a>
              </h4>
              <h4 className="feedback-heading-brand">
                <a className="feedback-heading-brand-link-email" href="mailto:info@rublevka.ru">info@jqestate.ru</a>
              </h4>
              <time className="feedback-heading-brand hidden-xs" itemProp="openingHours" dateTime="Mo,Tu,We,Th,Fr,Sa,Su 10:00−20:00">
                С 10:00 до 20:00
              </time>
              <p className="feedback-address" itemProp="address" itemScope="" itemType="http://schema.org/PostalAddress">
                <span itemProp="streetAddress">
                  Рублево-Успенское шоссе 9 км, Дер.<br className="visible-xs" /> Жуковка дом 71, Бизнес Центр <br className="visible-xs" />«ОЛИМП»
                </span>
              </p>
              <div className="footer-social feedback-social btn-group  hidden-xs">
                <Link to="https://www.facebook.com/" traget="_blank" className="footer-social-icon footer-social-icon-facebook">
                  <UI.Icon icon="social-facebook" />
                </Link>
                <Link to="https://twitter.com/" traget="_blank" className="footer-social-icon footer-social-icon-twitter">
                  <UI.Icon icon="social-twitter" />
                </Link>
                <Link to="http://instagram.com/" traget="_blank" className="footer-social-icon footer-social-icon-vk">
                  <UI.Icon icon="social-vk" />
                </Link>
              </div>
            </div>
            <div className="col-sm-6 feedback-block feedback-block-form  hidden-xs">
              <h4 className="feedback-heading-brand">
                Обратная связь
              </h4>

              {this.renderForm()}
            </div>
          </UI.Row>
        </UI.Container>
        <UI.Container fluid>
          <UI.Row>
            <img className="feedback-xs-image visible-xs" src="http://d1g42dx7xlztem.cloudfront.net/c88bcbea-00c6-48fe-b369-60f9d5dfd043-538" alt="Офис JQ Estate" title="" />
          </UI.Row>
        </UI.Container>
      </section>
    );
  }
}

const pickState = ({ emails }) => ({
  state: { emails },
});

const pickActions = (dispatch) => ({
  actions: bindActionCreators(EmailActions, dispatch),
});

const reduxConnector = connect(pickState, pickActions)(FeedbackContainer);

export default reduxConnector;
