const ContractMasterData = {
    route : 'contract/master-data/:groupId',
    templateUrl : 'js/app/views/contract/contract-master-data.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' ),
            Countries = app.classManager.get( 'Countries' );

        this.render( { groupId : groupId, group : app.user.getActiveGroup(), countries : Countries.getAll() } );

        $container.find( '[data-id="btnSaveMasterData"]' ).click( function()
        {
            let $form = $container.find( '[data-id="form-contract-master-data"]' ).first(),
                isValid = app.util.Form.bootstrapValidate( $form );

            if ( isValid )
            {
                let arr = $form.serializeArray(),
                    props = {};

                for ( let i = 0; i < arr.length; i++ )
                {
                    props[ arr[ i ].name ] = arr[ i ].value;
                }

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

                app.rpc.call(
                    'Team',
                    'setContracteeData',
                    props,
                    function( resp )
                    {
                        $container.find( '.ibox-content' ).toggleClass( 'sk-loading' );
                        if ( resp && 200 == resp.code )
                        {
                            app.UI.toastSuccess( 'Daten erfolgreich aktualisiert.' );
                            setTimeout( function()
                            {
                                window.location.reload( true );
                            }, 250 );
                        }
                        else
                        {
                            app.UI.toastError();
                        }
                    }
                );
            }

        });

    },

    destroy : function()
    {

    }
};

export { ContractMasterData };