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

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

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

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

        app.core.View.setAuthContent(
            app.core.View.getTemplate( 'auth-forgot-password' )
        );

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

            if ( isValid )
            {
                app.core.Rpc.call(
                    'Auth',
                    'requestPasswordReset',
                    {
                        "email" : $form.find( '[data-id="input-forgot-password-email"]' ).first().val()
                    },
                    function( res )
                    {
                        if ( res && res.res )
                        {
                            if ( res.res === "success" )
                            {
                                $authContent.find( '[data-id="section-form-request-password-reset"]' ).first().hide();
                                $authContent.find( '[data-id="section-success-request-password-reset"]' ).first().show();
                            }
                            else
                            {
                                app.core.View.toastError( _lc( 'EMAIL_NOT_FOUND' ) );
                            }
                        }
                    }
                );
            }

            return false;
        });


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

    return state;
};