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

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

app.state.GroupDetailEdit  = function()
{
    var state = app.core.StateManager.createState( 'group-detail-edit' );

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

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Team',
            'getDetails',
            { teamId : groupId },
            function( res )
            {
                var group = new app.model.Group( res.team );

                app.core.View.setContent(
                    app.core.View.getTemplate( 'group-detail-edit', { group : group } )
                );

                $content.find( '[data-id="btn-save-changes"]' ).first().click( function()
                {
                    let $form = $content.find( '[data-id="form-team-details"]' ).first(),
                        isValid = app.util.Form.bootstrapValidate( $form );

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

                        app.core.Rpc.call(
                            'Team',
                            'update',
                            {
                                groupId : groupId,
                                groupName : app.util.Helper.trim( $form.find( '[data-id="input-team-name"]' ).first().val() ),
                                description : app.util.Helper.trim( $form.find( '[data-id="textarea-team-description"]' ).first().val() ),
                                contactInformation : app.util.Helper.trim( $form.find( '[data-id="textarea-team-contactinformation"]' ).first().val() ),
                                industry : $form.find( '[data-id="select-team-industry"]' ).first().val()
                            },
                            function( res )
                            {
                                app.core.Controller.redirect( '#/group/' + groupId );
                            }
                        );

                    }

                    return false;
                });

                app.gui.PageLoader.hide();
            }
        );
    };

    return state;
};