add-statistic.component.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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-statistic',
  12. templateUrl: './add-statistic.component.html',
  13. styleUrls: ['./add-statistic.component.css']
  14. })
  15. export class AddStatisticComponent 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. months = [];
  25. typeMode:boolean = false; //default false for create page
  26. typeLink: string;
  27. statisticId:number;
  28. checkDisabledSave:boolean = false;
  29. statistic = {
  30. name: '',
  31. name_en: '',
  32. value: '',
  33. month_id: '',
  34. year: '',
  35. status: 1,
  36. };
  37. ngOnInit() {
  38. //get adminstration
  39. this.userSer.getMonth().subscribe(
  40. (responce) => {
  41. console.log(responce);
  42. this.months = responce['months'];
  43. },
  44. (error) => {
  45. console.log(error)
  46. }
  47. );
  48. this.route.params.subscribe(
  49. (params: Params) => {
  50. console.log(params)
  51. if(params['typeStatistic'] == 'add'){
  52. this.typeMode = false;
  53. this.typeLink = 'إنشاء احصائية';
  54. } else if(params['typeStatistic'] == 'edit') {
  55. this.spinner.show();
  56. this.typeMode = true;
  57. this.statisticId = parseInt(params.statisticId);
  58. this.typeLink = 'تعديل احصائية';
  59. this.dashboardSer.getItemData(this.statisticId, 'statistic').subscribe(
  60. (responce) => {
  61. console.log(responce);
  62. this.statistic = responce['statistic'];
  63. console.log(this.statistic);
  64. this.spinner.hide();
  65. },
  66. (error) => {
  67. console.log(error);
  68. }
  69. );
  70. } else {
  71. this.toastr.warning('Catch Error Go To Home !');
  72. }
  73. }
  74. );
  75. }//ngOnInit
  76. onSubmitted() {
  77. this.checkDisabledSave = true;
  78. const dataFormEventBar = this.dataForm.value;
  79. console.log(dataFormEventBar);
  80. if(this.typeMode) {
  81. this.dashboardSer.editItem(dataFormEventBar, this.statisticId, 'statistic').subscribe(
  82. (responce) => {
  83. console.log(responce);
  84. this.toastr.success('تم التعديل بنجاح ');
  85. this.checkDisabledSave = false;
  86. this.location.back();
  87. },
  88. (error) => {
  89. console.log(error);
  90. this.checkDisabledSave = false;
  91. this.toastr.error(' خطأ في التعديل !');
  92. }
  93. );
  94. } else {
  95. this.dashboardSer.addItem(dataFormEventBar,'statistic').subscribe(
  96. (responce) => {
  97. this.toastr.success('تم الاضافه بنجاح');
  98. this.checkDisabledSave = false;
  99. console.log(responce);
  100. this.location.back();
  101. },
  102. (error) => {
  103. console.log(error);
  104. this.checkDisabledSave = false;
  105. this.toastr.error('خطأ في الاضافه');
  106. }
  107. );
  108. }
  109. }
  110. }