var USER = { auth:null, async login(scope) { if(!scope.info) { console.log("no info??"); return; } var path = "./_php/user.php" var req = { action:"userLogin", input: { data:scope.info, } } var res = await UTIL.apiCall(path,req); var hash = (res||{}).hash; if( hash) { localStorage.setItem("user_hash", hash); scope.$emit("onLogin",res) } else { var h = "Login Failed" var m = "Invalid User name or password" scope.$root.popup.prompt(h,m) } }, async logout(scope) { var h = "Log out" var m = "Are you sure you want to logout?" scope.$root.popup.confirm(h,m, async () => { var path = "./_php/user.php" var auth = localStorage.getItem('user_hash'); var req = { action:"userLogout", input: { hash:auth } } var res = await UTIL.apiCall(path,req); this.auth = null console.log("res",res); if((res||{}).end) { localStorage.removeItem('user_hash'); scope.$root.nav.setComp("p-user-page"); } }); }, async load() { var path = "./_php/user.php" var auth = localStorage.getItem('user_hash'); var req = { action:"getUser", input: { auth:auth } } return await UTIL.apiCall(path,req); }, async checkAuth() { var hash = localStorage.getItem('user_hash'); if(!hash) return {}; var path = "./_php/user.php"; var data = { hash:hash }; var req = { action:"getLevel", input: data } this.auth = await UTIL.apiCall(path,req); return this.auth; }, getNameFullName() { var au = this.auth; console.log("ello",au); if(UTIL.Empty(au)) return "" if(au.end) return "Guest" return (au.name||"")+" "+(au.surname||""); }, async onSignup(scope) { var d = scope.info; var error = ""; if(d.pass != d.pass_confirm) { error = "Please ensure password and confirm match" } if(!d.email || !d.pass ) { if(!error) error = "Please enter email or password" else error += "and enter email or password" } if(scope.fields.email.has_error) { error = "Email is invalid" } var path = "./_php/user.php" var req = { action:"checkEmail", input: { email:d.email, pass:d.pass } } var res = await UTIL.apiCall(path,req); if(res.email) { error = "Email is already in use" } if(error) this.$root.popup.prompt("Signup Error",error) else { delete d.pass_confirm; req = { action:"save", input: { data:d } } var res = await UTIL.apiCall(path,req); console.log(res); scope.info = d; USER.login(scope); } } } var LIST = { mixins:[COMP.base], props: { show_heading: { default:true }, _onSelect:{}, }, watch: { parent: { deep:true, handler(val) { this.requestFetch() } }, }, data() { return { path:"", search:"", fields:{}, rows:{}, action:null } }, created() { if(!this.path) console.log("no path to api"); }, methods: { async load(input) { // var rows_pre = UTIL.deepCopy(this.rows); if( !UTIL.Empty(this.parent)) { var type = this.parent.type?this.parent.type:"account"; this.path = "./api/"+type+".php"; input.parent = this.parent; } if( !UTIL.Empty(this.child)) { input.child = this.child; } var req = { action: this.action? this.action:(UTIL.Empty(this.parent)?"list":"link_list"), input: input } var rows= await WEB.api(this.path,req); if(this.$refs.list) { if(this.$refs.list.can_lock) for (var k in rows) { var row = rows[k]; // console.log(row.lock); if(!row.lock) row.lock=false; } } this.rows = rows; }, onSelect() { console.log("please add a onSelect method"); }, select(row) { this.$emit("onSelect",row); }, requestFetch() { if(this.$refs.list) this.$refs.list.requestFetch(); }, buildIputText(label,cls) { return {label:label,type:"input_text",cls_header:cls} }, buildImage(label,cls) { return {label:label,type:"image",col_cls:cls} }, buildText(label,cls) { return {label:label,col_cls:cls} }, buildSvg(label,cls) { return {label:label,type:"svg",col_cls:cls} }, buildIputNumber(label,cls,func) { return {label:label,type:"input_number",cls_header:cls,func:func} }, buildFunction(label,cls,func) { return {label:label,type:"function",cls_header:cls,func:func} }, buildTypeOptions(label,options,cls) { return { label:label, type:"option", options:options, cls_header:cls } }, }, } var FORM= { mixins:[COMP.base], props:["btns_show"], methods: { buildField(label,type,_default) { return{ label:label, type:type||"text", default:_default } }, buildOption(label,options,default_,onSelect) { return{ label:label, type:"option", options:options, default:default_, onSelect:onSelect?onSelect:()=>{} } }, buildFunction(label,cls,func) { return {label:label,type:"function",cls_header:cls,func:func} }, loadBtns() { var btns = {} if(!this.readonly) { btns.save = { text:"Save", onClick:this.save }; if(this.val?.id) btns.delete = { text:"Delete", onClick:this.delete } } this.btns = btns; }, async save() { if(this.form_data) { delete this.form_data.type; var req = { action:"save", input: { data:this.form_data } } var res = await WEB.api(this.path,req); this.$parent.$emit("onSave",res); POPUP.close(); POPUP.prompt("Save Successful","Data saved successfully"); } }, async delete() { var name = this.form_data?.name; var msg = "Are you sure you want to delete : "+name+"?"; var heading = "Delete "+name+"?"; POPUP.confirm(heading,msg,async ()=> { var req = { action:"delete", input: this.form_data } var res = await WEB.api(this.path,req); this.$emit("onDelete",res); POPUP.close(); }) } }, } var FILE = { loaded :{}, async save(file,account_id) { var res = {}; if(file.data) { file.name = file.name?file.name:UTIL.genHash(6); var path_file = "./d/api/file.php"; var res_file = { action:"save", input: { name:file.name, data:file.data, account_id:account_id } } if(file.id)res_file.input.id = file.id; console.log(res_file); res = await WEB.api(path_file,res_file); } return res; }, async get(image) { // var image_id = image?.id; // var image_size = image?.size||"<300"; // var res = FILE.loaded[image_id+"_"+image_size]; // if(!res) // { var path = "./d/api/file.php"; var res_file = { action:"get", input: image } res = await WEB.api(path,res_file); // console.log(res,"res_file"); // FILE.loaded[image_id+"_"+image_size]=res; // } return res; }, async toCsv(text) { var path = "./d/api/csv.php"; var req = { action:"getArrayFromCSV", input: { data:text } } return awaitWEB.api(path,req, true); } }