/**
 * (c) by aheadware.com
 */

var app = app || {};
app.state = app.state || {};

app.state.ConfigurationProfileEdit = function()
{
    var state = app.core.StateManager.createState( 'configuration-profile-edit' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent();

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Account',
            'getAccountRelatedData',
            null,
            function( res )
            {
                var profile = new app.model.Profile( res.profile, res.own_teams );
                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'configuration-profile-edit',
                        {
                            p : profile
                        }
                    )
                );

                $content.find( '[data-id="btn-save"]' ).click( function()
                {
                    let $form = $content.find('[data-id="form-profile"]').first(),
                        birthday = null,
                        teamProps = profile.getTeamProp(),
                        isValid = app.util.Form.bootstrapValidate($form);

                    if (isValid)
                    {
                        app.gui.PageLoader.show();

                        if ( $form.find( '[data-id="input-birthday"]' ).first().val().length > 0 )
                        {
                            //birthday = app.util.Helper.getMomentFromInputDate( $form.find( '[data-id="input-birthday"]' ).first() );
                            birthday = $form.find( '[data-id="input-birthday"]' ).first().val();
                        }

                        // Special group properties
                        if ( profile.isInGroupCategory( 'DOGSCHOOL' ) )
                        {
                            if ( null === teamProps )
                            {
                                teamProps = {};
                            }

                            if ( !teamProps.hasOwnProperty( 'DOGSCHOOL' ) )
                            {
                                teamProps[ 'DOGSCHOOL' ] = {};
                            }
                            teamProps[ 'DOGSCHOOL' ][ 'dogname'] = $form.find( '[data-id="input-dogschool-dogname"]' ).first().val();
                        }

                        app.core.Rpc.call(
                            'Profile',
                            'update',
                            {
                                status : $form.find( '[data-id="input-status"]' ).first().val(),
                                firstName: $form.find('[data-id="input-firstname"]').first().val(),
                                lastName : $form.find('[data-id="input-lastname"]').first().val(),
                                birthday : birthday,
                                teamProps : teamProps
                            },
                            function( res )
                            {
                                app.gui.PageLoader.hide();
                                app.core.View.toastSuccess( _lc( 'PROFILE_SUCCESSFULLY_UPDATED' ) );
                                app.core.Controller.redirect( '#/configuration/profile' );
                            }
                        );

                    }
                });

                app.gui.PageLoader.hide();

            }
        );
    };

    return state;
};