/**
 * (c) by aheadware.com
 */

var app = app || {};
app.state = app.state || {};

app.state.AuthResetPassword = function()
{
    var state = app.core.StateManager.createState( 'auth-reset-password' );

    state.onEnter = function( p )
    {
        let $authContent = app.core.View.getAuthContent(),
            token = p.token || null;

        app.core.Rpc.call(
            'Auth',
            'getToken',
            {
                token: token
            },
            function( tokenData )
            {
                var actionToken  = new app.model.ActionToken( tokenData );

                app.core.View.setAuthContent(
                    app.core.View.getTemplate( 'auth-reset-password', { actionToken : actionToken } )
                );

                $authContent.find( '[data-id="btn-save-new-password"]' ).click( function()
                {
                    let $form = $authContent.find( '[data-id="form-reset-password"]' ).first(),
                        isValid = app.util.Form.bootstrapValidate( $form );

                    if ( isValid )
                    {
                        app.core.Rpc.call(
                            'Auth',
                            'resetPassword',
                            {
                                token : token,
                                newPassword : $.trim( $form.find( '[data-id="input-new-password"]' ).first().val() )
                            },
                            function( res )
                            {
                                $authContent.find( '[data-id="section-form-reset"]' ).hide()
                                $authContent.find( '[data-id="section-success-reset-password"]' ).show();
                            }
                        );
                    }
                });

                app.gui.PageLoader.hide();
            }
        );
    };

    return state;
};