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

        self.contract = null;

        this.render( { groupId : groupId } );

        app.rpc.call(
            'Contract',
            'getDetail',
            {
                contractId : contractId
            },
            function( res )
            {
                if ( res && res.hasOwnProperty( 'data' ) && res.data.hasOwnProperty( 'contractData' ) )
                {
                    self.contract = self.createInstance( 'Contract', res.data.contractData );
                }

                const $compContractForm = self.createComponent(
                    'contract-form',
                    $container.find( '[f-id="container-contract-form"]' ).first().get( 0 ),
                    {
                        members : [],
                        contract : self.contract,
                        profileId : null,
                        groupId : groupId
                    }
                );

                $container.find( '[data-id="btnSaveContract"]' ).first().click( function()
                {
                    if ( $compContractForm.isValid() )
                    {
                        const $btn = $( this ),
                            cData = $compContractForm.getFormProperties();

                        cData.max_price_factor = ~~( cData.max_price_factor * 100 );
                        cData.current_price_factor = ~~( cData.current_price_factor * 100 );
                        cData.price = ~~( cData.price * 100 );
                        cData.price_payed = ~~( cData.price_payed * 100 );

                        $container.find( '.ibox-content' ).addClass( 'sk-loading' );
                        app.rpc.call(
                            'Contract',
                            'update',
                            cData,
                            function( resSave )
                            {
                                $container.find( '.ibox-content' ).removeClass( 'sk-loading' );
                                if ( resSave && 200 == resSave.code )
                                {
                                    app.UI.toastSuccess( 'Vertrag wurde erfolgreich aktualisiert.', 'Vertrag gespeichert' );
                                    if ( redirectPath )
                                    {
                                        app.redirect( redirectPath );
                                    }
                                    else
                                    {
                                        app.redirect( 'contract/list/' + groupId );
                                    }
                                }
                                else
                                {
                                    app.UI.toastError( 'Bitte überprüfen Sie ihre Angaben', 'Fehler' );
                                }
                            }
                        );
                    }
                });

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

            }.bind ( this )
        );
    },

    destroy : function()
    {

    }
};

export { ContractEdit };