$(document).ready(function() {
  $("#parentID").change(function() {
    var selectParentID = $("#parentID").val();
    if (selectParentID != "0") {
      $("#syncPerms").css("display", "inline-block");
    }
    else {
      $("#syncPerms").css("display", "none");
    }
  });
  $("#syncPerms").on("click", function() {
    var parentID = $("#parentID").val();
    if (parentID != null) {
      $.ajax({
        url: "/dashboard/ajax/forum/categories?parentID=" + parentID,
        success: function(result) {
          $("#forumPermissions input[type=hidden]").each(function(index, item) {
            item._x_model.set(0);
          });

          for (var i = 0; i < result.length; i++) {
            var roleID = result[i].roleID;
            var permissionID = result[i].permissionID;
            $(`#forumPermissions input[name=permission_${roleID}_${permissionID}]`)[0]._x_model.set(1);
          }
        }
      });
    }
  });
});
