const ContractList = {
    route : 'contract/list/:groupId',
    templateUrl : 'js/app/views/contract/contract-list.html',
    navigatioData : null,
    redirectUrl : 'auth/login',

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

    create : function( p )
    {
        this.container = this.app.UI.getContentContainer();
        self.contracts = [];
        const $container = $( this.container ),
            app = this.app,
            groupId = p.get( 'groupId' ),
            members = [],
            apps = [];

        this.render( { groupId : groupId } );

        this.app.rpc.call(
            'Contract',
            'getList',
            {
                teamId : groupId,
                state : 'active'
            },
            function( res )
            {
                if ( res && res.hasOwnProperty( 'data' ) && res.data.hasOwnProperty( 'contractData' ) )
                {
                    const cs = res.data.contractData;
                    self.contracts = [];
                    for( let ci = 0; ci < cs.length; ci++ )
                    {
                        self.contracts.push( this.createInstance( 'Contract', cs[ ci ] ) );
                    }
                }

                let compDt = this.createComponent(
                    'contract-data-table',
                    $container.find( '[f-id="container-contract-data-table"]' ).first().get( 0 ),
                    {
                        contracts : self.contracts,
                        groupId : groupId
                    }
                );

                compDt.addEventListener( 'singleContractArchiving', function( e )
                {
                    let contractId = e.data.contractId;

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

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

                            app.rpc.call(
                                'Contract',
                                'batchArchiving',
                                {
                                    contractIds : [ contractId ]
                                },
                                function( resp )
                                {
                                    if ( resp && resp.hasOwnProperty( 'code' ) && resp.code == 200 )
                                    {
                                        app.UI.toastSuccess( 'Vertrag erfolgreich ins Archiv verschoben.' );
                                        app.reload();
                                    }
                                    else
                                    {
                                        app.UI.toastError();
                                    }
                                    $modalRoot.modal( 'hide' );
                                }
                            );
                        });
                    });
                    $modalRoot.on( 'hidden.bs.modal', function()
                    {
                        $modalRoot.modal( 'dispose' );
                    });
                    $modalRoot.modal( 'show' );

                });

                $container.find( '[data-id="btnArchiveContracts"]' ).first().click( function()
                {
                    let contractIds = compDt.getSelectedIds();
                    if ( 0 === contractIds.length )
                    {
                        app.UI.toastError( 'Bitte wählen Sie zunächst die Verträge aus.', 'Keine Verträge ausgewählt' );
                        return false;
                    }

                    let $modalRoot = $container.find( '[data-id="modal-confirm-multiple-archive"]' ).first();
                    $modalRoot.find( '[data-id="section-single-archive-confirmation"]' ).first().show();
                    $modalRoot.find( '[data-id="section-loader"]' ).first().hide();
                    $modalRoot.on( 'shown.bs.modal', function()
                    {
                        $modalRoot.find( '[data-id="btnMoveToArchive"]' ).first().off( "click" );
                        $modalRoot.find( '[data-id="btnMoveToArchive"]' ).first().click( function()
                        {
                            $(this).off( "click" );

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

                            app.rpc.call(
                                'Contract',
                                'batchArchiving',
                                {
                                    contractIds : contractIds
                                },
                                function( resp )
                                {
                                    if ( resp && resp.hasOwnProperty( 'code' ) && resp.code == 200 )
                                    {
                                        app.UI.toastSuccess( 'Verträge erfolgreich ins Archiv verschoben.' );
                                        app.reload();
                                    }
                                    else
                                    {
                                        app.UI.toastError();
                                    }
                                    $modalRoot.modal( 'hide' );
                                }
                            );
                        });
                    });
                    $modalRoot.on( 'hidden.bs.modal', function()
                    {
                        $modalRoot.modal( 'dispose' );
                    });
                    $modalRoot.modal( 'show' );

                });

                $container.find( '.sk-loading' ).toggleClass( 'sk-loading' );
            }.bind ( this )
        );
    },

    destroy : function()
    {

    }
};

export { ContractList };