Array.prototype.remove = function () {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};
var agentsScript = agentsScript || function () {

    var isInit = false;
    var agentListingDataTable = null;
    var trashedAgentListingDataTable = null;
    var agentRolesListingDataTable = null;
    var selectedAgents = [];
    var customCheckBox = '<div class="icheck-material-primary f_custom_checkbox" style="pointer-events: none"><input type="checkbox" id="bulk-action-checkbox" class="select_all" /><label for="bulk-action-checkbox"></label></div>';

    var init = function () {
        if (isInit) return;
        isInit = true;
        handler();
    };

    var handler = function () {
        agentListDataTable($('#agentSearchFrom').serialize());
        globalScript.initValidator();

        $('body').on('change', '#min_age', function (e) {
            if($(this).val() === ""){
                $("#max_age").attr("required", false);
                if($("#max_age").val() !== ''){
                    $(this).attr("required", true);
                }
            } else {
                $(this).attr("required", false);
                if($("#max_age").val() === ''){
                    $("#max_age").attr("required", true);
                }
            }
        });

        $('body').on('change', '#max_age', function (e) {
            if($(this).val() === ""){
                $("#min_age").attr("required", false);
                if($("#min_age").val() !== ''){
                    $(this).attr("required", true);
                }
            } else {
                $(this).attr("required", false);
                if($("#min_age").val() === ''){
                    $("#min_age").attr("required", true);
                }
            }
        });

        $("body").on('click', '.select_all', function () { // bulk checked
            var status = this.checked;
            if (status === true) {
                $('#bulk-action-checkbox').prop('checked', true);
                $('body').find(".bulkRow").each(function () {
                    $(this).prop("checked", status);
                    var roleId = $(this).closest('tr').find('.agent-id').val();
                    if (selectedAgents.indexOf(roleId) === -1) {
                        selectedAgents.push(roleId);
                    }
                });
            } else {
                $(".bulkRow, .select_all").prop('checked', false);
                $('#bulk-action-checkbox').prop('checked', false);
                selectedAgents = [];
            }
        });

        $('body').on('click', '.bulkRow', function () {
            var status = this.checked;
            var roleId = $(this).closest('tr').find('.agent-id').val();
            if (status === true) {
                if (selectedAgents.indexOf(roleId) === -1) {
                    selectedAgents.push(roleId);
                }
                $('#bulk-action-checkbox').prop('checked', true);
            } else {
                selectedAgents.remove(roleId);
            }
            if (selectedAgents.length === 0) {
                $(".bulkRow, .select_all").prop('checked', false);
            }
        });

        $('body').on('click', '#apply_bulk_actions_btn', function () {
            var bulkOption = $('#bulk_actions').val();
            if (bulkOption === '') {
                $('#bulk_actions_error').html('Please select an action first').show();
            } else {
                if (selectedAgents.length === 0) {
                    $('#bulk_actions_error').html('Please select agent(s) first').show();
                } else {
                    $('#bulk_actions_error').hide();
                    if (bulkOption === 'delete_bulk') {
                        globalScript.showConfirmationAlert(
                            'You want to deactivate selected agent(s)?',
                            function () {
                                deleteAgents(function () {
                                    toastr.success('Deleted successfully');
                                    agentListDataTable($('#agentSearchFrom').serialize());
                                    $(".bulkRow, .select_all").prop('checked', false);
                                });
                            }
                        )
                    }
                    if (bulkOption === 'active_bulk') {
                        agentStatus = 1;
                        updateAgentStatus(agentStatus, function () {
                            toastr.success('Status successfully updated');
                            agentListDataTable($('#agentSearchFrom').serialize());
                            $(".bulkRow, .select_all").prop('checked', false);
                        });
                    }
                    if (bulkOption === 'inactive_bulk') {
                        agentStatus = 0;
                        updateAgentStatus(agentStatus, function () {
                            toastr.success('Status successfully updated');
                            agentListDataTable($('#agentSearchFrom').serialize());
                            $(".bulkRow, .select_all").prop('checked', false);
                        });
                    }
                    if (bulkOption === 'block_bulk') {
                        agentStatus = 1;
                        updateAgentBlock(agentStatus, function () {
                            toastr.success('Status successfully updated');
                            agentListDataTable($('#agentSearchFrom').serialize());
                            $(".bulkRow, .select_all").prop('checked', false);
                        });
                    }
                    if (bulkOption === 'unblock_bulk') {
                        agentStatus = 0;
                        updateAgentBlock(agentStatus, function () {
                            toastr.success('Status successfully updated');
                            agentListDataTable($('#agentSearchFrom').serialize());
                            $(".bulkRow, .select_all").prop('checked', false);
                        });
                    }
                }
            }
        });

        $('body').on('change', '.update_agent_status', function () {
            var status = $(this).val();
            var agentId = $(this).closest('tr').find('.agent-id').val();
            selectedAgents.push(agentId);
            updateAgentStatus(status, function () {
                toastr.success('Status successfully updated');
                agentListDataTable($('#agentSearchFrom').serialize());
            });
        });

        $('body').on('change', '.update_block_status', function () {
            var status = $(this).val();
            var agentId = $(this).closest('tr').find('.agent-id').val();
            selectedAgents.push(agentId);
            updateAgentBlock(status, function () {
                toastr.success('Status successfully updated');
                agentListDataTable($('#agentSearchFrom').serialize());
            });
        });

        $('body').on('click', '.edit-btn', function () {
            var agentId = $(this).closest('tr').find('.agent-id').val();
            var obj = Object.create(globalScript.GET_USER_DETAILS);
            var url = obj.url(agentId);
            obj.url = url;
            globalScript.ajaxCall(obj).done(function (response) {
                if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                    $('body').find('#agent_id').val(response.records.id);
                    $('body').find('#edit_agent_form #agent_name').val(response.records.name);
                    $('body').find('#edit_agent_form #agent_agency').val(response.records.parent);
                    $('body').find('#edit_agent_form #agent_phone').val(response.records.phone);
                    $('body').find('#edit_agent_form #agent_email').val(response.records.email);
                    $('.edit_errors').hide();
                    $('.edit_errors ul').html('');
                    $('#editAgentModal').modal('show');
                }
            })
        });

        $('body').on('click', '.view-btn', function () {
            var agentId = $(this).closest('tr').find('.agent-id').val();
            var obj = Object.create(globalScript.GET_USER_DETAILS);
            var url = obj.url(agentId);
            obj.url = url;
            globalScript.ajaxCall(obj).done(function (response) {
                if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                    populateValuesToProfileModal(response.records, function () {
                        $("#profileModal").modal('show');
                    });
                }
            })
        });

        $('body').on('click', '#trash-btn', function () {
            trashedAgentListDataTable();
        });

        $('body').on('click', '#recover-all-trashed-btn', function () {
            globalScript.showConfirmationAlert(
                'You want to recover all agents?',
                function () {
                    recoverAllAgents(function () {
                        toastr.success('All agents have been recovered successfully');
                        $('#trashedAgentModal').modal('hide');
                    });
                }
            )
        });

        $('body').on('click', '#delete-all-trashed-btn', function () {
            globalScript.showConfirmationAlert(
                'You want to permanently delete all agents?',
                function () {
                    permanentlyDeleteAllAgents(function () {
                        toastr.success('All agents have been permanently deleted successfully');
                        $('#trashedAgentModal').modal('hide');
                    });
                }
            )
        });

        $('body').on('click', '.recover-btn', function () {
            var agentID = $(this).closest('tr').find('.trashed-agent-id').val();
            var obj = Object.create(globalScript.RECOVER_USER);
            var url = obj.url(agentID);
            obj.url = url;
            globalScript.ajaxCall(obj).done(function (response) {
                if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                    toastr.success(response._metadata.message);
                    trashedAgentListDataTable();
                }
            })
        });

        $('body').on('click', '.per-delete-btn', function () {
            var agentID = $(this).closest('tr').find('.trashed-agent-id').val();
            var obj = Object.create(globalScript.PER_DELETE_USER);
            var url = obj.url(agentID);
            obj.url = url;
            globalScript.showConfirmationAlert(
                'You want to permanently delete this agent?',
                function () {
                    globalScript.ajaxCall(obj).done(function (response) {
                        if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                            toastr.success(response._metadata.message);
                            trashedAgentListDataTable();
                        }
                    })
                }
            )
        });

        $('body').on('click', '.roles-btn', function () {
            var agentId = $(this).closest('tr').find('.agent-id').val();
            $('.agent_main_id').val(agentId);
            $('#rolesListModal').modal('show');
            agentRolesListDataTable(agentId);
        });

        $('body').on('click', '#show_role_form', function () {
            $('#roles_list').html('');
            getAgentRolesListExcept(function () {
                $('#add_role_to_agent_form').show();
                $('#roles_list').selectpicker('val', '');
                $('#roles_list').selectpicker('refresh');
                globalScript.initValidatorSimple();
            });
        });

        $('body').on('submit', '#add_role_to_agent_form', function (e) {
            e.preventDefault();
            if ($('#roles_btn').hasClass('disabled')) {
                return false;
            }
            $('#roles_btn').addClass('disabled');
            $('.roles_errors').hide();
            $('.roles_errors ul').html('');
            var agentId = $('.agent_main_id').val();
            var roleIds = $('#roles_list').val();
            var obj = Object.create(globalScript.ASSIGN_ROLES_TO_USER);
            obj.data = {
                'role_ids': roleIds,
                'agent_id': agentId
            };

            globalScript.ajaxCall(obj).done(function (response) {
                if (response.errors && response.errors.length !== 0) {
                    $('#roles_btn').removeClass('disabled');
                    _.each(response.errors, function (v, k) {
                        $('.roles_errors ul').append('<li>' + v + '</li>');
                        $('.roles_errors').show();
                    });
                } else {
                    if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                        agentRolesListDataTable(agentId);
                        toastr.success(response._metadata.message);
                        $('#roles_list').html('');
                        $('#add_role_to_agent_form').hide();
                    }
                }
            });
        });

        $('body').on('click', '.delete_agent_roles_btn', function () {
            var agentId = $('.agent_main_id').val();
            var roleId = $(this).closest('tr').find('.agent-role-id').val();
            var obj = Object.create(globalScript.REMOVE_ROLE_FROM_USER);
            obj.data = {
                'agent_id': agentId,
                'role_id': roleId
            };
            globalScript.ajaxCall(obj).done(function (response) {
                if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                    toastr.success(response._metadata.message);
                    $('#roles_list').html('');
                    $('#add_role_to_agent_form').hide();
                    agentRolesListDataTable(agentId);
                }
            })
        });

        $('body').on('click', '#remove_all_attach_roles_btn', function () {
            var agentId = $('.agent_main_id').val();
            var obj = Object.create(globalScript.REMOVE_ALL_ROLES_FROM_USER);
            obj.data = {
                'agent_id': agentId
            };
            globalScript.showConfirmationAlert(
                'You want to remove all roles from this agent?',
                function () {
                    globalScript.ajaxCall(obj).done(function (response) {
                        if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                            toastr.success(response._metadata.message);
                            $('#roles_list').html('');
                            $('#add_role_to_agent_form').hide();
                            agentRolesListDataTable(agentId);
                        }
                    })
                }
            )
        });

        $('body').on('click', '.delete-btn', function () {
            selectedAgents = [];
            $(".bulkRow, .select_all").prop('checked', false);
            var agentId = $(this).closest('tr').find('.agent-id').val();
            selectedAgents.push(agentId);
            globalScript.showConfirmationAlert(
                'You want to deactivate selected agent?',
                function () {
                    deleteAgents(function () {
                        toastr.success('Deleted successfully');
                        $("#profileModal").modal("hide");
                        agentListDataTable($('#agentSearchFrom').serialize());
                    });
                }
            )
        });

        $('body').on('click', '.email-btn', function () {
            var email = $(this).closest('tr').find('.agent-id').attr('data-pro-email');
            $("#reply_email").val(email);
            $('#profileModal').modal('hide');
            setTimeout(function () {
                $('#emailModal').modal('show');
            }, 500);
        });

        $('body').on('click', '#agentSearchFormReset', function () {
            agentListDataTable('');
        });

        $('body').on('submit', '#create_reply_form', function (e) {
            e.preventDefault();
            if ($('#reply_btn').hasClass('disabled')) {
                return false;
            }
            $('.reply_errors').hide();
            $('.reply_errors ul').html('');
            var replyEmail = $('#reply_email').val();
            var replySubject = $('#reply_subject').val();
            var replyContent = $('#reply_content').summernote('code');
            var obj = Object.create(globalScript.EMAIL_USER);
            obj.data = {
                'email': replyEmail,
                'subject': replySubject,
                'body': replyContent
            };
            globalScript.ajaxCall(obj).done(function (response) {
                if (response.errors && response.errors.length !== 0) {
                    $('.reply_errors').show();
                    _.each(response.errors, function (v, k) {
                        $('.reply_errors ul').append('<li>' + v + '</li>');
                    });
                    return false;
                } else if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                    toastr.success(response._metadata.message);
                    $('#emailModal').modal('hide');
                    setTimeout(function () {
                        $('#profileModal').modal('show');
                    }, 500);
                }
            });
        });

        $('#addAgentModal').on('hidden.bs.modal', function (e) {
            $('.add_errors').hide();
            $('.add_errors ul').html('');
            $('#create_agent_form').trigger("reset");
        });

        $('#editAgentModal').on('hidden.bs.modal', function (e) {
            $('.edit_errors').hide();
            $('.edit_errors ul').html('');
            $('#edit_agent_form').trigger("reset");
        });

        $('#rolesListModal').on('hidden.bs.modal', function (e) {
            $('.agent_main_id').val('');
            $('#roles_list').html('');
            $('#add_role_to_agent_form').hide();
            $('.roles_errors').hide();
            $('.roles_errors ul').html('');
            agentListDataTable($('#agentSearchFrom').serialize());
        });

        $('#profileModal').on('hidden.bs.modal', function (e) {
            $('body').find('#profile_pictue').attr('src', '');
        });

        $('#emailModal').on('hidden.bs.modal', function (e) {
            $('#reply_content').summernote('reset');
            $('#reply_subject').val('');
            $('#emailModal').modal('hide');
            setTimeout(function () {
                $('#profileModal').modal('show');
            }, 500);
        });

        $('#trashedAgentModal').on('hidden.bs.modal', function (e) {
            agentListDataTable($('#agentSearchFrom').serialize());
        });
    };

    var getAgentRolesListExcept = function (callback) {
        var agentId = $('.agent_main_id').val();
        var obj = Object.create(globalScript.GET_USER_ROLES_EXCEPT);
        var url = obj.url(agentId);
        obj.url = url;
        globalScript.ajaxCall(obj).done(function (response) {
            if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                if (response.records.agentRolesList === null) {
                    toastr.error('No role found');
                } else if (response.records.agentRolesList.length !== 0) {
                    _.each(response.records.agentRolesList, function (v, k) {
                        $('#roles_list').append('<option value="' + v.id + '">' + v.display_name + '</option>');
                    });
                    if (typeof callback == 'function') callback();
                } else {
                    toastr.success('This agent already have all roles');
                }
            }
        })
    };

    var deleteAgents = function (callback) {
        var obj = Object.create(globalScript.DELETE_USER);
        obj.data = {
            'user_ids[]': selectedAgents
        };
        globalScript.ajaxCall(obj).done(function (response) {
            selectedAgents = [];
            if (typeof callback == 'function') callback();
        });
    };

    $('body').on('submit', '#agentSearchFrom', function (e) {
        var minReq = $("#min_age").attr('required');
        var maxReq = $("#max_age").attr('required');

        if (typeof minReq !== typeof undefined && minReq !== false) {
            toastr.error('Please select age from');
            return false;
        }
        if (typeof maxReq !== typeof undefined && maxReq !== false) {
            toastr.error('Please select age to');
            return false;
        }
        e.preventDefault();
        agentListDataTable($('#agentSearchFrom').serialize());
    });

    var getQueryParam = function (param) {
        var urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(param);
    }

    var agentListDataTable = function (type) {
        if (agentListingDataTable != null) {
            agentListingDataTable.clear().destroy();
        }
        agentListingDataTable = $('#agentListTable').DataTable({
            "ajax": {
                url: globalScript.APP_URL + "users?type=3",
                type: "GET",
                xhrFields: {
                    withCredentials: true
                },
                dataSrc: function (json) {
                    if (json.hasOwnProperty('_metadata') && json._metadata.hasOwnProperty('outcomeCode') && json._metadata.outcomeCode === 4) {
                        window.top.location.href = "login";
                        return;
                    }
                    json.draw = json.records.draw;
                    json.recordsTotal = json.records.recordsTotal;
                    json.recordsFiltered = json.records.recordsFiltered;
                    $('body').find('#module_total_count').html(json.records.recordsTotal);
                    if (json.records.data.length !== 0) {
                        $("body").find(".f_custom_checkbox").css('pointer-events', 'all');
                        $("body").find(".f_custom_card_footer").show();
                    } else {
                        $("body").find(".f_custom_card_footer").hide();
                    }
                    return json.records.data;
                }
            },
            "searching": true,
            "ordering": true,
            "serverSide": true,
            "paging": true,
            "stateSave": false,
            "columns": [
                {"orderable": true, "searchable": false, "data": 'created_at', "visible": false},
                {"data": "name", "title": "Name", "name": "name", "searchable": true, "orderable": true},
                {"data": "parent", "title": "Agency", "name": "parent.name", "searchable": false, "orderable": false},
                {"data": "email", "title": "Email", "name": "email", "searchable": true, "orderable": true},
                {"data": "phone", "title": "Phone", "name": "phone", "searchable": true, "orderable": false},
                {"data": "status", "title": "Status", "name": "status", "searchable": false, "orderable": false},
                {"data": "emirates_id_front", "title": "Emirates id front", "name": "emirates_id_front", "searchable": false, "orderable": false},
                {"data": "emirates_id_back", "title": "Emirates ID back", "name": "emirates_id_back", "searchable": false, "orderable": false},
                {"data": "copy_of_passport", "title": "Copy of passport", "name": "copy_of_passport", "searchable": false, "orderable": false},
                {"data": "copy_of_visa", "title": "Copy of visa", "name": "copy_of_visa", "searchable": false, "orderable": false},
                {"data": null, "title": "Action", "searchable": false, "orderable": false}
            ],
            "order": [[0, 'desc']],
            "rowCallback": function (row, data) {
                // if (!data.profile) {
                //     data.id = data.agent_id
                //     data.profile = data;
                // }
                var tmpl = _.template($('#agentListTemplate').html());
                $(row).html(tmpl(data));
            }
        });
    };

    var trashedAgentListDataTable = function () {
        if (trashedAgentListingDataTable != null) {
            trashedAgentListingDataTable.clear().destroy();
        }
        trashedAgentListingDataTable = $('#trashedAgentListTable').DataTable({
            "ajax": {
                url: globalScript.APP_URL + "trashedagents",
                type: "GET",
                xhrFields: {
                    withCredentials: true
                },
                dataSrc: function (json) { // Manipulate the data returned from the server
                    if (json.hasOwnProperty('_metadata') && json._metadata.hasOwnProperty('outcomeCode') && json._metadata.outcomeCode === 4) {
                        window.top.location.href = "login";
                        return;
                    }
                    json.draw = json.records.draw;
                    json.recordsTotal = json.records.recordsTotal;
                    json.recordsFiltered = json.records.recordsFiltered;
                    if (json.records.data.length !== 0) {
                        $("body").find(".trashed_modal_footer").show();
                    } else {
                        $("body").find(".trashed_modal_footer").hide();
                    }
                    return json.records.data;
                }
            },
            "searching": true,
            "ordering": false,
            "serverSide": true,
            "paging": true,
            "columns": [
                {
                    "data": 'deact_reason',
                    "name": 'deact_reason',
                    "title": 'Reason',
                    "searchable": true,
                    "orderable": false,
                    "visible": true
                },
                {
                    "data": 'deact_date',
                    "name": 'deact_date',
                    "title": 'Date',
                    "searchable": false,
                    "orderable": true,
                    "visible": true
                },
                {"data": "profile", "title": "Profile ID", "name": "profile.p_id", "searchable": true, "orderable": true},
                {
                    "data": "name",
                    "title": "Name",
                    "name": "name",
                    "searchable": true,
                    "orderable": true
                },
                {"data": "email", "title": "Email", "name": "email", "searchable": true, "orderable": true},
                {"data": "profile", "title": "Gender", "name": "profile.gender", "searchable": true, "orderable": true},
                {
                    "data": "profile",
                    "title": "Marital status",
                    "name": "profile.marital_status",
                    "searchable": true,
                    "orderable": true
                },
                {
                    "data": "profile",
                    "title": "Education",
                    "name": "profile.education",
                    "searchable": true,
                    "orderable": true
                },
                {
                    "data": "profile",
                    "title": "Occupation",
                    "name": "profile.profession",
                    "searchable": true,
                    "orderable": true
                },
                {
                    "data": "profile",
                    "title": "Nationality",
                    "name": "profile.nationality",
                    "searchable": true,
                    "orderable": true
                },
                {
                    "data": "profile",
                    "title": "Ethnicity",
                    "name": "profile.ethnicity",
                    "searchable": true,
                    "orderable": true
                },
                {"data": "profile", "title": "DOB", "name": "profile.dob", "searchable": true, "orderable": true},
                {"data": "profile", "title": "Age", "name": "profile.age", "searchable": true, "orderable": true},
                {"data": "profile", "title": "Contact no.", "name": "profile.applicant_contact_no", "searchable": true, "orderable": true},
                {"data": "profile", "title": "Town/City", "name": "profile.town_city", "searchable": true, "orderable": true},
                {"data": "profile", "title": "Associated Mosque", "name": "profile.associated_mosque", "searchable": true, "orderable": true},
                {"data": "profile", "title": "Profile created", "name": "profile.created_at", "searchable": false, "orderable": true},
                {
                    "data": null,
                    "title": "Action",
                    "searchable": false,
                    "orderable": false
                }
            ],
            "order": [[1, 'asc']],
            "rowCallback": function (row, data) {
                var tmpl = _.template($('#trashedAgentListTemplate').html());
                $(row).html(tmpl(data));
            }
        });
    };

    var agentRolesListDataTable = function (agentId) {
        if (agentRolesListingDataTable != null) {
            agentRolesListingDataTable.clear().destroy();
        }
        agentRolesListingDataTable = $('#agentRolesListTable').DataTable({
            "ajax": {
                url: globalScript.APP_URL + "getagentroles/" + agentId,
                type: "GET",
                xhrFields: {
                    withCredentials: true
                },
                dataSrc: function (json) { // Manipulate the data returned from the server
                    if (json.hasOwnProperty('_metadata') && json._metadata.hasOwnProperty('outcomeCode') && json._metadata.outcomeCode === 4) {
                        window.top.location.href = "login";
                        return;
                    }
                    json.draw = json.records.draw;
                    json.recordsTotal = json.records.recordsTotal;
                    json.recordsFiltered = json.records.recordsFiltered;
                    if (json.records.data.length !== 0) {
                        $("body").find(".attach_modal_footer").show();
                    } else {
                        $("body").find(".attach_modal_footer").hide();
                    }
                    return json.records.data;
                }
            },
            "searching": true,
            "ordering": false,
            "serverSide": true,
            "paging": true,
            "columns": [
                {
                    "data": 'display_name',
                    'title': 'Title',
                    "name": 'display_name',
                    "searchable": true,
                    "orderable": true,
                    "visible": true
                },
                {
                    "data": null,
                    'title': 'Action',
                    "searchable": false,
                    "orderable": false,
                    "visible": true
                }
            ],
            "rowCallback": function (row, data) {
                var tmpl = _.template($('#agentRolesListTemplate').html());
                $(row).html(tmpl(data));
            }
        });
    };

    var recoverAllAgents = function (callback) {
        var obj = Object.create(globalScript.RECOVER_ALL_USERS);
        globalScript.ajaxCall(obj).done(function (response) {
            if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                if (typeof callback == 'function') callback();
            }
        })
    };

    var permanentlyDeleteAllAgents = function (callback) {
        var obj = Object.create(globalScript.PER_DELETE_ALL_USERS);
        globalScript.ajaxCall(obj).done(function (response) {
            if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                if (typeof callback == 'function') callback();
            }
        })
    };

    var updateAgentStatus = function (status, callback) {
        var obj = Object.create(globalScript.UPDATE_USER_STATUS);
        obj.data = {
            'user_ids[]': selectedAgents,
            'status': status
        };
        globalScript.ajaxCall(obj).done(function (response) {
            selectedAgents = [];
            if (typeof callback == 'function') callback();
        });
    };

    var updateAgentBlock = function (status, callback) {
        var obj = Object.create(globalScript.UPDATE_USER_BLOCK);
        obj.data = {
            'agent_ids[]': selectedAgents,
            'status': status
        };
        globalScript.ajaxCall(obj).done(function (response) {
            selectedAgents = [];
            if (typeof callback == 'function') callback();
        });
    };

    var populateValuesToProfileModal = function (data, callback) {
        var tmpl = _.template($('#profileDetailsTemplate').html());
        $("#profile_details_table").html(tmpl(data));
        if (typeof callback == 'function') callback();
    };

    return {
        'init': init
    }

}();

$(document).ready(function () {
    agentsScript.init();
});
