const EmployeeList = {
    route : 'kkspecial/employee-list/:groupId',
    isDefault : true,
    templateUrl : 'js/app/views/kkspecial/employee-list.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 ),
              self = this,
              members = [];

        const fnGetSpecialAttribute = function( satt, attr, defaultValue = null )
        {
            let ret = defaultValue;
            if ( satt && satt.hasOwnProperty( attr ) )
            {
                ret = satt[ attr ];
            }
            return ret;
        };


        this.render();

        self.app.rpc.call(
            'Special',
            'getKKProfileAttributes',
            {
                groupId : p.get( 'groupId' )
            },
            function( res0 )
            {
                let satts = ( res0 && res0.hasOwnProperty( 'data' ) && res0.data.hasOwnProperty( 'special_profile_attributes' ) ) ? res0.data.special_profile_attributes : [];

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

                                satt = null;
                                for ( let si = 0; si < satts.length; si++ )
                                {
                                    if ( satts[ si ].profile_id == m.id )
                                    {
                                        satt = satts[ si ].attributes_js;
                                        break;
                                    }
                                }

                                // Attach special attributes
                                m.kk_hourlyRate = fnGetSpecialAttribute( satt, 'hourlyRate', 0 );
                                m.kk_hourMax = fnGetSpecialAttribute( satt, 'hourMax', 0 );
                                m.kk_notice = fnGetSpecialAttribute( satt, 'notice', '' );


                                members.push( m );
                            }

                            self.createComponent(
                                'kkspecial-employee-data-table',
                                $container.find( '[f-id="container-employee-data-table"]' ).first().get( 0 ),
                                members
                            );
                        }
                        $container.find( '.sk-loading' ).toggleClass( 'sk-loading' );
                    }.bind ( this )
                );

            }
        );
    },

    destroy : function()
    {

    }
};

export { EmployeeList };