living-service-add.component.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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-living-service-add',
  12. templateUrl: './living-service-add.component.html',
  13. styleUrls: ['./living-service-add.component.css']
  14. })
  15. export class LivingServiceAddComponent 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. locations =[];
  28. valueType: string = '';
  29. checkSaveclick:boolean = false;
  30. check : boolean = false;
  31. isEdit: boolean = false;
  32. editPageId: number;
  33. formData = {
  34. name: '',
  35. status: '1',
  36. }
  37. ngOnInit() {
  38. this.route.params.subscribe(
  39. (params: Params) => {
  40. this.editPageId = +params['listPageEditId'];
  41. console.log(this.editPageId);
  42. }
  43. );
  44. if (this.editPageId) {
  45. this.spinner.show();
  46. this.isEdit = true;
  47. this.typePage = 'تعديل';
  48. this.dashBoardSer.getItemData(this.editPageId, 'livingService' ).subscribe(
  49. res => {
  50. console.log('ssss', res);
  51. this.formData.name = res['covenant_category'].name;
  52. this.formData.status = res['covenant_category'].status;
  53. this.spinner.hide();
  54. },
  55. err => {
  56. console.log(err);
  57. }
  58. );
  59. }else {
  60. this.typePage = 'اضافة';
  61. }
  62. }
  63. onSubmitted(){
  64. console.log('HERE',this.definitionForm.value);
  65. if(this.editPageId) {
  66. this.dashBoardSer.editItem(this.editPageId, this.definitionForm.value, 'livingService').subscribe(
  67. res => {
  68. console.log(res);
  69. this.toastr.success('تم التعديل بنجاح');
  70. this.location.back();
  71. },
  72. err => {
  73. console.log(err);
  74. this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
  75. }
  76. )
  77. } else {
  78. this.dashBoardSer.addItem( this.definitionForm.value , 'livingService').subscribe(
  79. res => {
  80. console.log('ADD',res);
  81. this.toastr.success('تم الإضافه بنجاح');
  82. this.checkSaveclick = false;
  83. this.location.back();
  84. },
  85. err => {
  86. console.log(err);
  87. this.checkSaveclick = false;
  88. this.toastr.error('خطأ في الخادم ، حاول لاحقا');
  89. }
  90. );
  91. }
  92. }
  93. }