const HourAllocation = {
    route : 'kkspecial/hour-allocation/:groupId/:from/:to',
    isDefault : true,
    templateUrl : 'js/app/views/kkspecial/hour-allocation.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 = [];

        self.groupId = p.get( 'groupId' );

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

        const fnGetMemberById = function( pId )
        {
            let m = null;

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

            return m;
        };

        /*
        const fnUpdateDataTable = function()
        {
            // Wrap own function
            self.app.rpc.call(
                'Special',
                'getWorkshifts',
                {
                    groupId : self.groupId,
                    startDate : self.momentStart.format( 'YYYY-MM-DD' ),
                    endDate : self.momentEnd.format( 'YYYY-MM-DD' )
                },
                function ( res )
                {
                    if ( !res || res.code != 200 || !res.data.hasOwnProperty( 'workshifts' ) )
                    {
                        app.gui.toastError();
                        return;
                    }

                    console.log( res );

                    let ws = res.data.workshifts;
                    for ( let wi = 0; wi < ws.length; wi++ )
                    {
                        ws[ wi ].appointment_momentStart = moment( ws[ wi ].appointment_start , "YYYY-MM-DD hh:mm:ss" );
                        ws[ wi ].appointment_momentEnd = moment( ws[ wi ].appointment_end , "YYYY-MM-DD hh:mm:ss" );
                        ws[ wi ].appointment_duration = parseFloat( moment.duration( ws[ wi ].appointment_momentEnd.diff( ws[ wi ].appointment_momentStart ) ).asHours() ).toFixed( 2 );
                        ws[ wi ].member = fnGetMemberById( ws[ wi ].profile_id );
                        if ( null === ws[ wi ].gross_hour )
                        {
                            ws[ wi ].gross_hour = ws[ wi ].appointment_duration;
                        }
                        if ( null === ws[ wi ].pause_hour )
                        {
                            if ( ws[ wi ].gross_hour >= 6 && ws[ wi ].gross_hour <= 9 )
                            {
                                ws[ wi ].pause_hour = 0.5;
                            }
                            else if ( ws[ wi ].gross_hour > 9 )
                            {
                                ws[ wi ].pause_hour = 0.75;
                            }
                            else
                            {
                                ws[ wi ].pause_hour = 0;
                            }
                        }
                        if ( null === ws[ wi ].net_hour )
                        {
                            ws[ wi ].net_hour = ws[ wi ].gross_hour - ws[ wi ].pause_hour;
                        }


                    }

                    self.createComponent(
                        'kkspecial-hour-allocation-data-table',
                        $container.find( '[data-id="container-hour-allocation-data-table"]' ).first().get( 0 ),
                        ws
                    );

                }
            );
        };
         */


        this.render();

        if ( p.get( 'from' ) === '0' || p.get( 'to' ) === '0' )
        {
            this.momentStart = moment().subtract( 1, 'month' ).startOf( 'month' );
            this.momentEnd = moment().subtract( 1, 'month' ).endOf( 'month' );
        }
        else
        {
            this.momentStart = moment( p.get( 'from' ), 'YYYY-MM-DD' );
            this.momentEnd = moment( p.get( 'to' ), 'YYYY-MM-DD' );
        }


        this.rangePicker = new Litepicker(
            {
                element: $container.find( '[data-id="litepicker-daterange"]' ).first().get( 0 ),
                singleMode: false,
                format : "DD.MM.YYYY",
                splitView : true,
                lang : "de-DE",
                tooltipText: {
                    one: 'Tag',
                    other: 'Tage'
                },
                startDate : this.momentStart.format( 'DD.MM.YYYY' ),
                endDate : this.momentEnd.format( 'DD.MM.YYYY' )
            }
        );

        $container.find( '[data-id="updateDateRange"]' ).click( function()
        {
            let from = moment( self.rangePicker.getStartDate().dateInstance ).format( 'YYYY-MM-DD' ),
                to = moment( self.rangePicker.getEndDate().dateInstance ).format( 'YYYY-MM-DD' );
            self.redirect( '/kkspecial/hour-allocation/' + self.groupId + '/' + from + '/' + to );
        });

        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(
                    'Special',
                    'getWorkshifts',
                    {
                        groupId : self.groupId,
                        startDate : self.momentStart.format( 'YYYY-MM-DD' ),
                        endDate : self.momentEnd.format( 'YYYY-MM-DD' )
                    },
                    function ( res )
                    {
                        if ( !res || res.code != 200 || !res.data.hasOwnProperty( 'workshifts' ) || !res.data.hasOwnProperty( 'members' ) )
                        {
                            app.gui.toastError();
                            return;
                        }

                        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 );
                        }

                        let ws = res.data.workshifts;
                        for ( let wi = 0; wi < ws.length; wi++ )
                        {
                            ws[ wi ].appointment_momentStart = moment( ws[ wi ].appointment_start , "YYYY-MM-DD hh:mm:ss" );
                            ws[ wi ].appointment_momentEnd = moment( ws[ wi ].appointment_end , "YYYY-MM-DD hh:mm:ss" );
                            ws[ wi ].appointment_duration = parseFloat( moment.duration( ws[ wi ].appointment_momentEnd.diff( ws[ wi ].appointment_momentStart ) ).asHours() ).toFixed( 2 );
                            ws[ wi ].member = fnGetMemberById( ws[ wi ].profile_id );
                            if ( null === ws[ wi ].additional_pay )
                            {
                                ws[ wi ].additional_pay = 0;
                            }
                            if ( null === ws[ wi ].hourly_rate )
                            {
                                ws[ wi ].hourly_rate = ws[ wi ].member.kk_hourlyRate;
                            }
                            if ( null === ws[ wi ].gross_hour )
                            {
                                ws[ wi ].gross_hour = ws[ wi ].appointment_duration;
                            }
                            if ( null === ws[ wi ].pause_hour )
                            {
                                if ( ws[ wi ].gross_hour >= 6 && ws[ wi ].gross_hour <= 9 )
                                {
                                    ws[ wi ].pause_hour = 0.5;
                                }
                                else if ( ws[ wi ].gross_hour > 9 )
                                {
                                    ws[ wi ].pause_hour = 0.75;
                                }
                                else
                                {
                                    ws[ wi ].pause_hour = 0;
                                }
                            }
                            if ( null === ws[ wi ].net_hour )
                            {
                                ws[ wi ].net_hour = ws[ wi ].gross_hour - ws[ wi ].pause_hour;
                            }
                        }

                        self.createComponent(
                            'kkspecial-hour-allocation-data-table',
                            $container.find( '[data-id="container-hour-allocation-data-table"]' ).first().get( 0 ),
                            ws
                        );

                        $container.find( '.sk-loading' ).toggleClass( 'sk-loading' );

                    }.bind( this )
                );
            }
        );
    },

    destroy : function()
    {
        this.rangePicker.destroy();
    }
};

export { HourAllocation };