$(document).ready(function(){function e(){$.get("/api/admin/users").done(function(e){$("#users-tbody").empty(),$("#user-select").html(''),e&&0!==e.length?e.forEach(function(e){var n=e.role||"unbekannt",t=''+n+"",a=e.created_at?new Date(e.created_at).toLocaleDateString("de-DE"):"-";if("client"===n&&e.expires_at){var r=new Date(e.expires_at).toLocaleDateString("de-DE");e.expired?(t='abgelaufen',a="Abgelaufen: "+r):a="Bis: "+r}var s='";s+='",$("#users-tbody").append("
Keine Unterordner in Websitebilder gefunden. Erstelle Ordner in Nextcloud.
")}),$("body").removeClass("is-preload"))}).fail(function(){window.location.href="/"}),$("#logout-btn").click(function(e){e.preventDefault(),$.post("/api/auth/logout").done(function(){window.location.href="/"})}),$("#create-user-form").submit(function(n){n.preventDefault();var t=$("#new-username").val().trim(),a=$("#new-password").val(),r=$("#new-role").val();t&&a?$.ajax({url:"/api/admin/users",type:"POST",contentType:"application/json",data:JSON.stringify({username:t,password:a,role:r})}).done(function(n){alert('Nutzer "'+t+'" erfolgreich erstellt!'),$("#new-username").val(""),$("#new-password").val(""),e()}).fail(function(e){var n="Unbekannter Fehler";e.responseJSON&&e.responseJSON.error?n=e.responseJSON.error:e.status&&(n="HTTP "+e.status+": "+e.statusText),alert("Fehler beim Erstellen: "+n)}):alert("Bitte Username und Passwort eingeben.")}),window.deleteUser=function(n,t){confirm('Nutzer "'+t+'" wirklich loeschen?')&&$.ajax({url:"/api/admin/users/"+n,type:"DELETE"}).done(e).fail(function(e){alert(e.responseJSON?e.responseJSON.error:"Fehler")})},window.changePassword=function(e,n){var t=prompt('Neues Passwort fuer "'+n+'":');t&&$.ajax({url:"/api/admin/users/"+e+"/password",type:"PUT",contentType:"application/json",data:JSON.stringify({new_password:t})}).done(function(){alert("Passwort geaendert!")}).fail(function(e){alert(e.responseJSON?e.responseJSON.error:"Fehler")})},$("#user-select").change(function(){var e=$(this).val();$(".folder-cb").prop("checked",!1),e&&$.get("/api/admin/assign?user_id="+e).done(function(e){e.forEach(function(e){$('.folder-cb[value="'+e+'"]').prop("checked",!0)})})}),$("#save-assignments").click(function(){var e=$("#user-select").val();if(!e)return alert("Bitte Nutzer waehlen");var n=[];$(".folder-cb:checked").each(function(){n.push($(this).val())}),$.ajax({url:"/api/admin/assign",type:"POST",contentType:"application/json",data:JSON.stringify({user_id:e,folders:n})}).done(function(){$("#assign-status").text("Gespeichert!").fadeIn().delay(2e3).fadeOut()}).fail(function(){alert("Fehler beim Speichern")})}),$("#admin-pw-form").submit(function(e){e.preventDefault(),$.post("/api/auth/change-password",{old_password:$("#admin-pw-old").val(),new_password:$("#admin-pw-new").val()}).done(function(){alert("Passwort geaendert!"),$("#admin-pw-old, #admin-pw-new").val("")}).fail(function(e){alert(e.responseJSON?e.responseJSON.error:"Fehler")})})});