const ContractCommercial = {
    route : 'contract/commercial/:groupId',
    templateUrl : 'js/app/views/contract/contract-commercial.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;

        this.render( { user : app.user });

        $container.find( '[data-id="btnConfirmActivationStart"]' ).first().click( function()
        {
            let $modalRoot = $container.find( '[data-id="modal-confirm-contract-test-activation"]' ).first();
            $modalRoot.find( '[data-id="section-activation-confirmation"]' ).first().show();
            $modalRoot.find( '[data-id="section-loader"]' ).first().hide();
            $modalRoot.on( 'shown.bs.modal', function()
            {
                $modalRoot.find( '[data-id="btnStartTestphase"]' ).first().off( "click" );
                $modalRoot.find( '[data-id="btnStartTestphase"]' ).first().click( function()
                {
                    $(this).off( "click" );

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

                    app.rpc.call(
                        'Contract',
                        'activateTestphase',
                        {
                            teamId : app.user.activeGroupId
                        },
                        function( resp )
                        {
                            if ( resp && resp.hasOwnProperty( 'code' ) && resp.code == 200 )
                            {
                                app.UI.toastSuccess( 'Vertragsmodul freigeschaltet' );
                                setTimeout( function()
                                {
                                    app.reload( '/dashboard/' + app.user.activeGroupId );
                                }, 250 )
                            }
                            else
                            {
                                app.UI.toastError();
                                $modalRoot.modal( 'hide' );
                            }
                        }
                    );
                });
            });
            $modalRoot.on( 'hidden.bs.modal', function()
            {
                $modalRoot.modal( 'dispose' );
            });
            $modalRoot.modal( 'show' );

        });

    },

    destroy : function()
    {

    }
};

export { ContractCommercial };