definition-of-covenants-add.component.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { HttpClient } from '@angular/common/http';
  2. import { NgxSpinnerService } from 'ngx-spinner';
  3. import { ActivatedRoute, Params } from '@angular/router';
  4. import { Location } from '@angular/common';
  5. import { AuthServiceService } from './../../../shared/auth-service.service';
  6. import { ToastrService } from 'ngx-toastr';
  7. import { DashboardService } from './../../../shared/dashboard.service';
  8. import { Component, OnInit, ViewChild } from '@angular/core';
  9. import { NgForm } from '@angular/forms';
  10. @Component({
  11. selector: 'app-definition-of-covenants-add',
  12. templateUrl: './definition-of-covenants-add.component.html',
  13. styleUrls: ['./definition-of-covenants-add.component.css']
  14. })
  15. export class DefinitionOfCovenantsAddComponent implements OnInit {
  16. constructor(
  17. private dashBoardSer: DashboardService,
  18. private toastr: ToastrService,
  19. public authSer: AuthServiceService,
  20. private route: ActivatedRoute,
  21. private spinner: NgxSpinnerService,
  22. private location: Location,
  23. private http: HttpClient
  24. ) { }
  25. @ViewChild('f') definitionForm : NgForm;
  26. typePage: string = '';
  27. valueType: string = '';
  28. editPageId: number;
  29. locations =[];
  30. categories = [];
  31. checkSaveclick:boolean = false;
  32. check : boolean = false;
  33. isEdit: boolean = false;
  34. disable: boolean = false;
  35. formData = {
  36. name: '',
  37. category_id:'',
  38. amount:'',
  39. status: '1',
  40. }
  41. ngOnInit() {
  42. this.route.params.subscribe(
  43. (params: Params) => {
  44. this.editPageId = +params['listPageEditId'];;
  45. console.log(this.editPageId);
  46. }
  47. );
  48. this.http.get(this.authSer.pathApi + '/get_covenant_categories_list')
  49. .subscribe(
  50. res => {
  51. console.log('gg', res);
  52. this.categories = res[('covenant_categories')];
  53. },
  54. err =>{
  55. console.log(err);
  56. }
  57. );
  58. if (this.editPageId) {
  59. this.spinner.show();
  60. this.isEdit = true;
  61. this.typePage = 'تعديل';
  62. this.dashBoardSer.getItemData(this.editPageId, 'definitionOfCovenant' ).subscribe(
  63. res => {
  64. console.log('ssss', res);
  65. this.formData.name = res['covenant'].name;
  66. this.formData.category_id = res[('covenant')].category_id;
  67. this.formData.amount = res[('covenant')].amount;
  68. this.formData.status = res['covenant'].status;
  69. this.spinner.hide();
  70. },
  71. err => {
  72. console.log(err);
  73. }
  74. );
  75. }else {
  76. this.typePage = 'اضافة';
  77. }
  78. }
  79. onSubmitted(){
  80. this.disable = true;
  81. console.log('HERE',this.definitionForm.value);
  82. if(this.editPageId) {
  83. this.dashBoardSer.editItem(this.editPageId, this.definitionForm.value, 'definitionOfCovenant').subscribe(
  84. res => {
  85. console.log(res);
  86. this.disable = false;
  87. this.toastr.success('تم التعديل بنجاح');
  88. this.location.back();
  89. this.disable = false;
  90. },
  91. err => {
  92. console.log(err);
  93. this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
  94. }
  95. )
  96. } else {
  97. this.dashBoardSer.addItem( this.definitionForm.value , 'definitionOfCovenant').subscribe(
  98. res => {
  99. console.log('ADD',res);
  100. this.disable = false;
  101. this.toastr.success('تم الإضافه بنجاح');
  102. this.checkSaveclick = false;
  103. this.location.back();
  104. },
  105. err => {
  106. console.log(err);
  107. this.checkSaveclick = false;
  108. this.toastr.error('خطأ في الخادم ، حاول لاحقا');
  109. this.disable = false;
  110. }
  111. );
  112. }
  113. }
  114. }