add-department.component.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { DashboardService } from './../../../shared/dashboard.service';
  2. import { Location } from '@angular/common';
  3. import { UserService } from './../../../shared/user.service';
  4. import { AuthServiceService } from './../../../shared/auth-service.service';
  5. import { Component, OnInit, ViewChild } from '@angular/core';
  6. import { NgxSpinnerService } from 'ngx-spinner';
  7. import { ToastrService } from 'ngx-toastr';
  8. import { Router, ActivatedRoute, Params } from '@angular/router';
  9. @Component({
  10. selector: 'app-add-department',
  11. templateUrl: './add-department.component.html',
  12. styleUrls: ['./add-department.component.css']
  13. })
  14. export class AddDepartmentComponent implements OnInit {
  15. constructor(private authService: AuthServiceService,
  16. private userService: UserService,
  17. private dashboardSer: DashboardService,
  18. private spinner: NgxSpinnerService,
  19. private toastr: ToastrService,
  20. private router: Router,
  21. private authSer: AuthServiceService,
  22. private route: ActivatedRoute,
  23. private location: Location) {
  24. this.spinner.show();
  25. }
  26. checkEditMode: boolean = false;
  27. departId: number;
  28. admin = {
  29. name: '',
  30. description: '',
  31. email: '',
  32. phone: '',
  33. switch_phone: '',
  34. status: '1',
  35. }
  36. typeLink: string = '';
  37. checkSaveDisabled: boolean = false;
  38. @ViewChild('f') addDepertmentForm;
  39. ngOnInit() {
  40. this.userService.getUserDataProfile();
  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. //show / hide notification search in header
  47. this.authSer.notificationLogin = true;
  48. this.authSer.showSearchHeader = false;
  49. this.authSer.showHeaderLogin = false;
  50. this.authSer.showHeaderDashBoard = true;
  51. this.authSer.showDashboardHeader = true;
  52. this.authSer.internalHeader = false;
  53. this.route.params.subscribe(
  54. (params: Params) => {
  55. if(params['typeAdminMode'] == 'edit') {
  56. this.typeLink = 'تعديل';
  57. this.checkEditMode = true;
  58. this.departId = params['editAdminId'];
  59. this.dashboardSer.getItemData(this.departId, 'department').subscribe(
  60. (responce) => {
  61. console.log(responce);
  62. this.admin.name = responce['adminstration'].name;
  63. this.admin.email = responce['adminstration'].email;
  64. this.admin.phone = responce['adminstration'].phone;
  65. this.admin.switch_phone = responce['adminstration'].switch_phone;
  66. this.admin.status = responce['adminstration'].status;
  67. this.admin.description = responce['adminstration'].description;
  68. this.spinner.hide();
  69. },
  70. (error) => {
  71. console.log(error);
  72. }
  73. )
  74. } else {
  75. this.typeLink = 'إنشاء جديد';
  76. this.spinner.hide();
  77. }
  78. }
  79. )
  80. }
  81. //add function
  82. onSubmitted() {
  83. this.checkSaveDisabled = true;
  84. console.log(this.addDepertmentForm.value);
  85. if(this.checkEditMode) {
  86. this.dashboardSer.editItem( this.departId, this.addDepertmentForm.value , 'department').subscribe(
  87. (responce) => {
  88. this.checkSaveDisabled = false;
  89. console.log(responce);
  90. this.toastr.success('تم التعديل');
  91. this.location.back();
  92. },
  93. (error) => {
  94. this.toastr.error('خطأ تعديل');
  95. this.checkSaveDisabled = false;
  96. console.log(error);
  97. }
  98. );
  99. } else {
  100. this.dashboardSer.addItem(this.addDepertmentForm.value, 'department').subscribe(
  101. (responce) => {
  102. this.checkSaveDisabled= false;
  103. console.log(responce);
  104. this.toastr.success('تم الاضافه بنجاح');
  105. this.location.back();
  106. },
  107. (error) => {
  108. this.checkSaveDisabled= false;
  109. console.log(error);
  110. this.toastr.error('خطأ الاضافه !');
  111. }
  112. );
  113. }
  114. }
  115. }