import React from 'react';
import ReactDOM from 'react-dom';
import { Input, Button, Checkbox, message, Spin, Icon } from 'antd';
import './adminLogin.less';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';

var Login = React.createClass({
    getInitialState() {
        return {
            user: '',
            password: '',
            checked: '',
            loading: false
        };
    },

    handleSubmit() {
        if (this.state.user === "" || this.state.password === "") {
            this.tips('账号或者密码不能为空！');
        } else {
            this.setState({
                loading: true
            });
            $.ajax({
                method: "POST",
                dataType: "json",
                crossDomain: false,
                url: globalConfig.context + "/managesignin",
                data: {
                    mobile: this.state.user,
                    password: this.state.password,
                    remeber: false
                }
            }).done(function (data) {
                if (!data.error.length) {
                    window.location.href =  globalConfig.context + "/admin/userManage.html";
                } else {
                    this.tips(data.error[0].message);
                }
            }.bind(this)).always(function () {
                this.setState({
                    loading: false
                });
            }.bind(this));
        };
    },

    keyboard(event) {
        if (event.keyCode == 13) {
            this.handleSubmit()
        }
    },

    tips: function (text) {
        message.warning(text);
    },

    getUser(event) {
        this.setState({ user: event.target.value });
    },

    getPassword(event) {
        this.setState({ password: event.target.value });
    },

    onChange(event) {
        this.setState({ checked: event.target.checked });
    },

    render() {
        return (
            <div className="login-box">
                <Spin spinning={this.state.loading} className='spin-box'>
                    <div className="user-box">
                        <Input onChange={this.getUser}
                            onKeyDown={this.keyboard} placeholder="账号" value={this.state.user}
                            addonBefore={<Icon type="user" />} />
                    </div>
                    <div className="pw-box">
                        <Input onChange={this.getPassword}
                            onKeyDown={this.keyboard} type="password" placeholder="密码" value={this.state.password}
                            addonBefore={<Icon type="lock" />}
                        />
                    </div>
                    <Button type="primary" onClick={this.handleSubmit}>登录</Button>
                </Spin>
            </div>
        );
    }
});

export default Login;