var userGroup_detail = {
    settings: {
        user_group_id: null,
        userGroup_account_user_ajaxUrl: '',
        userGroup_account_user_formUrl: '',
        accountUser_autocompleteUrl: '',
        userGroup_account_user_formActionUrl: '',
    },

    accountUserDataList: null,
    init: function() {
        userGroup_detail.initDataTable();
        userGroup_detail.bindAccountUserForm();
    },
    bindAccountUserForm: function() {
        if(jQ('.href-account-user-form').length > 0) {

            jQ('.href-account-user-form').unbind('click').bind('click', function(){

                var requestName = 'userGroupAccountUserForm',
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content');

                global.ajaxCancel(requestName);

                var settings = {
                    url: userGroup_detail.settings.userGroup_account_user_formUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id'),
                    type: 'GET',
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open');
                        global.bindFormSubmitted();
                        userGroup_detail.bindaccountUserFormAction();
                    }
                };

                global.ajax(settings, requestName);
            });
        }
    },

    bindaccountUserFormAction: function() {

        jQ('.href-account-user-action').unbind('click.accountUserFormAction').bind('click.accountUserFormAction', function () {

            var requestName = 'accountUserFormAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content'),
                _form = jQ('#form-accountUser'),
                axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

            global.formSubmitted();
            global.ajaxCancel(requestName);

            var settings = {
                url: userGroup_detail.settings.userGroup_account_user_formActionUrl,
                type: 'POST',
                data: _form.serialize(),
                success: function (json) {
                    if (json.success) {
                        location.reload();
                    } else {
                        global.formSubmittedReverse();
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

            global.ajax(settings, requestName);
        });
    },

    initDataTable: function() {

        var callBack1 = function() {
            userGroup_detail.bindAccountUserForm()
        };

        userGroup_detail.accountUserDataList = jQ('#userGroupAccountUser-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': userGroup_detail.settings.userGroup_account_user_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.user_group_id = userGroup_detail.settings.user_group_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 3 },
                { 'searchable': false, 'targets': 3 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBack1();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack1);
                    }
                }
            }
        });

        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    }
};