/**
 * (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()),
                                termsConditions: app.util.Helper.trim($form.find('[data-id="textarea-team-terms-conditions"]').first().val()),
                                termsConditionsActive: $form.find('[data-id="checkbox-team-terms-conditions-active"]').first().is(":checked"),
                                newUsersInactive: $form.find('[data-id="checkbox-new-users-active"]').first().is(":checked"),
                                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;
                });

                $content.find('[data-id="btn-reset-terms-members"]').first().click(function () {
                    app.core.View.confirm(
                        _lc('DO_YOU_REALLY_WANT_TO_RESET_GROUP_MEMBERS_ACCEPTED_TERMS'),
                        function () {
                            app.gui.PageLoader.show();
                            app.core.View.closeModal();
                            app.core.Rpc.call(
                                'Team',
                                'resetAcceptedMemberTerms',
                                {
                                    groupId: groupId,
                                },
                                function (res) {
                                    app.core.View.toastSuccess(_lc('SUCCESS_RESET_TERMS_MEMBERS'));
                                },
                                function (res) {
                                    app.core.View.toastError(_lc('GENERAL_SERVER_ERROR'));
                                }
                            );
                        },
                        _lc('BTN_CONFIRM_RESET_GROUP_MEMBERS_ACCEPTED_TERMS')
                    );
                });
            }
        );
        app.gui.PageLoader.hide();
    }
    return state;
}