import React from 'react';
import { Form, Icon, Input, Button, Checkbox, Spin,message } from 'antd';
import './login.less';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';
const FormItem = Form.Item;

const LoginForm = Form.create()(React.createClass({
    getInitialState() {
        return {
            loading: false
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.props.spinState(true);
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    url: globalConfig.context + "/signin",
                    data: {
                        "mobile": values.phone,
                        "password": values.password,
                        "type": this.props.loginProp,
                        "remember": values.remember
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        window.location.href = "account/index.html"
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this)).always(function () {
                    this.props.spinState(false);
                }.bind(this));
            }
        });
    },
    render() {
        const { getFieldDecorator } = this.props.form;
        return (
                <Form onSubmit={this.handleSubmit} className="login-form">
                    <FormItem>
                        {getFieldDecorator('phone', {
                            rules: [{ required: true, message: '请输入手机号!' }],
                        })(
                            <Input addonBefore={<Icon type="user" />} placeholder="手机号" />
                            )}
                    </FormItem>
                    <FormItem>
                        {getFieldDecorator('password', {
                            rules: [{ required: true, message: '请输入密码!' }],
                        })(
                            <Input addonBefore={<Icon type="lock" />} type="password" placeholder="密码" />
                            )}
                    </FormItem>
                    <FormItem>
                        {getFieldDecorator('remember', {
                            valuePropName: 'checked',
                            initialValue: true,
                        })(
                            <Checkbox>记住我</Checkbox>
                            )}
                        <a className="login-form-forgot">忘记密码</a>
                        <Button type="primary" htmlType="submit" className="login-form-button">
                            登录
                    </Button>
                    </FormItem>
                </Form>
        );
    },
}));

export default LoginForm;