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';

import ForgetPw from './forgetPw.jsx';

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,
                    }
                }).done(function (res) {
                    if (res.error && res.error.length) {
                        message.warning(res.error[0].message);
                    } else {
                        let data = res.data;
                        if (data && data.requestURI) {
                            let path = globalConfig.context + data.requestURI
                            if (data.queryString) {
                                path += '?' + data.queryString;
                            }
                            if (window.location.hash) {
                                path += window.location.hash;
                            }
                            window.location.href = path;
                        } else {
                            window.location.href = globalConfig.context + "/user/account/index.html";
                        }
                    }
                }.bind(this)).always(function () {
                    this.props.spinState(false);
                }.bind(this));
            }
        });
    },
    render() {
        const { getFieldDecorator } = this.props.form;
        console.log(this.props.loginProp);
        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: false,
                    })(
                        <Checkbox>记住我</Checkbox>
                        )}
                    <ForgetPw theType={this.props.loginProp} />
                    <Button type="primary" htmlType="submit" className="login-form-button">
                        登录
                    </Button>
                </FormItem>
            </Form>
        );
    },
}));

export default LoginForm;