$(document).ready(function () {
    getRoleList();
    $('#txtSearch').keydown(function (e) {
        if (e.which === 13) {
            refresh();
            return false;
        }
    });
});
function openModal(value) {
    var src = "Setting/RoleEdit?roleCode=" + value;
    openInModal('', src, window.top.editRoles, '700', '');
}
function deleteUser(value) {
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            loading: true,
            url: "GeneralApi/DeleteRole",
            data: { code: value },
            success: function (result) {
                showMsgModal(2, window.top.deletedSuccessfully);
                refresh();
            }
        });
    }
}
function refresh() {
    $("#divRoleList").empty();
    getRoleList();
}
async function getRoleList() {
    await ajax({
        loading: true,
        url: "GeneralApi/GetRoleList",
        data: { search: $("#txtSearch").val() },
        success: function (result) {
            result.forEach(function (item) {
                var $div = $("#divTemplate").clone();
                var onclickStr = "openInModal('','Setting/RoleSave?code=" + item.Code + "','" + window.top.saveRoleFormTitle + "','900','')";
                $div.removeAttr("hidden");
                $div.find("[data-id='Name']").text(item.Name);
                $div.find("[data-id='save']").attr("onclick", onclickStr);
                $div.find("[data-id='delete']").attr("onclick", "deleteUser("+item.Code+")");
                $("#divRoleList").append($div);
            });
        }
    });
}