var Login = React.createClass({
    getInitialState: function () {
        return ({
            user: {},
            formErrors: [],
            role: 0
        })
    },
    componentWillUnmount: function () {
        if(!Back)
        {
            Navigation.push("login");
        }
        else
        {
            Back = false;
        }
    },
    handleClickLogIn: function (e) {
        e.preventDefault();
        errors= [];
        var user = this.refs.user.getDOMNode().value;
        var pass = this.refs.pass.getDOMNode().value;
        //console.log("HANDLE CLICK LOGIN");
        if(user.length == 0)
        {
            errors.push("User can not be left empty");
        }
        if(pass.length == 0)
        {
            errors.push("Password can not be left empty");
        }

        this.setState({formErrors: errors})
        if(errors.length == 0)
        {
            var loadRequest = $.ajax({
                type: 'GET',
                url: "/authenticate/" + user + "/" + pass,
                dataType: "json",
                success: function(data){
                    console.log(data);
                    if(data != -99)
                        role = 0;
                    if(data[0] != undefined && "termid" in data[0])
                        role = data[0].roleid;
                    else
                        role = data;

                    if(role == 1)
                        e = 'studentHome';
                    else if(role == 2)
                        e = 'judgeHome';
                    else if(role == 3)
                        e = 'adminHome';
                    if(role != 0 && role != -1)
                    {
                        //console.log("LOG IN SUCESSFULL", role);
                        //console.log("USER AT LOGIN ", data[0]);
                        this.props.handleUser(data[0]);
                        this.props.handler(e);
                    }
                    else
                    {
                        console.log("DATA ", data);
                        if(data == -1)
                            errors.push("The login for judges has not yet been activated by the admin. Please try again during the event.");
                        else
                            errors.push("The information you entered does not match any of our active records");
                        this.setState({formErrors: errors});
                    }
                }.bind(this)
                });
        }
    },

    handleClickForgotPass: function (e) {
        e.preventDefault();
        e = 'forgotPass';
        this.props.handler(e);
    },

    render: function () {
        //console.log("RENDERED LOGIN");
        return (
            <div className="container loginView judgeView">
                <h1 className="title-form">Please Log In</h1>

                <form>
                    <Alert show={this.state.formErrors.length > 0} type="danger" messages={this.state.formErrors}/>
                    <div className="form-group">
                        <input ref="user" type="email" className="form-control input-app input-upper" id="exampleInputEmail1"
                               placeholder="email@example.com"/>
                        <input ref="pass" type="password" className="form-control input-app" id="exampleInputPassword1"
                               placeholder="password"/>
                    </div>

                    <div className="form-group">
                        <div className='input-group full-width'>
                            <button type='submit' id='loginme' className='btn btn-lightblue btn-block' onClick={this.handleClickLogIn}>
                                Login
                            </button>
                        </div>
                    </div>

                    <div className="form-group">
                        <div className='input-group full-width'>
                            <button type='button' id='forgotpass' className='btn btn-lightgrey btn-block' onClick={this.handleClickForgotPass}>
                                Forgot your Password?
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        );
    }
});