const ContractChargeAppointment = {
    route : 'contract/charge-appointment/:groupId/:profileId(/:contractId)',
    templateUrl : 'js/app/views/contract/contract-charge-appointment.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' ),
              profileId = p.get( 'profileId' ),
              preselectedContractId = p.get( 'contractId' ),
              self = this;

        self.contracts = [];
        self.attendedApps = [];
        self.profile = null;

        this.render( { groupId : groupId, profileId : profileId } );

        app.rpc.call(
            'Contract',
            'getList',
            {
                teamId : groupId,
                profileId : profileId,
                state : 'active'
            },
            function( res )
            {
                app.rpc.call(
                    'Appointment',
                    'getListForAttendedProfileId',
                    {
                        teamId : groupId,
                        profileId : profileId
                    },
                    function( res2 )
                    {

                        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( self.createInstance( 'Contract', cs[ ci ] ) );
                            }
                        }

                        if ( res2 && res2.hasOwnProperty( 'data' )  )
                        {
                            if ( res2.data.hasOwnProperty( 'attended_appointments' ) )
                            {
                                const aa = res2.data.attended_appointments;
                                self.attendedApps = [];
                                for( let aai = 0; aai < aa.length; aai++ )
                                {
                                    self.attendedApps.push( self.createInstance( 'AttendedAppointment', aa[ aai ] ) );
                                }
                            }

                            if ( res2.data.hasOwnProperty( 'profile' ) )
                            {
                                self.profile = self.createInstance( 'UserProfile', res2.data.profile );
                            }
                        }


                        const $caad = self.createComponent(
                            'contract-appointment-assign-dual-list',
                            $container.find( '[f-id="container-contract-appointment-assignment"]' ).first().get( 0 ),
                            {
                                profile : self.profile,
                                contracts : self.contracts,
                                attendedAppointments : self.attendedApps,
                                groupId : groupId,
                                preselectedContractId : preselectedContractId
                            }
                        );
                        $container.find( '.sk-loading' ).toggleClass( 'sk-loading' );

                        $container.find( '[data-id="btnSave"]' ).first().click( function()
                        {
                            $container.find( '.ibox-content' ).toggleClass( 'sk-loading' );
                            let ids = $caad.getSelectedIds();
                            app.rpc.call(
                                'Contract',
                                'updateAttendances',
                                {
                                    teamId : groupId,
                                    profileId : ids.profileId,
                                    contractId : ids.contractId,
                                    attendancesIds : ids.contractAttendeeIds
                                },
                                function( resp )
                                {
                                    if ( resp && resp.hasOwnProperty( 'code' ) && resp.code == 200 )
                                    {
                                        app.UI.toastSuccess( 'Änderungen für ' + self.profile.getName() + ' erfolgreich gespeichert', 'Änderungen gespeichert.' );
                                        app.redirect( '/contract/charging/' + groupId );
                                    }
                                    else
                                    {
                                        app.UI.toastError();
                                    }
                                }
                            );
                        });
                    }
                );
            }.bind ( this )
        );
    },

    destroy : function()
    {

    }
};

export { ContractChargeAppointment };