home.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. start();
  2. user_role = '';
  3. data = '';
  4. obj = '';
  5. // 之後透過 fetch 取得fastapi後端資料!
  6. function start(){
  7. var xhr = new XMLHttpRequest();
  8. var url = "home/show";
  9. obj = "";
  10. xhr.open("GET",url);
  11. xhr.onload = function(){
  12. obj = JSON.parse(xhr.responseText);
  13. data = obj ;
  14. console.log(data);
  15. document.getElementById('company_text').innerHTML=data[1][0].company;
  16. document.getElementById('factory_text').innerHTML=data[1][0].factory;
  17. document.getElementById('department_text').innerHTML=data[1][0].department;
  18. if(data[0].user_role==1){
  19. company_show();
  20. }
  21. else if(data[0].user_role==2){
  22. factory_show(data[1][0].company);
  23. }
  24. else if(data[0].user_role==3){
  25. department_show(data[1][0].company,data[1][0].department);
  26. department_change();
  27. }
  28. else{
  29. towerGroup_show();
  30. }
  31. //insertHTML();
  32. };
  33. xhr.send();
  34. console.log('Start');
  35. }
  36. function get_user_role(){
  37. user_role = data[0].user_role
  38. }
  39. function clear(){
  40. document.getElementById('company').innerHTML='';
  41. document.getElementById('towergroup').innerHTML='';
  42. document.getElementById('company_name').innerHTML='';
  43. document.getElementById('factory_select').innerHTML='';
  44. document.getElementById("map").style.display="none";
  45. document.getElementById('department_show').innerHTML="";
  46. document.getElementById("department_list").style.display="none";
  47. }
  48. function company_show(){
  49. //顯示company按鈕
  50. var inner='';
  51. var company_arr = new Array();
  52. for(var i=0;i<obj[1].length;i++){
  53. if(company_arr.indexOf(obj[1][i].company)==-1){
  54. inner+="<button id='company " +obj[1][i].company+ "' onclick=\"factory_show(\'"+obj[1][i].company+"\');\"";
  55. inner += ">公司"+obj[1][i].company+"</button><br>";
  56. company_arr.push(obj[1][i].company);
  57. console.log(obj[1][i].company);
  58. }
  59. }
  60. clear();
  61. document.getElementById('company').innerHTML=inner;
  62. company_health(company_arr);
  63. }
  64. function company_health(company_arr){
  65. //使公司按鈕變色
  66. for(var i=0;i<company_arr.length;i++){
  67. //初始化(健康)
  68. document.getElementById('company ' + company_arr[i]).style.background="green";
  69. }
  70. for(var i=0;i<obj[1].length;i++){
  71. if(obj[1][i].health!=1 && obj[1][i].company==document.getElementById('company_text').innerHTML){
  72. //有不健康的就改色
  73. document.getElementById('company ' + obj[1][i].company).style.background="red";
  74. }
  75. }
  76. }
  77. function factory_show(company){
  78. var inner='<p>廠區資訊</p><br>';
  79. var factory_arr = new Array();
  80. for(var i=0;i<obj[1].length;i++){
  81. if(factory_arr.indexOf(obj[1][i].factory)==-1 && obj[1][i].company==company){
  82. inner+="<button id='factory " +obj[1][i].factory+ "' onclick=\"department_show(\'"+obj[1][i].company+"\',\'"+obj[1][i].factory+"\');\"";
  83. inner += ">廠區"+obj[1][i].factory+"</button><br>";
  84. factory_arr.push(obj[1][i].factory);
  85. }
  86. }
  87. //測試用
  88. inner+="<button id='factory B' onclick=\"department_show(\'"+obj[1][0].company+"\',\'B\');\"";
  89. inner += ">廠區B</button><br>";
  90. inner+="<button id='factory C' onclick=\"department_show(\'"+obj[1][0].company+"\',\'C\');\"";
  91. inner += ">廠區C</button><br>";
  92. document.getElementById('company').innerHTML="";
  93. document.getElementById('company_name').innerHTML="公司 "+ company + "<button onclick=\"company_show();\">返回</button>";
  94. document.getElementById('factory_select').innerHTML=inner;
  95. factory_health(factory_arr);
  96. document.getElementById("map").style.display="";
  97. }
  98. function factory_health(factory_arr){
  99. //使工廠按鈕變色
  100. for(var i=0;i<factory_arr.length;i++){
  101. //初始化(健康)
  102. document.getElementById('factory ' + factory_arr[i]).style.background="green";
  103. }
  104. for(var i=0;i<obj[1].length;i++){
  105. if(obj[1][i].health!=1){
  106. //有不健康的改色
  107. document.getElementById('factory ' + obj[1][i].factory).style.background="red";
  108. }
  109. }
  110. }
  111. function department_show(company,factory){
  112. var inner='';
  113. var department_arr = new Array();
  114. for(var i=0;i<obj[1].length;i++){
  115. if(department_arr.indexOf(obj[1][i].department)==-1 && obj[1][i].company==company&& obj[1][i].factory==factory){
  116. inner+='<option value="'+obj[1][i].department+'">課'+obj[1][i].department+'</option>';
  117. department_arr.push(obj[1][i].department);
  118. }
  119. }
  120. document.getElementById('company_text').innerHTML=company;
  121. document.getElementById('factory_text').innerHTML=factory;
  122. document.getElementById('department_list').innerHTML=inner;
  123. console.log("click");
  124. if(factory==='A'){
  125. newLocation(25.0339031,121.5623212);
  126. }
  127. else if(factory==='B'){
  128. newLocation(24.42,121.42);
  129. }
  130. else if(factory==='C'){
  131. newLocation(24.12,121.42);
  132. }
  133. document.getElementById("department_list").style.display="";
  134. department_change();
  135. }
  136. function department_change(){
  137. console.log("change");
  138. var inner='<table><tr><td>機組</td><td>健康狀態</td><td>查看</td></tr>';
  139. var tower_group_arr = new Array();
  140. for(var i=0;i<obj[1].length;i++){
  141. if(tower_group_arr.indexOf(obj[1][i].group)==-1 && obj[1][i].company==document.getElementById('company_text').innerHTML&& obj[1][i].factory==document.getElementById('factory_text').innerHTML&& obj[1][i].department==document.getElementById("department_list").value){
  142. inner+='<tr><td>' + obj[1][i].group + '</td><td>健康</td><td><button onclick=\"towerGroup_show();\">更多</button></td></tr>' ;
  143. tower_group_arr.push(obj[1][i].group);
  144. }
  145. }
  146. inner += '</table>'
  147. document.getElementById('department_text').innerHTML=document.getElementById("department_list").value;
  148. document.getElementById('department_show').innerHTML=inner;
  149. }
  150. function towerGroup_show(){
  151. var inner= '';
  152. var tower_group_arr = new Array();
  153. for(var i=0;i<obj[1].length;i++){
  154. if(tower_group_arr.indexOf(obj[1][i].group)==-1 && obj[1][i].company==document.getElementById('company_text').innerHTML&& obj[1][i].factory==document.getElementById('factory_text').innerHTML&& obj[1][i].department==document.getElementById("department_text").innerHTML){
  155. inner += tower(obj[1][i].group);
  156. tower_group_arr.push(obj[1][i].group);
  157. }
  158. }
  159. document.getElementById('towergroup').innerHTML=inner;
  160. }
  161. function tower(tower_group){
  162. //顯示tower
  163. var inner= '<p>機組'+tower_group +'</p><br><table><tr><td>設備</td><td>馬達效能</td><td>查看</td></tr>';
  164. for(var i=0;i<obj[1].length;i++){
  165. if(obj[1][i].company==document.getElementById('company_text').innerHTML&& obj[1][i].factory==document.getElementById('factory_text').innerHTML&& obj[1][i].department==document.getElementById("department_text").innerHTML&&obj[1][i].group==tower_group){
  166. inner+='<tr><td>' + obj[1][i].tower + '</td><td>健康</td><td>更多</td></tr>';
  167. }
  168. }
  169. inner += '</table>'
  170. return inner;
  171. }
  172. function newLocation(newLat,newLng)
  173. {
  174. map.setCenter({
  175. lat : newLat,
  176. lng : newLng
  177. });
  178. addmarker(newLat,newLng);
  179. }
  180. var map;
  181. function initMap() {
  182. var position = {
  183. lat: 25.0339031,
  184. lng: 121.5623212
  185. };
  186. map = new google.maps.Map(document.getElementById('map'), {
  187. center: position,
  188. zoom: 14
  189. });
  190. var marker = new google.maps.Marker({
  191. position: position,
  192. map: map
  193. });
  194. }
  195. function addmarker(lat,lng){
  196. var myLatlng = new google.maps.LatLng(lat,lng);
  197. var marker = new google.maps.Marker({
  198. position: myLatlng,
  199. });
  200. console.log("yes");
  201. // To add the marker to the map, call setMap();
  202. marker.setMap(map);
  203. }
  204. function clearMarkers(lat,lng) {
  205. var myLatlng = new google.maps.LatLng(lat,lng);
  206. marker = google.maps.Marker({
  207. position: myLatlng,
  208. title:"Hello World!"
  209. });
  210. markers.setMap(null);
  211. }
  212. function hide(){
  213. document.getElementById("map").style.display="none";//隱藏
  214. }