home.js 12 KB

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