const ContractCreate = {
    route : 'contract/create/:groupId(/:profileId)',
    templateUrl : 'js/app/views/contract/contract-create.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,
              profileId = p.get( 'profileId' ),
              groupId = p.get( 'groupId' ),
              members = [],
              self = this;

        this.render( { groupId : groupId } );

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

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

                $container.find( '[data-id="btnSaveContract"]' ).first().click( function()
                {

                    if ( false === app.user.getActiveGroup().hasValidContracteeData() )
                    {
                        app.UI.toastError( "Bitte aktualisieren Ihre Vertragsdaten der Stammdaten.", "Fehlende Daten!" );
                        return false;
                    }


                    if ( $compContractForm.isValid() )
                    {
                        const $btn = $( this ),
                              cData = $compContractForm.getFormProperties();

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

                        $container.find( '.ibox-content' ).addClass( 'sk-loading' );
                        app.rpc.call(
                            'Contract',
                            'create',
                            cData,
                            function( resSave )
                            {
                                $container.find( '.ibox-content' ).removeClass( 'sk-loading' );
                                if ( resSave && 200 == resSave.code )
                                {
                                    app.UI.toastSuccess( 'Vertrag wurde erfolgreich angelegt.', 'Vertrag gespeichert' );
                                    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 { ContractCreate };