import React, { Component } from 'react';
import * as types from 'cem/constants/users';

import { reduxForm } from 'redux-form';
import { formFields } from 'cem/constants/users';
import validate from 'cem/validators/users';

import Select from 'react-select';
import { fetchResource } from 'cem/helpers/autocomplete';

import cn from 'classnames';
import styles from 'cem/styles/id/main';

import UI from 'cem/components/ui';
const { Grid, Form, /* Table, */ Button } = UI;

import Documents from './documents';

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

    if (formKey === `create`) {
      actions.createUser(values).then(({ type }) => {
        if (type === types.CREATE_ID_SUCCESS) resetForm();
      });
    } else {
      actions.updateUser(values);
    }
  }

  fetchDepartments(input, callback) {
    fetchResource(`/v1/dictionary/department`).then(({ body }) => {
      callback(null, { options: body.items }, { complete: true });
    });
  }

  fetchPositions(input, callback) {
    fetchResource(`/v1/dictionary/position`).then(({ body }) => {
      callback(null, { options: body.items }, { complete: true });
    });
  }

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

    return (
      <Grid.Row>
        <section className={styles.section}>
          <Form.Container onSubmit={handleSubmit(::this.createOrUpdate)}>
            <Grid.Row className={styles.pushedBottom30}>
              <Grid.Col sm="10">
                <Grid.Row>
                  <Grid.Col sm="18" md="12">
                    <h2 className={styles.title}>Отдел и должность</h2>

                    <Form.Group kind={fields.departmentId.touched && fields.departmentId.error && `error`}>
                      <Form.Label>Департамент</Form.Label>
                      <Select {...fields.departmentId} asyncOptions={this.fetchDepartments} labelKey="title" valueKey="id" onBlur={() => {}} />
                      {fields.departmentId.touched && fields.departmentId.error && <Form.Helper>{fields.departmentId.error}</Form.Helper>}
                    </Form.Group>

                    <Form.Group kind={fields.positionId.touched && fields.positionId.error && `error`}>
                      <Form.Label>Должность</Form.Label>
                      <Select {...fields.positionId} asyncOptions={this.fetchPositions} labelKey="title" valueKey="id" onBlur={() => {}} />
                      {fields.positionId.touched && fields.positionId.error && <Form.Helper>{fields.positionId.error}</Form.Helper>}
                    </Form.Group>
                  </Grid.Col>
                  {/* <Form.Group className={styles.resetMargin}>
                    <Form.Label>Руководитель</Form.Label>
                    <div className={styles.selectWrapper}>
                      <Form.Select kind="primary" block placeholder="Александр Оськин" {...fields.fullName} options={[
                        { value: `one`, label: `one` },
                        { value: `two`, label: `two` },
                      ]} />
                    </div>
                  </Form.Group> */}
                </Grid.Row>
              </Grid.Col>

              <Grid.Col sm="10">
                <Grid.Row>
                  <Grid.Col sm="18" md="12">
                    <h2 className={styles.title}>Логин и пароль</h2>

                    <Form.Group float kind={fields.email.touched && fields.email.error && `error`}>
                      <Form.Input block type="text" placeholder="Логин" {...fields.email} required />
                      <Form.Label>Логин</Form.Label>
                      {fields.email.touched && fields.email.error && <Form.Helper>{fields.email.error}</Form.Helper>}
                    </Form.Group>

                    <Form.Group float kind={fields.password.touched && fields.password.error && `error`}>
                      <Form.Input block type="password" {...fields.password} placeholder={formKey !== `create` && `******` || `Пароль`} required />
                      <Form.Label>Пароль</Form.Label>
                      {fields.password.touched && fields.password.error && <Form.Helper>{fields.password.error}</Form.Helper>}
                    </Form.Group>
                  </Grid.Col>
                </Grid.Row>
              </Grid.Col>
            </Grid.Row>

            <h2 className={styles.title}>Контактные данные</h2>
            <Grid.Row className={styles.pushedBottom30}>
              <Grid.Col sm="10">
                <Grid.Row>
                  <Grid.Col sm="18" md="12">
                    <Form.Group float kind={fields.workPhoneNumber.touched && fields.workPhoneNumber.error && `error`}>
                      <Form.Input block type="tel" {...fields.workPhoneNumber} mask="+7 (111) 111-11-11" placeholder="+7 (___) ___ - __ - __" required />
                      <Form.Label>Рабочий телефон</Form.Label>
                      {fields.workPhoneNumber.touched && fields.workPhoneNumber.error && <Form.Helper>{fields.workPhoneNumber.error}</Form.Helper>}
                    </Form.Group>
                    <Form.Group float kind={fields.email.touched && fields.email.error && `error`}>
                      <Form.Input block type="text" placeholder="Рабочий email" {...fields.email} required />
                      <Form.Label>Рабочий email</Form.Label>
                      {fields.email.touched && fields.email.error && <Form.Helper>{fields.email.error}</Form.Helper>}
                    </Form.Group>
                  </Grid.Col>
                </Grid.Row>
              </Grid.Col>
              <Grid.Col sm="10">
                <Grid.Row>
                  <Grid.Col sm="18" md="12">
                    <Form.Group float kind={fields.personalPhoneNumber.touched && fields.personalPhoneNumber.error && `error`}>
                      <Form.Input block type="tel" {...fields.personalPhoneNumber} mask="+7 (111) 111-11-11" placeholder="+7 (___) ___ - __ - __" required />
                      <Form.Label>Личный телефон</Form.Label>
                      {fields.personalPhoneNumber.touched && fields.personalPhoneNumber.error && <Form.Helper>{fields.personalPhoneNumber.error}</Form.Helper>}
                    </Form.Group>
                    <Form.Group float kind={fields.personalEmail.touched && fields.personalEmail.error && `error`}>
                      <Form.Input block type="text" placeholder="Личный e-mail" {...fields.personalEmail} required />
                      <Form.Label>Личный e-mail</Form.Label>
                      {fields.personalEmail.touched && fields.personalEmail.error && <Form.Helper>{fields.personalEmail.error}</Form.Helper>}
                    </Form.Group>
                  </Grid.Col>
                </Grid.Row>
              </Grid.Col>
            </Grid.Row>
            {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>}
          </Form.Container>

          {formKey !== `create` &&
            <Grid.Row>
              <Grid.Col xs="20">
                <Documents resourceId={formKey} />
              </Grid.Col>
            </Grid.Row>
          }
        </section>
      </Grid.Row>
    );
  }
}

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

export default reduxForm(formSettings)(About);
