import React, { Component } from 'react';
import UI from 'cem/components/ui';

import About from 'cem/components/properties/id/about/index';
import { reduxForm } from 'redux-form';
import validate from 'cem/validators/properties';

import constants from 'cem/constants/properties';
const { formFields } = constants;

import cn from 'classnames';
import styles from 'cem/styles/id/main';
console.log(styles);

const { Grid, Form, Button } = UI;

class AboutComponent extends Component {
  createOrUpdate() {
    const { formKey, values, actions } = this.props;

    if (formKey === `create`) actions.createProperty(values);
    if (formKey !== `create`) actions.updateProperty(formKey, values);
  }

  render() {
    const { formKey, pristine, handleSubmit } = this.props;

    return (
      <Form.Container onSubmit={handleSubmit(::this.createOrUpdate)}>
        <Grid.Row>
          <section className={styles.section}>
            <About.Deals {...this.props} />
            <About.Apartment className={styles.pushedBottom30} {...this.props} />
            <About.Equipment className={styles.pushedBottom60} {...this.props} />
            <About.Building className={styles.pushedBottom30} {...this.props} />
            <About.Address className={styles.pushedBottom30} {...this.props} />
            <About.Contacts className={styles.pushedBottom30} propertyId={this.props.formKey} />
            <About.Status className={styles.pushedBottom30} {...this.props} />
          </section>

          {formKey === `create` && <Button className={cn(styles.btnSave, { [styles.btnHidden]: pristine })} type="submit" kind="success" size="md" block>Добавить</Button>}
          {formKey !== `create` && <Button className={cn(styles.btnSave, { [styles.btnHidden]: pristine })} type="submit" kind="warning" size="md" block>Сохранить</Button>}
        </Grid.Row>
      </Form.Container>
    );
  }
}

const formSettings = {
  form: `property`,
  fields: formFields,
  destroyOnUnmount: false,
  validate,
};

export default reduxForm(formSettings)(AboutComponent);
