const SettingsAccess = {
    route : 'settings/access/:groupId',
    isDefault : true,
    templateUrl : 'js/app/views/settings/settings-access.html',
    navigatioData : null,
    redirectUrl : 'auth/login',

    canAccess : function()
    {
        return this.app.user.isLoggedIn();
    },

    create : function( p )
    {
        this.container = this.app.UI.getContentContainer();

        const $container = $( this.container ),
            app = this.app,
            admins = [],
            self = this,
            groupId = p.get( 'groupId' );

        this.render();

        app.rpc.call(
            'Team',
            'getDetails',
            {
                teamId : groupId
            },
            function( res )
            {
                if ( res && res.hasOwnProperty( 'data' ) && res.data.hasOwnProperty( 'members' ) )
                {
                    let m;
                    for ( let mi = 0; mi < res.data.members.length; mi++ )
                    {
                        m = self.createInstance( 'UserProfile', res.data.members[ mi ] );
                        if ( m.isAdminOfGroup( groupId ) )
                        {
                            m.setContextGroupId( groupId );
                            admins.push( m );
                        }
                    }

                    let adt = self.createComponent(
                        'access-data-table',
                        $container.find( '[data-id="container-access-data-table"]' ).first().get( 0 ),
                        {
                            members : admins
                        }
                    );
                    $container.find( '[data-id="ibox-access-data-table"]' ).toggleClass( 'sk-loading' );

                    adt.addEventListener( 'grantManagerAccess', function( e )
                    {
                        let memberId = e.data.memberId;

                        let $modalRoot = $container.find( '[data-id="modal-confirm-grant-manager-access"]' ).first();
                        $modalRoot.find( '[data-id="section-confirm-manager-access"]' ).first().show();
                        $modalRoot.find( '[data-id="section-loader"]' ).first().hide();
                        $modalRoot.on( 'shown.bs.modal', function()
                        {
                            $modalRoot.find( '[data-id="btnSubmitGrant"]' ).first().off( "click" );
                            $modalRoot.find( '[data-id="btnSubmitGrant"]' ).first().click( function()
                            {
                                $(this).off( "click" );

                                $modalRoot.find( '[data-id="section-confirm-manager-access"]' ).first().hide();
                                $modalRoot.find( '[data-id="section-loader"]' ).first().show();

                                app.rpc.call(
                                    'Profile',
                                    'grantManagerAccess',
                                    {
                                        profileId : memberId,
                                        teamId : groupId
                                    },
                                    function( resp )
                                    {
                                        if ( resp && resp.hasOwnProperty( 'code' ) && resp.code == 200 )
                                        {
                                            app.UI.toastSuccess( 'Aktion erfolgreich.' );
                                            app.reload();
                                        }
                                        else
                                        {
                                            app.UI.toastError();
                                        }
                                        $modalRoot.modal( 'hide' );
                                    }
                                );
                            });
                        });
                        $modalRoot.on( 'hidden.bs.modal', function()
                        {
                            $modalRoot.modal( 'dispose' );
                        });
                        $modalRoot.modal( 'show' );
                    });

                    adt.addEventListener( 'removeManagerAccess', function( e )
                    {
                        let memberId = e.data.memberId;

                        let $modalRoot = $container.find( '[data-id="modal-confirm-remove-manager-access"]' ).first();
                        $modalRoot.find( '[data-id="section-confirm-manager-access"]' ).first().show();
                        $modalRoot.find( '[data-id="section-loader"]' ).first().hide();
                        $modalRoot.on( 'shown.bs.modal', function()
                        {
                            $modalRoot.find( '[data-id="btnSubmitRemove"]' ).first().off( "click" );
                            $modalRoot.find( '[data-id="btnSubmitRemove"]' ).first().click( function()
                            {
                                $(this).off( "click" );

                                $modalRoot.find( '[data-id="section-confirm-manager-access"]' ).first().hide();
                                $modalRoot.find( '[data-id="section-loader"]' ).first().show();

                                app.rpc.call(
                                    'Profile',
                                    'removeManagerAccess',
                                    {
                                        profileId : memberId,
                                        teamId : groupId
                                    },
                                    function( resp )
                                    {
                                        if ( resp && resp.hasOwnProperty( 'code' ) && resp.code == 200 )
                                        {
                                            if ( app.user.userProfile.get( 'id' ) == memberId )
                                            {

                                                app.UI.toastWarning( 'Sie haben sich selbt den Zugriff entzogen und werden ausgeloggt.' );
                                                setTimeout( function()
                                                {
                                                    app.user.logout();
                                                }, 1000 );
                                            }
                                            else
                                            {
                                                app.UI.toastSuccess( 'Aktion erfolgreich.' );
                                                app.reload();
                                            }
                                        }
                                        else
                                        {
                                            app.UI.toastError();
                                        }
                                        $modalRoot.modal( 'hide' );
                                    }
                                );
                            });
                        });
                        $modalRoot.on( 'hidden.bs.modal', function()
                        {
                            $modalRoot.modal( 'dispose' );
                        });
                        $modalRoot.modal( 'show' );
                    });
                }
            }
        );
    },

    destroy : function()
    {

    }
};

export { SettingsAccess };