define(["jQuery","loading","humanedate","paper-icon-button-light","cardStyle"],function($,loading){"use strict";function deleteUser(page,id){var msg=Globalize.translate("DeleteUserConfirmation");require(["confirm"],function(confirm){confirm({title:Globalize.translate("DeleteUser"),text:msg,confirmText:Globalize.translate("ButtonDelete"),primary:"cancel"}).then(function(){loading.show(),ApiClient.deleteUser(id).then(function(){loadData(page)})})})}function showUserMenu(elem){var card=$(elem).parents(".card")[0],page=$(card).parents(".page")[0],userId=card.getAttribute("data-userid"),menuItems=[];menuItems.push({name:Globalize.translate("ButtonOpen"),id:"open",ironIcon:"mode-edit"}),menuItems.push({name:Globalize.translate("ButtonLibraryAccess"),id:"access",ironIcon:"lock"}),menuItems.push({name:Globalize.translate("ButtonParentalControl"),id:"parentalcontrol",ironIcon:"person"}),menuItems.push({name:Globalize.translate("ButtonDelete"),id:"delete",ironIcon:"delete"}),require(["actionsheet"],function(actionsheet){actionsheet.show({items:menuItems,positionTo:card,callback:function(id){switch(id){case"open":Dashboard.navigate("useredit.html?userId="+userId);break;case"access":Dashboard.navigate("userlibraryaccess.html?userId="+userId);break;case"parentalcontrol":Dashboard.navigate("userparentalcontrol.html?userId="+userId);break;case"delete":deleteUser(page,userId)}}})})}function getUserHtml(user,addConnectIndicator){var html="",cssClass="card squareCard scalableCard squareCard-scalable";user.Policy.IsDisabled&&(cssClass+=" grayscale"),html+="
",html+='
',html+='",html+='
',html+='
',html+='',html+="
",html+='
',html+=user.Name,html+="
",html+='
';var lastSeen=getLastSeenText(user.LastActivityDate);return html+=""!=lastSeen?lastSeen:" ",html+="
",html+="
",html+="
",html+="
"}function getLastSeenText(lastActivityDate){return lastActivityDate?"Last seen "+humane_date(lastActivityDate):""}function getUserSectionHtml(users,addConnectIndicator){var html="";return html+=users.map(function(u){return getUserHtml(u,addConnectIndicator)}).join("")}function renderUsersIntoElement(elem,users,addConnectIndicator){var html=getUserSectionHtml(users,addConnectIndicator);elem.html(html),$(".btnUserMenu",elem).on("click",function(){showUserMenu(this)})}function renderUsers(page,users){renderUsersIntoElement($(".localUsers",page),users.filter(function(u){return"Guest"!=u.ConnectLinkType}),!0),renderUsersIntoElement($(".connectUsers",page),users.filter(function(u){return"Guest"==u.ConnectLinkType}))}function showPendingUserMenu(elem){var menuItems=[];menuItems.push({name:Globalize.translate("ButtonCancel"),id:"delete",ironIcon:"delete"}),require(["actionsheet"],function(actionsheet){var card=$(elem).parents(".card")[0],page=$(elem).parents(".page")[0],id=card.getAttribute("data-id");actionsheet.show({items:menuItems,positionTo:card,callback:function(menuItemId){switch(menuItemId){case"delete":cancelAuthorization(page,id)}}})})}function getPendingUserHtml(user){var html="",cssClass="card squareCard scalableCard squareCard-scalable";html+="
",html+='
',html+='",html+='
',html+='
',html+='',html+="
",html+='
',html+=user.UserName,html+="
",html+="
",html+="
",html+="
"}function renderPendingGuests(page,users){users.length?$(".sectionPendingGuests",page).show():$(".sectionPendingGuests",page).hide();var html=users.map(getPendingUserHtml).join(""),elem=$(".pending",page).html(html);$(".btnUserMenu",elem).on("click",function(){showPendingUserMenu(this)})}function cancelAuthorization(page,id){loading.show(),ApiClient.ajax({type:"DELETE",url:ApiClient.getUrl("Connect/Pending",{Id:id})}).then(function(){loadData(page)})}function loadData(page){loading.show(),ApiClient.getUsers().then(function(users){renderUsers(page,users),loading.hide()}),ApiClient.getJSON(ApiClient.getUrl("Connect/Pending")).then(function(pending){renderPendingGuests(page,pending)})}function showLinkUser(page,userId){require(["components/guestinviter/connectlink"],function(connectlink){connectlink.show().then(function(){loadData(page)})})}function showInvitePopup(page){Dashboard.getCurrentUser().then(function(user){return user.ConnectUserId?void require(["components/guestinviter/guestinviter"],function(guestinviter){guestinviter.show().then(function(){loadData(page)})}):void showLinkUser(page,user.Id)})}$(document).on("pageinit","#userProfilesPage",function(){var page=this;$(".btnInvite",page).on("click",function(){showInvitePopup(page)}),$(".btnAddUser",page).on("click",function(){Dashboard.navigate("usernew.html")})}).on("pagebeforeshow","#userProfilesPage",function(){var page=this;loadData(page)})});