const ContractCharging = {
    route : 'contract/charging/:groupId',
    templateUrl : 'js/app/views/contract/contract-charging.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,
            members = [],
            groupId = p.get( 'groupId' ),
            self = this;

        function getMemberById( mId )
        {
            let m = null;

            for ( let mi = 0; mi < members.length; mi++ )
            {
                if ( members[ mi ].id == mId )
                {
                    m = members[ mi ];
                    break;
                }
            }

            return m;
        }

        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 ] );
                        m.contextGroupId = p.get( 'groupId' );
                        members.push( m );
                    }

                    app.rpc.call(
                        'Contract',
                        'getUnchargedProfileIds',
                        {
                            teamId : groupId
                        },
                        function( res )
                        {
                            let unchargedItems = [],
                                memberIdsInList = [];
                            if ( res && res.hasOwnProperty( 'data' ) && res.data.hasOwnProperty( 'unchargedProfileIds' ) )
                            {
                                const up = res.data.unchargedProfileIds;
                                for( let upi = 0; upi < up.length; upi++ )
                                {
                                    memberIdsInList.push( +up[ upi ].profile_id );
                                    unchargedItems.push({
                                        member : getMemberById( up[ upi ].profile_id ),
                                        count : up[ upi ].cnt
                                    });
                                }
                            }

                            for ( let mi = 0; mi < members.length; mi++ )
                            {
                                if ( -1 === memberIdsInList.indexOf( +members[ mi ].id ) )
                                {
                                    unchargedItems.push({
                                        member : members[ mi ],
                                        count : 0
                                    });
                                }
                            }

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

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

    destroy : function()
    {

    }
};

export { ContractCharging };