add-event.component.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { AuthServiceService } from './../../../shared/auth-service.service';
  2. import { Location } from '@angular/common';
  3. import { Component, OnInit, ViewChild } from '@angular/core';
  4. import { Router, ActivatedRoute, Params } from '@angular/router';
  5. import { UserService } from '../../../shared/user.service';
  6. import { ToastrService } from 'ngx-toastr';
  7. import { NgxSpinnerService } from 'ngx-spinner';
  8. import { FormGroup, NgForm } from '@angular/forms';
  9. import { DashboardService } from 'src/app/shared/dashboard.service';
  10. @Component({
  11. selector: 'app-add-event',
  12. templateUrl: './add-event.component.html',
  13. styleUrls: ['./add-event.component.css']
  14. })
  15. export class AddEventComponent implements OnInit {
  16. @ViewChild('f') eventForm: NgForm;
  17. typeMode: boolean = false;
  18. typeLink: string = '';
  19. eventId: number;
  20. bindingDateSplit;
  21. higriDateVal:string;
  22. checkSaveClick:boolean = false;
  23. event = {
  24. employee_name: '',
  25. employee_name_en: '',
  26. name: '',
  27. name_en: '',
  28. ranking: '',
  29. type: '',
  30. status: '',
  31. time: '',
  32. }
  33. constructor(private dashboard: DashboardService,
  34. private userService: UserService,
  35. private location: Location,
  36. private toastr: ToastrService,
  37. private authSer: AuthServiceService,
  38. private route: ActivatedRoute,
  39. private spineer: NgxSpinnerService) { }
  40. ngOnInit() {
  41. //show / hide notification search in header
  42. this.authSer.notificationLogin = true;
  43. this.authSer.showSearchHeader = false;
  44. this.authSer.showHeaderLogin = false;
  45. this.authSer.showHeaderDashBoard = true;
  46. this.authSer.showDashboardHeader = true;
  47. this.authSer.internalHeader = false;
  48. this.event.status = '1';
  49. this.route.params.subscribe(
  50. (params: Params) => {
  51. if(params['typeEventMode'] == 'edit') {
  52. this.typeMode = true;
  53. this.typeLink = 'تعديل';
  54. this.spineer.show();
  55. this.eventId = +params['editEventId'];
  56. this.dashboard.getItemData(this.eventId, "events").subscribe(
  57. (responce) => {
  58. console.log(responce);
  59. const eventData = responce['event'][0];
  60. console.log(eventData);
  61. this.event.name = eventData.name;
  62. this.event.employee_name = eventData.employee_name;
  63. this.event.employee_name_en = eventData.employee_name_en;
  64. this.event.name_en = eventData.name_en;
  65. this.event.ranking = eventData.ranking;
  66. this.event.type = eventData.type;
  67. this.event.status = eventData.status;
  68. const dateTime = eventData.event_time.split(' ');
  69. this.event.time = dateTime[1];
  70. const higriDate = dateTime[0].split('-');
  71. console.log('higrrrrrrrri', higriDate);
  72. this.higriDateVal = dateTime[0];
  73. this.bindingDateSplit = {
  74. 'year': parseInt(higriDate[0]),
  75. 'month': parseInt(higriDate[1]),
  76. 'day': parseInt(higriDate[2])
  77. };
  78. console.log('hhhhhhhhhhhhhhh' , this.bindingDateSplit);
  79. this.spineer.hide();
  80. },
  81. (error) => {
  82. console.log(error);
  83. }
  84. )
  85. } else {
  86. this.typeLink = 'إنشاء جديد';
  87. }
  88. }
  89. );
  90. }
  91. //get value date from child component
  92. public getDate(date: any):void {
  93. console.log( date);
  94. this.higriDateVal = date.year + '-' + date.month + '-' + date.day;
  95. console.log('higrii date', this.higriDateVal);
  96. }
  97. //submitted form
  98. onSubmitted() {
  99. this.checkSaveClick = true;
  100. console.log(this.eventForm.value);
  101. this.eventForm.value['date'] = this.higriDateVal;
  102. if(this.eventForm.value.time) {
  103. this.eventForm.value.event_time = this.eventForm.value.date + ' ' + this.eventForm.value.time;
  104. } else {
  105. this.eventForm.value.event_time = this.eventForm.value.date + this.event.time;
  106. }
  107. console.log(this.eventForm.value.event_time);
  108. delete this.eventForm.value.time;
  109. delete this.eventForm.value.date;
  110. console.log(this.eventForm.value);
  111. if(this.typeMode) {
  112. this.dashboard.editItem( this.eventId ,this.eventForm.value , "events").subscribe(
  113. (responce) => {
  114. console.log(responce);
  115. this.toastr.success('تمت التعديل بنجاح');
  116. this.location.back();
  117. this.checkSaveClick = false;
  118. },
  119. (error) => {
  120. console.log(error.error['status']);
  121. if(error.error.status == 'invalid event_time') {
  122. this.toastr.warning('يجب ان يكون الحدث في وقت سابق !');
  123. }else if(error.error['status'] == 'active status for only 4 records') {
  124. this.toastr.warning('لديك أربعه أحداث في الحاله فعال بالفعل ، قم بعدم تفعيل أو مسح أحدهم ');
  125. }else {
  126. this.toastr.error('حدث خطأ !');
  127. }
  128. console.log(error);
  129. this.checkSaveClick = false;
  130. }
  131. );
  132. } else {
  133. this.dashboard.addItem(this.eventForm.value, "events").subscribe(
  134. (responce) => {
  135. console.log(responce);
  136. this.toastr.success('تمت الاضافه بنجاح');
  137. this.location.back();
  138. this.checkSaveClick = false;
  139. },
  140. (error) => {
  141. console.log(error.error['status']);
  142. if(error.error.status == 'invalid event_time') {
  143. this.toastr.warning('يجب ان يكون الحدث في وقت سابق !');
  144. } else if(error.error['status'] == 'active status for only 4 records') {
  145. this.toastr.warning('لديك أربعه أحداث في الحاله فعال بالفعل ، قم بعدم تفعيل أو مسح أحدهم ');
  146. }else {
  147. this.toastr.error('حدث خطأ !');
  148. }
  149. console.log(error);
  150. this.checkSaveClick = false;
  151. }
  152. );
  153. }
  154. }
  155. }