import React from 'react';
import { Form, Icon, Input, Button, Checkbox, Spin } 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.setState({
                    loading: true
                });
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/signin",
                    data: { 
                        user: values.userName, 
                        pwd: values.password, 
                        remember:values.remember,
                        group: this.props.loginProp === "2" ? true : false
                     }
                }).done(function (data) {
                    if (!data.error.length) {
                        window.location.href = "warning.html"
                    } else {
                        this.tips(data.error[0].message);
                    }
                }.bind(this)).always(function () {
                    this.setState({
                        loading: false
                    });
                }.bind(this));
            }
        });
    },
    render() {
        const { getFieldDecorator } = this.props.form;
        return (
            <Spin spinning={this.state.loading}>
                <Form onSubmit={this.handleSubmit} className="login-form">
                    <FormItem>
                        {getFieldDecorator('userName', {
                            rules: [{ required: true, message: 'Please input your username!' }],
                        })(
                            <Input addonBefore={<Icon type="user" />} placeholder="Username" />
                            )}
                    </FormItem>
                    <FormItem>
                        {getFieldDecorator('password', {
                            rules: [{ required: true, message: 'Please input your Password!' }],
                        })(
                            <Input addonBefore={<Icon type="lock" />} type="password" placeholder="Password" />
                            )}
                    </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>
            </Spin>
        );
    },
}));

export default LoginForm;