maintenance-communication-edit.component.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. import { Observable } from 'rxjs/Observable';
  2. import { HttpClient } from '@angular/common/http';
  3. import { NgxSpinnerService } from 'ngx-spinner';
  4. import { UserService } from 'src/app/shared/user.service';
  5. import { DashboardService } from './../../../shared/dashboard.service';
  6. import { AuthServiceService } from './../../../shared/auth-service.service';
  7. import { ActivatedRoute, Params } from '@angular/router';
  8. import { Component, OnInit } from '@angular/core';
  9. import { ToastrService } from 'ngx-toastr';
  10. import { Location } from '@angular/common';
  11. import { timer } from 'rxjs';
  12. import { take, map } from 'rxjs/operators';
  13. @Component({
  14. selector: 'app-maintenance-communication-edit',
  15. templateUrl: './maintenance-communication-edit.component.html',
  16. styleUrls: ['./maintenance-communication-edit.component.css']
  17. })
  18. export class MaintenanceCommunicationEditComponent implements OnInit {
  19. pageId: number;
  20. bindingDateSplitEnd;
  21. maintData:any = [];
  22. userData:any = [];
  23. times:any = [];
  24. locationList: any = [];
  25. subLocationList: any = [];
  26. parentCategoriesList = [];
  27. childsCategoriesList: any = [];
  28. adminstrationsIds: any = [];
  29. adminstrations: any = [];
  30. selectedAdminstrations: any = [];
  31. //reopen object
  32. reOpenReasonObject = {
  33. communication_id: null,
  34. reopen_status_reason: '',
  35. }
  36. selectedAll: any;
  37. mainLocationVal = '';
  38. subLocationVal = '';
  39. checkSaveClick: boolean = false;
  40. checkStatusClosed: boolean = false;
  41. communicationData = {
  42. communication_id: null,
  43. parent_category_id: null,
  44. child_category_id: null,
  45. time_period_id: '',
  46. maintenance_notes: '',
  47. status: null,
  48. closed_status_reason: '',
  49. urgent_communication: null,
  50. };
  51. countries = [];
  52. identities = [];
  53. disabledInput: boolean = true;
  54. showAttachements: boolean = false;
  55. disabledVal: boolean = true;
  56. adminstrationsButtonDisabled: boolean = false;
  57. counter$: Observable<number>;
  58. counter = 180;
  59. constructor(private route: ActivatedRoute,
  60. private dashBoardSer: DashboardService,
  61. private userSer: UserService,
  62. private http: HttpClient,
  63. private toastr: ToastrService,
  64. private location: Location,
  65. private spinner: NgxSpinnerService,
  66. public authSer: AuthServiceService) { }
  67. ngOnInit() {
  68. this.spinner.show();
  69. //show / hide notification search in header
  70. this.authSer.notificationLogin = true;
  71. this.authSer.showSearchHeader = false;
  72. this.authSer.showHeaderLogin = false;
  73. this.authSer.showHeaderDashBoard = true;
  74. this.authSer.showDashboardHeader = true;
  75. this.authSer.internalHeader = false;
  76. //reload page every 10 seconds
  77. setTimeout(function(){
  78. location.reload();
  79. },180000);
  80. this.counter$ = timer(0,1000).pipe(
  81. take(this.counter),
  82. map(() => --this.counter)
  83. );
  84. //get maintenance id
  85. this.route.params.subscribe(
  86. (params: Params) => {
  87. this.pageId = params['maintEditId'];
  88. this.communicationData.communication_id = this.pageId;
  89. this.reOpenReasonObject.communication_id = this.pageId;
  90. }
  91. );
  92. //get adminstration list
  93. this.getAdminstrationList('');
  94. //get nationality data
  95. this.userSer.getNationality().subscribe(
  96. (responce) => {
  97. this.countries = responce['countries'];
  98. },
  99. (error) => {
  100. console.log(error);
  101. }
  102. );
  103. //get identites
  104. this.userSer.onGetIdentities().subscribe(
  105. (responce) => {
  106. this.identities = responce['identities'];
  107. },
  108. (error) => {
  109. console.log(error);
  110. }
  111. );
  112. //get parent_categories_list
  113. this.http.get(this.authSer.pathApi + '/parent_categories_list').subscribe(
  114. (responce) => {
  115. this.parentCategoriesList = responce['categories'];
  116. },
  117. (error) => {
  118. console.log(error);
  119. }
  120. );
  121. //get maintanence data by id
  122. this.dashBoardSer.getItemData(this.pageId, 'maintenance').subscribe(
  123. (responce) => {
  124. this.maintData = responce['communication'];
  125. console.log('main data request ', this.maintData);
  126. this.maintData.adminstrations.map(admin => admin.id = admin.adminstration_id);
  127. console.log('after ediiiiiiiiiiiiiiit', this.maintData);
  128. this.communicationData.maintenance_notes = this.maintData.maintenance_notes;
  129. this.communicationData.status = this.maintData.status == 'closed' ? this.maintData.status : false;
  130. this.communicationData.time_period_id = this.maintData.time_period_id;
  131. this.communicationData.closed_status_reason = this.maintData.closed_status_reason ? this.maintData.closed_status_reason : '';
  132. this.communicationData.parent_category_id = this.maintData.parent_category_id;
  133. if(this.communicationData.parent_category_id) {
  134. this.getChildGategories(this.communicationData.parent_category_id);
  135. this.communicationData.child_category_id = this.maintData.child_category_id;
  136. }
  137. //get main_location_id
  138. this.mainLocationVal = this.maintData.main_location_id;
  139. if(this.mainLocationVal) {
  140. this.getSublocationList(this.mainLocationVal);
  141. this.subLocationVal = this.maintData.sub_location_id;
  142. }
  143. this.returnEditData();
  144. //check status closed or no
  145. if(this.maintData.reopen == 1) {
  146. this.checkStatusClosed = true;
  147. } else {
  148. this.checkStatusClosed = false;
  149. }
  150. //make selected adminstarations is true in checkbox and full array of selectedAdminstrations
  151. this.selectedAdminstrations = [];
  152. if(this.maintData.adminstrations.length > 0) {
  153. for(let i = 0; i < this.maintData.adminstrations.length; i++) {
  154. this.selectedAdminstrations.push(this.maintData.adminstrations[i]);
  155. for(let j = 0; j < this.adminstrations.length; j++) {
  156. if(this.maintData.adminstrations[i].adminstration_id == this.adminstrations[j].id) {
  157. this.adminstrations[j].selected = true;
  158. }
  159. }
  160. }
  161. }
  162. this.bindingDateSplitEnd = {
  163. year: parseInt(this.maintData.created_date.split('-')[0]),
  164. month: parseInt(this.maintData.created_date.split('-')[1]),
  165. day: parseInt(this.maintData.created_date.split('-')[2]),
  166. }
  167. this.userData = responce['communication']['user'];
  168. this.spinner.hide();
  169. if(this.maintData['files'].length > 0) {
  170. this.showAttachements = true;
  171. } else {
  172. this.showAttachements = false;
  173. }
  174. },
  175. (error) => {
  176. console.log(error);
  177. }
  178. );
  179. //get time period
  180. this.http.get(this.authSer.pathApi + '/page_list/53/1/1000/for_communication').subscribe(
  181. (response) => {
  182. this.times = response['time_periods'];
  183. },
  184. (error) => {
  185. console.log(error);
  186. }
  187. );
  188. //get location parent
  189. this.http.get(this.authSer.pathApi + '/parent_locations_list').subscribe(
  190. (response) => {
  191. this.locationList = response['locations'];
  192. },
  193. (error) => {
  194. console.log(error);
  195. }
  196. );
  197. }
  198. changeParentCategory(event) {
  199. this.getChildGategories(event.target.value);
  200. }
  201. //get child gategories
  202. getChildGategories(parentId: number) {
  203. this.communicationData.parent_category_id = parentId;
  204. this.http.get(this.authSer.pathApi + '/childs_categories_list_by_parent_id/' + parentId).subscribe(
  205. (responce) => {
  206. this.childsCategoriesList = responce['categories'];
  207. },
  208. (error) => {
  209. console.log(error);
  210. }
  211. );
  212. };
  213. getChildGategoriesVal(event) {
  214. this.communicationData.child_category_id = event.target.value;
  215. }
  216. onChangemainLocation(event){
  217. const id = event.target.value
  218. this.mainLocationVal = id;
  219. this.http.get(this.authSer.pathApi + '/childs_locations_list_by_parent_id/' + id).subscribe(
  220. (response) => {
  221. this.subLocationList = response['locations'];
  222. },
  223. (error) => {
  224. console.log(error)
  225. }
  226. );
  227. }
  228. changeSubLocation(event){
  229. this.subLocationVal = event.target.value;
  230. }
  231. //get adminstration list
  232. getAdminstrationList(dataSearch) {
  233. const searchKey = dataSearch ? dataSearch : '';
  234. this.http.get(this.authSer.pathApi + '/page_list/3/1/1000/all' + searchKey).subscribe(
  235. (responce) => {
  236. this.adminstrations = responce['adminstrations'];
  237. console.log('adminstrationnns', this.adminstrations);
  238. },
  239. (error) => {
  240. console.log(error);
  241. }
  242. );
  243. };
  244. //search function
  245. filtterFunc(event) {
  246. this.adminstrations = [];
  247. this.getAdminstrationList(event.target.value);
  248. }
  249. //make all checkbox of user checked
  250. selectAll() {
  251. for (var i = 0; i < this.adminstrations.length; i++) {
  252. this.adminstrations[i].selected = this.selectedAll;
  253. }
  254. }
  255. //to checked one checkBox
  256. checkIfAllSelected() {
  257. this.selectedAll = this.adminstrations.every(function(item:any) {
  258. return item.selected == true;
  259. });
  260. }
  261. //get departments ids
  262. getDepartments() {
  263. this.adminstrationsIds = [];
  264. this.selectedAdminstrations = [];
  265. for(let i = 0; i < this.adminstrations.length; i++) {
  266. if(this.adminstrations[i].selected == true) {
  267. this.adminstrationsIds.push(this.adminstrations[i].id);
  268. this.selectedAdminstrations.push(this.adminstrations[i]);
  269. }
  270. }
  271. console.log('adminstartion id', this.adminstrationsIds);
  272. console.log('selectedAdminstrations', this.selectedAdminstrations);
  273. }
  274. //get sub location list
  275. getSublocationList(parentId: any) {
  276. this.http.get(this.authSer.pathApi + '/childs_locations_list_by_parent_id/' + parentId).subscribe(
  277. (response) => {
  278. this.subLocationList = response['locations'];
  279. },
  280. (error) => {
  281. console.log(error)
  282. }
  283. );
  284. }
  285. //get value of reason reOpen
  286. getReopenReason(event) {
  287. this.reOpenReasonObject.reopen_status_reason = event.target.value;
  288. }
  289. //reOpen mentainence function
  290. reOpenMaintenance() {
  291. if(!this.reOpenReasonObject.reopen_status_reason){
  292. this.toastr.warning('أدخل إعاده سبب فتح البلاغ !');
  293. } else {
  294. this.http.post(this.authSer.pathApi + '/reopen_closed_communication', this.reOpenReasonObject).subscribe(
  295. (responce) => {
  296. this.toastr.success('تم إعاده الفتح بنجاح');
  297. this.location.back();
  298. },
  299. (error) => {
  300. console.log(error);
  301. this.toastr.error('حدث خطا في الحفظ ، حاول لاحقاً');
  302. }
  303. );
  304. }
  305. }
  306. //remove remove selected adminsration
  307. removeSelectedAdminsration(adminstrationData) {
  308. //to remove checked from adminstrations list
  309. for(let j = 0; j < this.adminstrations.length; j++) {
  310. if(adminstrationData.id == this.adminstrations[j].id) {
  311. this.adminstrations[j].selected = false;
  312. }
  313. }
  314. const index = this.selectedAdminstrations.indexOf(adminstrationData);
  315. this.selectedAdminstrations.splice(index, 1);
  316. }
  317. //return substring of subject and check negaive and positive time
  318. returnEditData() {
  319. for(let i = 0; i < this.maintData['adminstrations'].length; i++) {
  320. this.maintData['adminstrations'][i].countdown = this.maintData['adminstrations'][i].countdown ? this.dashBoardSer.secondsToDhms(this.maintData['adminstrations'][i].countdown) : '-';
  321. }
  322. }
  323. //on print function
  324. onPrint(): void {
  325. window.print();
  326. // let printContents, popupWin;
  327. // printContents = document.getElementById('print-section').innerHTML;
  328. // popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto');
  329. // popupWin.document.open();
  330. // popupWin.document.write(`
  331. // <html>
  332. // <head>
  333. // <title> طباعه بطاقه المتدرب</title>
  334. // <style>
  335. // //........Customized style.......
  336. // </style>
  337. // </head>
  338. // <body onload="window.print();window.close()">${printContents}</body>
  339. // </html>`
  340. // );
  341. // popupWin.document.close();
  342. }
  343. //submitted form
  344. onSubmittedForm() {
  345. this.checkSaveClick = true; //make save btn disabled till request is success
  346. this.adminstrationsIds = [];
  347. if(this.communicationData.status) {
  348. this.communicationData.status = 'closed';
  349. } else {
  350. this.communicationData.status = '';
  351. this.communicationData.closed_status_reason = '';
  352. }
  353. if(this.communicationData.urgent_communication) {
  354. this.communicationData.urgent_communication = '1';
  355. }
  356. // } else {
  357. // this.communicationData.urgent_communication = '0';
  358. // }
  359. for(let i = 0; i < this.selectedAdminstrations.length; i++) {
  360. this.adminstrationsIds.push(this.selectedAdminstrations[i].id);
  361. }
  362. this.communicationData['adminstrations_ids'] = this.adminstrationsIds;
  363. if(this.adminstrationsIds.length == 0 && this.communicationData.urgent_communication == '1') {
  364. this.toastr.warning('قم بإختيار الإدارات');
  365. this.checkSaveClick = false;
  366. }else if( this.communicationData.status == 'closed' && !this.communicationData.closed_status_reason){
  367. this.toastr.warning('قم بإختيار سبب غلق البلاغ !');
  368. this.checkSaveClick = false;
  369. } else {
  370. if(this.communicationData.urgent_communication != '1'){
  371. this.communicationData.urgent_communication = '0';
  372. }
  373. this.http.post(this.authSer.pathApi + '/maintenance_handle_communication', this.communicationData).subscribe(
  374. (responce) => {
  375. if(responce['status'] == 'some adminstrations didnt closed yet') {
  376. this.toastr.warning('بعض الإدارات لم تقم بإغلاق البلاغ بعد !');
  377. } else {
  378. this.toastr.success('تم الحفظ بنجاح');
  379. this.location.back();
  380. }
  381. },
  382. (error) => {
  383. console.log(error);
  384. this.checkSaveClick = false;
  385. this.toastr.error('خطأ في الحفظ حاول لاحقاً');
  386. }
  387. );
  388. }
  389. }
  390. }