const AppointmentList = {
    route : 'appointments/:groupId',
    isDefault : true,
    templateUrl : 'js/app/views/appointments/appointment-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 ),
              app = this.app,
              groupId = p.get( 'groupId' ),
              members = [],
              apps = [];

        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 = this.createInstance( 'UserProfile', res.data.members[ mi ] );
                        m.contextGroupId = p.get( 'groupId' );
                        members.push( m );
                    }

                    app.rpc.call(
                        'Appointment',
                        'getList',
                        {
                            groupId : p.get( 'groupId' ),
                            filter : 'all'
                        },
                        function( res2 )
                        {
                            if ( res2 && res2.hasOwnProperty( 'data' ) && res2.data.hasOwnProperty( 'appointments' ) )
                            {
                                let app;
                                for ( let ai = 0; ai < res2.data.appointments.length; ai++ )
                                {
                                    app = this.createInstance( 'Appointment', res2.data.appointments[ ai ] );
                                    app.setAttendees( res2.data[ 'attendees_' + res2.data.appointments[ ai ].id ] );
                                    apps.push( app );
                                }
                            }

                            let compAst = this.createComponent(
                                'appointment-select-table',
                                $container.find( '[f-id="container-appointment-data-table"]' ).first().get( 0 ),
                                apps
                            );
                            $container.find( '.sk-loading' ).toggleClass( 'sk-loading' );


                            $container.find( '[data-id="btnEditPriceFactor"]' ).click( function()
                            {
                                let appIds = compAst.getSelectedIds();

                                if ( 0 === appIds.length )
                                {
                                    app.UI.toastError( 'Bitte wählen Sie mindestens einen Termin zum Bearbeiten aus.', 'Kein Termin ausgewählt')
                                }
                                else
                                {
                                    let $modalRoot = $container.find( '[data-id="modal-price-factor-form"]' ).first();
                                    $modalRoot.find( '[data-id="section-pricefactor-form"]' ).first().show();
                                    $modalRoot.find( '[data-id="section-loader"]' ).first().hide();
                                    $modalRoot.on( 'shown.bs.modal', function()
                                    {
                                        $modalRoot.find( '[data-id="btnSavePriceFactor"]' ).first().off( "click" );
                                        $modalRoot.find( '[data-id="btnSavePriceFactor"]' ).first().click( function()
                                        {
                                            $(this).off( "click" );

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

                                            app.rpc.call(
                                                'Appointment',
                                                'batchUpdatePriceFactor',
                                                {
                                                    teamId : groupId,
                                                    appointmentIds : appIds,
                                                    priceFactor : ( +$modalRoot.find( '[data-id="inputPriceFactor"]' ).val() ) * 100
                                                },
                                                function( res, err )
                                                {
                                                    if ( res )
                                                    {
                                                        app.UI.toastSuccess( 'Einträge erfolgreich aktualisiert.' );
                                                        app.reload();
                                                    }
                                                    else
                                                    {
                                                        app.UI.toastError();
                                                    }
                                                    $modalRoot.modal( 'hide' );
                                                }
                                            );
                                        });
                                    });
                                    $modalRoot.on( 'hidden.bs.modal', function()
                                    {
                                        $modalRoot.modal( 'dispose' );
                                    });
                                    $modalRoot.modal( 'show' );
                                }
                            });

                        }.bind( this )
                    );

                }
            }.bind ( this )
        );
    },

    destroy : function()
    {

    }
};

export { AppointmentList };