import React, { Component } from 'react';
import UI from 'staff-legacy/components/ui';

import { history } from 'staff-legacy/config/routes';

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as AuthActions from 'core/actions/auth';

// import styles from 'styles/login';
const styles = {};

const {
  Grid, Grid: { Row, Col },
  Form, Form: { Input, Group},
  Button,
} = UI;

class SignInContainer extends Component {
  componentWillReceiveProps(props) {
    if (props.auth.token && this.props.auth.token !== props.auth.token) {
      history.push(`/`);
    }
  }

  handleOnSubmit(e, data) {
    e.preventDefault();

    this.props.actions.login(data);
  }

  render() {
    return (
      <Grid.Container>
        <Row>
          <Col sm="4" smOffset="4">
            <div className={styles.panel}>
              <h3 className={styles.textCenter}>Log In {this.props.auth && this.props.auth.inProccess && <span>LOADING</span>}</h3>

              <Form.Container onSubmit={::this.handleOnSubmit}>
                <Group size="lg">
                  <Input name="email" type="email" placeholder="E-mail" block />
                </Group>

                <Group size="lg">
                  <Input name="password" type="password" placeholder="Password" block />
                </Group>

                <Button type="submit" kind="primary" size="lg" block>Sign In</Button>
              </Form.Container>
            </div>
          </Col>
        </Row>
      </Grid.Container>
    );
  }
}

const mapState = ({ auth }) => ({
  auth,
});

const mapDispatch = (dispatch) => ({
  actions: bindActionCreators(AuthActions, dispatch),
});


export default connect(mapState, mapDispatch)(SignInContainer);
