"use strict";!function(e){var o,a=new function(){var o=this;o.root="../..",o.original_id=e(document).getUrlParam("id"),o.roles=[],o.file=null,o.fullname=ko.observable().extend({required:!0,maxLength:255}),o.uname=ko.observable().extend({required:!0,maxLength:20}),o.password=ko.observable().extend({required:!0,maxLength:128}),o.assignedRoles=ko.observableArray(),o.assignedRolesSelected=ko.observableArray(),o.availableRoles=ko.observableArray(),o.availableRolesSelected=ko.observableArray(),o.areRolesExpanded=ko.observable(!1),o.title=ko.observable(),o.email=ko.observable().extend({email:!0}),o.phone=ko.observable().extend({pattern:"^[+()0-9-]{6,40}$"}),o.mobile=ko.observable().extend({pattern:"^[+()0-9-]{6,40}$"}),o.imname=ko.observable(),o.imtype=ko.observable(),o.image=ko.observable(),o.isnew=ko.observable(!o.original_id),o.focusOnFirstField=ko.observable(o.isnew()),o.modified=ko.observable(!1),o.shouldPushDown=function(){return!(ISPHONE||ISTABLET)},o.toggleRolesExpand=function(){o.areRolesExpanded(!o.areRolesExpanded())},o.hasErrors=function(){return!(o.fullname.isValid()&&o.uname.isValid()&&o.password.isValid()&&o.email.isValid()&&o.phone.isValid()&&o.mobile.isValid())},o.cancel=function(){"1"==e("#isadmin").val()?window.location=".":window.location.reload()},o.assignRoles=function(){o.move(o.availableRolesSelected(),o.availableRoles,o.assignedRoles),o.availableRolesSelected.removeAll(),o.setModified()},o.removeRoles=function(){o.move(o.assignedRolesSelected(),o.assignedRoles,o.availableRoles),o.assignedRolesSelected.removeAll(),o.setModified()},o.move=function(e,o,a){a.push.apply(a,e),o.removeAll(e),a.sort(n),o.sort(n)},o.setModified=function(){return o.isnew()||o.modified(!0),!0},o.deleteUser=function(){e.post(o.root+"/admin/users/delete","id="+o.original_id,(function(e){window.location="."})).fail(MMONIT.error.json)},o.parseUser=function(e){ko.mapping.fromJS(e,{},o);var a=o.image();a=a&&a.length>0?o.root+"/uploads/"+a:i(),document.getElementById("preview").style.background="url("+a+") no-repeat center"},o.saveUser=function(){setTimeout("$('#button-spinner').addClass('visible')",300);for(var a=e("#theform input:not('#isadmin'), #theform select").serialize(),n=o.assignedRoles(),i=0;i<n.length;i++)a+="&roleid="+n[i].id;var r=o.root+"/admin/users/create";if(o.original_id&&(a+="&id="+o.original_id,r=o.root+"/admin/users/update"),o.file){var l="id"+(new Date).getTime()+o.file.name.substr(o.file.name.lastIndexOf(".")),s=new FormData;s.append("fileid",l),s.append("uploadimage",o.file),e.ajax({url:"upload",data:s,type:"POST",cache:!1,contentType:!1,processData:!1,success:function(){e.post(r,a+"&image="+l,(function(){"1"==e("#isadmin").val()?window.location=".":window.location.reload()})).fail(MMONIT.error.json)}}).fail(MMONIT.error.json)}else e.post(r,a,(function(){"1"==e("#isadmin").val()?window.location=".":window.location.reload()})).fail(MMONIT.error.json)};var n=function(e,o){var a=e.name.toLowerCase().localeCompare(o.name.toLowerCase());return a||(e.name>o.name?1:e.name<o.name?-1:0)};let i=()=>{let e=o.root+"/img/avatar-3.png";return o.assignedRoles().find((e=>"admin"===e.name))?e=e.replace("3","1"):o.assignedRoles().find((e=>"collector"===e.name))&&(e=e.replace("3","2")),e};o.original_id?(o.modified(!0),"1"==e("#isadmin").val()?e.when(e.getJSON(o.root+"/admin/roles/list"),e.getJSON(o.root+"/admin/users/get?id="+o.original_id)).then(((e,i)=>{o.roles=e[0].sort(n),o.availableRoles(a.roles.slice());var r=o.availableRoles.remove((function(e){for(var o=0;o<i[0].roles.length;o++)if(i[0].roles[o]==e.id)return!0;return!1}));o.assignedRoles.push.apply(o.assignedRoles,r),o.parseUser(i[0])})):e.getJSON(o.root+"/admin/users/get?id="+o.original_id,(function(e){o.parseUser(e)})).fail(MMONIT.error.json)):(o.isnew(!0),e.getJSON(o.root+"/admin/roles/list",(function(e){o.roles=e.sort(n),o.availableRoles(a.roles.slice())})).fail(MMONIT.error.json),document.getElementById("preview").style.background="url("+o.root+"/img/avatar-3.png) no-repeat center")};ko.bindingHandlers.selectText={init:function(o){e(o).on("click",(function(){e(this).select()}))}},ko.validation.init({insertMessages:!1,decorateElement:!0,errorElementClass:"error"}),ko.applyBindings(a),e(window).width()<768?e("#actions .btn").addClass("btn-block"):e("span.accessory input").prop("size",100),o=document.getElementById("preview"),document.getElementById("fileInput").onchange=function(n){n.preventDefault();var i=n.target.files[0];return function(o){var a=o.name.substr(o.name.lastIndexOf(".")+1).toLowerCase();-1==e.inArray(a,["gif","png","tiff","jpg","jpeg"])&&MMONIT.error.abort("File type not supported. Supported file types are gif, png, tiff, jpeg and jpg")}(i),function(e){var n=new FileReader;n.onload=function(n){a.file=e,o.style.background="url("+n.target.result+") no-repeat center"},n.readAsDataURL(e)}(i),!1}}(jQuery);