add-bar-event.component.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { DashboardService } from './../../../shared/dashboard.service';
  2. import { AuthServiceService } from './../../../shared/auth-service.service';
  3. import { UserService } from './../../../shared/user.service';
  4. import { ActivatedRoute, Params } from '@angular/router';
  5. import { ToastrService } from 'ngx-toastr';
  6. import { Location } from '@angular/common';
  7. import { Component, OnInit, ViewChild } from '@angular/core';
  8. import { NgForm } from '@angular/forms';
  9. import { NgxSpinnerService } from 'ngx-spinner';
  10. @Component({
  11. selector: 'app-add-bar-event',
  12. templateUrl: './add-bar-event.component.html',
  13. styleUrls: ['./add-bar-event.component.css']
  14. })
  15. export class AddBarEventComponent implements OnInit {
  16. @ViewChild('f') dataForm: NgForm;
  17. constructor(private userSer:UserService,
  18. private authSer:AuthServiceService,
  19. private toastr: ToastrService,
  20. private spinner: NgxSpinnerService,
  21. private dashboardSer: DashboardService,
  22. private location: Location,
  23. private route:ActivatedRoute) { }
  24. typeMode:boolean = false; //default false for create page
  25. typeLink: string;
  26. barEventId:number;
  27. checkDisabledSave:boolean = false;
  28. barEvent = {
  29. name: '',
  30. name_en: '',
  31. description: '',
  32. description_en: '',
  33. status: 1,
  34. };
  35. higriDateVal = "";
  36. bindingDateSplit:any;
  37. ngOnInit() {
  38. this.route.params.subscribe(
  39. (params: Params) => {
  40. if(params['typeEventBar'] == 'add'){
  41. this.typeMode = false;
  42. this.typeLink = 'إنشاء حدث';
  43. } else if(params['typeEventBar'] == 'edit') {
  44. this.spinner.show();
  45. this.typeMode = true;
  46. this.barEventId = parseInt(localStorage.getItem('editbarEventIdStorage'));
  47. //alert(this.barEventId);
  48. this.typeLink = 'تعديل حدث';
  49. this.dashboardSer.getItemData(this.barEventId, 'barEvent').subscribe(
  50. (responce) => {
  51. console.log(responce);
  52. this.barEvent = responce['event'];
  53. console.log(this.barEvent);
  54. const date = responce['event'].event_time.split('-');
  55. this.bindingDateSplit = {
  56. year: parseInt(date[0]),
  57. month: parseInt(date[1]),
  58. day: parseInt(date[2])
  59. };
  60. this.higriDateVal = this.bindingDateSplit.year + '-' + this.bindingDateSplit.month + '-' + this.bindingDateSplit.day;
  61. this.spinner.hide();
  62. },
  63. (error) => {
  64. console.log(error);
  65. }
  66. );
  67. } else {
  68. this.toastr.warning('Catch Error Go To Home !');
  69. }
  70. }
  71. );
  72. }//ngOnInit
  73. //get value date from child component
  74. public getDate(date: any):void {
  75. console.log( date);
  76. this.higriDateVal = date.year + '-' + date.month + '-' + date.day;
  77. console.log('higrii date', this.higriDateVal);
  78. }
  79. onSubmitted() {
  80. this.checkDisabledSave = true;
  81. const dataFormEventBar = this.dataForm.value;
  82. dataFormEventBar['event_time'] = this.higriDateVal;
  83. console.log(dataFormEventBar);
  84. if(this.typeMode) {
  85. this.dashboardSer.editItem(dataFormEventBar, this.barEventId, 'barEvent').subscribe(
  86. (responce) => {
  87. console.log(responce);
  88. this.toastr.success('تم التعديل بنجاح ');
  89. this.checkDisabledSave = false;
  90. this.location.back();
  91. },
  92. (error) => {
  93. console.log(error);
  94. this.checkDisabledSave = false;
  95. this.toastr.error(' خطأ في التعديل !');
  96. }
  97. );
  98. } else {
  99. this.dashboardSer.addItem(dataFormEventBar,'barEvent').subscribe(
  100. (responce) => {
  101. this.toastr.success('تم الاضافه بنجاح');
  102. this.checkDisabledSave = false;
  103. console.log(responce);
  104. this.location.back();
  105. },
  106. (error) => {
  107. console.log(error);
  108. this.checkDisabledSave = false;
  109. this.toastr.error('خطأ في الاضافه');
  110. }
  111. );
  112. }
  113. }
  114. }