follow-communications-add.component.ts 13 KB

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