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

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

app.state.ConfigurationProfileDelete = function()
{
    var state = app.core.StateManager.createState( 'configuration-profile-delete' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent();

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Account',
            'getAccountRelatedData',
            null,
            function( res ) {
                app.gui.PageLoader.hide();

                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'configuration-profile-delete',
                        {
                            profile: new app.model.Profile(res.profile)
                        }
                    )
                );

                $content.find('[data-id="btn-delete-profile"]').click(function ()
                {
                    let $form = $content.find('[data-id="form-profile-deletion"]').first(),
                        isValid = app.util.Form.bootstrapValidate($form);

                    if (isValid)
                    {
                        app.gui.PageLoader.show();
                        app.core.Rpc.call(
                            'Account',
                            'delete',
                            {

                            },
                            function( res )
                            {
                                app.core.NotificationManager.destroy();
                                app.core.Session.destroy();
                                app.gui.PageLoader.hide();
                                app.core.Controller.redirect( '#/auth/login' );
                            }
                        );
                    }
                });
            }
        );
    };

    return state;
};