$data.groups = null; $data.grade_names = {}; $data.grade_colors = {}; $data.stat = null; $data.file = {}; $data.units = null; $data.search = ""; $data.govunit = null; $data.govunit_score = null; $data.govunit_grade = null; $data.loading = false; $data.forms = {}; $data.maingroup = {}; $data.job = {}; $data.kpilist = null; $data.unitlist = []; $data.map_dialog=0; $methods.ready = function() { this.map = null; setTimeout(() => { this.load_reportdata(); }, 100); } $methods.numformat = function (n) { if (!n || isNaN(n)) return 0; if (n == null) return ""; if (n === undefined) return ""; if (!Number.isInteger(n)) n = Number.parseInt(n); return n.toLocaleString(); }; $methods.f2 = function (n) { if (n == null) return ""; if (n === undefined) return ""; if (!Number.isFinite(n)) return ""; return Number.parseFloat(n).toFixed(2); }; $methods.pc = function (v, n) { if (!v) return 0; if (n == null || n === undefined || n <= 0) return ""; if (!Number.isFinite(n)) return ""; v = Number.parseFloat(v); n = Number.parseFloat(n); return this.f2((v * 100) / n); }; async function api_calljob(path, cache = 0, reload = 0, onprogress = null) { const now = Math.floor(new Date().getTime() / 1000); if (cache && cache_has(path)) { var d = cache_get(path); if (cache < 10) cache = 10; if (now - d._time > cache) { cache_remove(path); } else if (d.contentType == "json") { if (d.ok || d.progress >= 100) return d.data; } } try { var config = { method: "GET", cache: "no-cache", headers: { "Content-Type": "application/json", "X-API-Key": "json", }, }; var qreload = ""; if (reload) { if (path.indexOf("?") > 0) qreload = "&reload=" + reload; else qreload = "?reload=" + reload; } const res = await axios.get(path + qreload, {}, config); data = res.data; if (data.contentType == "json") { if (data.ok || data.progress >= 100) { if (cache) { try { cache_set(path, { data: data, _time: now }); } catch (e) {} } } else { if (onprogress != null) { onprogress(data); } } } } catch (error) { console.log(error); data = { ok: false, errors: [error.message] }; window.qalert("เกิดข้อผิดพลาด " + error.message); } if (data.exception) { data.ok = false; window.qalert(data.exception + " " + data.message); } return data; } async function api_getjson(path, cache = 0, reload = false, cachekey = null) { var data = null; if (window._alert) window.qalert = window._alert; const now = Math.floor(new Date().getTime() / 1000); var _key = cachekey ? cachekey : path; if (cache && cache_has(_key)) { var d = cache_get(_key); console.log("cache_get " + _key, d); if (cache < 10) cache = 10; if (now - d._time > cache) { cache_remove(_key); } else { return d.data; } } try { var config = { method: "GET", cache: "no-cache", headers: { "Content-Type": "application/json", "X-API-Key": "json", }, }; var qreload = ""; if (reload) { if (path.indexOf("?") > 0) qreload = "&reload=1"; else qreload = "?reload=1"; } const res = await axios.get(path + qreload, {}, config); data = res.data; var _savecache = !!cachekey; if (cache) _savecache = true; if (_savecache && data) { try { cache_set(_key, { data: data, _time: now }); } catch (e) {} } } catch (error) { console.log(error); data = { ok: false, errors: [error.message] }; window.qalert("เกิดข้อผิดพลาด " + error.message); } if (data.exception) { data.ok = false; window.qalert(data.exception + " " + data.message); } return data; } function cache_set(path, data) { var json = JSON.stringify(data); localStorage.removeItem(path); localStorage.setItem(path, json); } $methods.getcolor = function (v) { if (!v) return ""; if (v >= 95) return "purple-lighten-4"; if (v >= 85) return "green-lighten-4"; if (v >= 70) return "orange-lighten-4"; return "red-lighten-4"; }; $methods.getval = function (v) { if (!v) return ""; if (typeof(v)=='string') return v; return v.toFixed(2); }; $methods.get_piechart = function (options, values, a = true) { var data = []; for (var oi in options) { var op = options[oi]; var i = oi; if (a) i = parseInt(oi) + 1; data.push({ value: values[i], name: op }); } return { series: [ { name: "สถิติการตอบ", type: "pie", radius: "50%", data: data, }, ], }; }; $methods.pageclick = async function(pid){ if(!this.pageloaded) this.pageloaded={}; var fload=`page${pid}_load`; var fclick=`page${pid}_click`; if(this[fload] && !this.pageloaded[fload]){ this[fload](); this.pageloaded[fload]=true; return; } if(this[fclick]){ this[fclick](); return; } } $methods.load_reportdata = async function ( ) { this.loading = true; if(!this.report){ var r = await api_getjson("/v/report/api_public?f="+this.report_file); if (r.ok) { this.report = r.report; } else{ return; } } var r = this.report; this.kpilist = r.kpilist; this.groups = r.data; var glist = Object.keys(this.groups); var mainid = 0; if(glist.length==1) mainid=glist[0]; this.maingroup = this.groups[mainid]; this.grade_names = r.grades; this.grade_colors = r.colors; this.stat = r.stat; this.files = r.file; this.forms = r.form; this.pageclick(this.rtab); this.map_loaddata(); this.loading = false; /* var r = await api_getjson("/v/report/api_score_group?t=" + this.gtype + "&y=" + this.year); if (r.ok) { this.kpilist = r.kpilist; this.groups = r.data; this.maingroup = this.groups[0]; this.maingroup.dep = this.depgroups.data[0]; this.grade_names = r.grades; this.grade_colors = r.colors; this.stat = r.stat; this.files = r.file; this.forms = r.form; this.forms.eit.options.department = this.depgroups.options.department; this.forms.eit.options.service = this.depgroups.options.service; this.forms.eit.options.language = this.depgroups.options.language; this.report = r; this.unitlist = []; // รวมทั้งหน่วยงาน และ กลุ่ม this.units = []; // เฉพาะหน่วยงาน this.unitnames = {}; // ชื่อ this.i = 0; this.init_chart(); this.loading = false; this.rtab = "main"; this.pageclick(this.rtab); this.job.status = r.status; this.job.progress = r.progress; if(r.top){ this.top10 = r.top.top10; this.last10 = r.top.last10; }else{ this.top10=null; this.last10=null; } for (var g1 in r.data) { var u1 = r.data[g1]; this.unitlist.push(r.data[g1]); u1.count = u1.stat.count.iit + u1.stat.count.eit; u1.lv = 1; for (var g2 in r.data[g1].units) { var u2 = r.data[g1].units[g2]; u2.id = g2; u2.count = u2.stat.count.iit + u2.stat.count.eit; u2.lv = 2; this.unitlist.push(u2); } } this.job.count = r.keys.length; var i=0; this.loading = true; for (var k in r.keys) { i++; this.job.n = i; this.load_key = r.keys[k]; if(!this.loading) break; var u = await api_getjson("/v/report/api_score_group?t=" + this.gtype + "&y=" + this.year + "&k=" + k); if (u.ok) { this.load_key = u.key; this.report.progress = u.progress; this.report.status = u.status; this.job.status = r.status; this.job.progress = u.progress; data_set(this.report.data, u.key, u.data); for (var i3 in u.data) { var u3 = u.data[i3]; u3.lv = 3; u3.count = u3.stat.count.iit + u3.stat.count.eit; this.unitlist.push(u3); this.unitnames[u3.id] = u3.name; this.units.push(u3); } } } this.loading = false; reindex_reportdata(this.report.data); this.unitlist.sort((a, b) => a.no - b.no); //this.get_top10(this.units); this.job.progress=100; } else { this.maingroup = null; this.job = r; if (this.job.progress < 100 || !this.job.ok) { this.loading = true; setTimeout(() => { this.load_reportdata(); }, 5000); } }*/ } function reindex_reportdata(data) { var i = 0; for (var g1 in data) { i++; var u1 = data[g1]; u1.id = g1; u1.no = i; for (var g2 in u1.units) { i++; var u2 = u1.units[g2]; u2.no = i; u2.id = g2; for (var g3 in u2.units) { var u3 = u2.units[g3]; u3.group1_id = u2.name; i++; u3.no = i; } } } } function data_set(data, key, val) { var keys = key.split("."); var p = data; var len = keys.length; var sk = ""; for (k in keys) { sk = keys[k]; if (typeof (p[sk]) == 'undefined') { p[sk] = {} }; if (k < len - 1) { p = p[sk]; } } p[sk] = val; }