home.js 12 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' style='font-size:24px; padding:20px 100px' class='btn text-white me-3 mb-3' 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 style='padding:20px 30px;font-size:20px;' class='btn btn-primary mb-3' 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+="<div><button type='button' style='font-size:24px; padding:20px 100px' class='btn btn btn-success me-3 mb-3' id='factory B' onclick=\"change_map(\'"+obj[1][0].company+"\',\'B\');\"";
  112. inner += ">廠區B</button><button style='padding:20px 30px;font-size:20px;' class='btn btn-primary mb-3' onclick=\"department_show(\'"+obj[1][0].company+"\',\'B\');\">查看 </button></div>";
  113. inner+="<div><button type='button' style='font-size:24px; padding:20px 100px' class='btn btn btn-success me-3 mb-3' id='factory C' onclick=\"change_map(\'"+obj[1][0].company+"\',\'C\');\"";
  114. inner += ">廠區C</button><button style='padding:20px 30px;font-size:20px;' class='btn btn-primary mb-3' onclick=\"department_show(\'"+obj[1][0].company+"\',\'C\');\"> 查看</button></div>";
  115. document.getElementById('company').innerHTML="";
  116. document.getElementById('company_text').innerHTML=company;
  117. document.getElementById('company_name').innerHTML="<button type='button' class='btn btn-primary m-1' onclick=\"company_show();\">返回</button> 公司"+ company + "";
  118. document.getElementById('factory_select').innerHTML=inner;
  119. factory_health(factory_arr);
  120. document.getElementById("map").style.display="";
  121. }
  122. function factory_health(factory_arr){
  123. //使工廠按鈕變色
  124. for(var i=0;i<factory_arr.length;i++){
  125. //初始化(健康)
  126. document.getElementById('factory ' + factory_arr[i]).style.background="green";
  127. }
  128. for(var i=0;i<obj[1].length;i++){
  129. if(obj[1][i].health!=1){
  130. //有不健康的改色
  131. document.getElementById('factory ' + obj[1][i].factory).style.background="red";
  132. }
  133. }
  134. }
  135. function department_show(company,factory){
  136. var inner='';
  137. var department_arr = new Array();
  138. for(var i=0;i<obj[1].length;i++){
  139. if(department_arr.indexOf(obj[1][i].department)==-1 && obj[1][i].company==company/*&& obj[1][i].factory==factory*/){
  140. inner+='<option selected value="'+obj[1][i].department+'">課'+obj[1][i].department+'</option>';
  141. department_arr.push(obj[1][i].department);
  142. }
  143. }
  144. document.getElementById('company_text').innerHTML=company;
  145. document.getElementById('factory_text').innerHTML=factory;
  146. document.getElementById('department_list').innerHTML=inner;
  147. console.log("click");
  148. clear();
  149. document.getElementById('company_name').innerHTML="<button type='button' class='btn btn-primary m-1' onclick=\"factory_show('"+company+"');\">返回</button> 目前位置: 公司 " + company+ " 廠區"+ factory + "";
  150. document.getElementById("department_list").style.display="";
  151. department_change();
  152. }
  153. function change_map(company,factory){
  154. if(factory==='A'){
  155. newLocation(25.0339031,121.5623212);
  156. }
  157. else if(factory==='B'){
  158. newLocation(24.42,121.42);
  159. }
  160. else if(factory==='C'){
  161. newLocation(24.12,121.42);
  162. }
  163. }
  164. function department_change(){
  165. console.log("change");
  166. check = 0 ;
  167. var inner='<table style="font-size: 16px;" class="table text-center"><thead><tr><th>機組</th><th>健康狀態</th><th>機組狀態</th></tr></thead>';
  168. var tower_group_arr = new Array();
  169. for(var i=0;i<obj[1].length;i++){
  170. 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*/){
  171. inner+='<tr><td>' + obj[1][i].group + '</td><td style="color:#078b07" class="fw-bold">健康<i class="fas fa-check-circle ps-1"></i></td>\<td><button class="btn btn-primary m-1" onclick=\"towerGroup_show();\">查看</button></td></tr>' ;
  172. tower_group_arr.push(obj[1][i].group);
  173. console.log(inner);
  174. }
  175. }
  176. inner += '</table>';
  177. document.getElementById('department_text').innerHTML=document.getElementById("department_list").value;
  178. document.getElementById('department_show').innerHTML=inner;
  179. }
  180. function towerGroup_show(){
  181. check = 1;
  182. var inner= '';
  183. var tower_group_arr = new Array();
  184. for(var i=0;i<obj[1].length;i++){
  185. 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*/){
  186. inner += tower(obj[1][i].group);
  187. tower_group_arr.push(obj[1][i].group);
  188. }
  189. }
  190. clear();
  191. company = document.getElementById('company_text').innerHTML
  192. factory= document.getElementById('factory_text').innerHTML
  193. 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 + "";
  194. document.getElementById('towergroup').innerHTML=inner;
  195. }
  196. function tower(tower_group){
  197. //顯示tower
  198. 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>';
  199. for(var i=0;i<obj[1].length;i++){
  200. 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){
  201. inner+='<tr><td>' + obj[1][i].tower + '</td><td style="color:#078b07" class="fw-bold">健康<i class="fas fa-check-circle ps-1"></i></td>\<td><button class="btn btn-primary m-1">查看</button></td></tr>';
  202. }
  203. }
  204. inner += '</table>'
  205. return inner;
  206. }
  207. function newLocation(newLat,newLng)
  208. {
  209. map.setCenter({
  210. lat : newLat,
  211. lng : newLng
  212. });
  213. addmarker(newLat,newLng);
  214. }
  215. var map;
  216. function initMap() {
  217. var position = {
  218. lat: 25.0339031,
  219. lng: 121.5623212
  220. };
  221. map = new google.maps.Map(document.getElementById('map'), {
  222. center: position,
  223. zoom: 14
  224. });
  225. var marker = new google.maps.Marker({
  226. position: position,
  227. map: map
  228. });
  229. }
  230. function addmarker(lat,lng){
  231. var myLatlng = new google.maps.LatLng(lat,lng);
  232. var marker = new google.maps.Marker({
  233. position: myLatlng,
  234. });
  235. console.log("yes");
  236. // To add the marker to the map, call setMap();
  237. marker.setMap(map);
  238. }
  239. function clearMarkers(lat,lng) {
  240. var myLatlng = new google.maps.LatLng(lat,lng);
  241. marker = google.maps.Marker({
  242. position: myLatlng,
  243. title:"Hello World!"
  244. });
  245. markers.setMap(null);
  246. }
  247. function hide(){
  248. document.getElementById("map").style.display="none";//隱藏
  249. }