home.js 11 KB

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