define(["datetime","loading","libraryMenu","dom","globalize","emby-button"],function(datetime,loading,libraryMenu,dom,globalize){"use strict";function revoke(page,key){require(["confirm"],function(confirm){confirm(globalize.translate("MessageConfirmRevokeApiKey"),globalize.translate("HeaderConfirmRevokeApiKey")).then(function(){loading.show(),ApiClient.ajax({type:"DELETE",url:ApiClient.getUrl("Auth/Keys/"+key)}).then(function(){loadData(page)})})})}function renderKeys(page,keys,users){var rows=keys.map(function(item){var html="";html+='',html+='',html+='",html+="",html+='',html+=item.AccessToken,html+="",html+='',html+=item.AppName||"",html+="",html+='',html+=item.DeviceName||"",html+="",html+='';var user=users.filter(function(u){return u.Id==item.UserId})[0];user&&(html+=user.Name),html+="",html+='';var date=datetime.parseISO8601Date(item.DateCreated,!0);return html+=datetime.toLocaleDateString(date)+" "+datetime.getDisplayTime(date),html+="",html+=""}).join(""),resultBody=page.querySelector(".resultBody");resultBody.innerHTML=rows,loading.hide()}function loadData(page){loading.show(),ApiClient.getUsers().then(function(users){ApiClient.getJSON(ApiClient.getUrl("Auth/Keys")).then(function(result){renderKeys(page,result.Items,users)})})}function showNewKeyPrompt(page){require(["prompt"],function(prompt){prompt({title:globalize.translate("HeaderNewApiKey"),label:globalize.translate("LabelAppName"),description:globalize.translate("LabelAppNameExample")}).then(function(value){ApiClient.ajax({type:"POST",url:ApiClient.getUrl("Auth/Keys",{App:value})}).then(function(){loadData(page)})})})}function getTabs(){return[{href:"dashboardhosting.html",name:globalize.translate("TabHosting")},{href:"serversecurity.html",name:globalize.translate("TabSecurity")}]}pageIdOn("pageinit","serverSecurityPage",function(){var page=this;page.querySelector(".btnNewKey").addEventListener("click",function(){showNewKeyPrompt(page)}),page.querySelector(".tblApiKeys").addEventListener("click",function(e){var btnRevoke=dom.parentWithClass(e.target,"btnRevoke");btnRevoke&&revoke(page,btnRevoke.getAttribute("data-token"))})}),pageIdOn("pagebeforeshow","serverSecurityPage",function(){libraryMenu.setTabs("adminadvanced",1,getTabs);var page=this;loadData(page)})});