supervisor-add.component.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { ActivatedRoute, Params } from '@angular/router';
  2. import { Location } from '@angular/common';
  3. import { DashboardService } from './../../../shared/dashboard.service';
  4. import { AuthServiceService } from './../../../shared/auth-service.service';
  5. import { FormGroup, NgForm } from '@angular/forms';
  6. import { Component, OnInit, ViewChild } from '@angular/core';
  7. import { UserService } from '../../../shared/user.service';
  8. import { ToastrService } from 'ngx-toastr';
  9. import { NgxSpinnerService } from 'ngx-spinner';
  10. @Component({
  11. selector: 'app-supervisor-add',
  12. templateUrl: './supervisor-add.component.html',
  13. styleUrls: ['./supervisor-add.component.css']
  14. })
  15. export class SupervisorAddComponent implements OnInit {
  16. constructor(private userSer: UserService,
  17. private dashboardService: DashboardService,
  18. private toastr: ToastrService,
  19. private location: Location,
  20. private spinner: NgxSpinnerService,
  21. private route: ActivatedRoute,
  22. public authSer: AuthServiceService) { }
  23. formData: any;
  24. checkSaveClick: boolean = false;
  25. superDeaprt = {
  26. email: '',
  27. phone: '',
  28. switch_phone: '',
  29. department_id: '',
  30. supervisor_id: '',
  31. }
  32. departments = []; //all departments list view
  33. department = []; //department for edit
  34. supervisorList = [];
  35. departmentData = [];
  36. supervisorId: number;
  37. departmentName: string = '';
  38. checkSaveDisabled: boolean = false;
  39. typeLink: string = '';
  40. ngOnInit() {
  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. this.authSer.internalHeader = false;
  47. //get user departments
  48. this.userSer.getTrainningServiceDepartments().subscribe(
  49. (responce) => {
  50. console.log(responce);
  51. this.departments = responce['departments'];
  52. if(this.departments.length == 0) {
  53. this.toastr.warning('جميع الأقسام لديها مشرفين !');
  54. }
  55. },
  56. (error) => {
  57. console.log(error);
  58. }
  59. );
  60. this.route.params.subscribe(
  61. (params: Params) => {
  62. this.supervisorId = +params['superEditId'];
  63. }
  64. );
  65. if(this.supervisorId) {
  66. this.spinner.show();
  67. this.typeLink = 'تعديل';
  68. this.dashboardService.getItemData(this.supervisorId, 'supervisor').subscribe(
  69. (responce) =>{
  70. console.log('get data of one', responce);
  71. this.department = responce['department'];
  72. this.superDeaprt.email = this.department['email'];
  73. this.superDeaprt.phone = this.department['phone'];
  74. this.superDeaprt.switch_phone = this.department['switch_phone'];
  75. this.superDeaprt.department_id = this.department['id'];
  76. this.superDeaprt.supervisor_id = this.department['supervisor_id'];
  77. this.getSupervisors(this.department['id']);
  78. this.departmentName = this.department['name'];
  79. console.log(this.superDeaprt.department_id);
  80. this.spinner.hide();
  81. },
  82. (error) => {
  83. console.log(error);
  84. }
  85. )
  86. } else {
  87. this.typeLink = 'إضافه';
  88. }
  89. }
  90. getSupervisors(departmentId: number) {
  91. this.userSer.getSupervisorsList(departmentId).subscribe(
  92. (responce) => {
  93. console.log(responce);
  94. this.supervisorList = responce['users'];
  95. if(this.supervisorList.length == 0) {
  96. this.toastr.warning(' القسم ليس لديه مشرفين !');
  97. }
  98. },
  99. (error) => {
  100. console.log(error);
  101. }
  102. );
  103. }
  104. changeDepartment(event) {
  105. console.log(event.target.value);
  106. this.getSupervisors(event.target.value);
  107. this.userSer.getDepartmentData(event.target.value).subscribe(
  108. (responce) => {
  109. this.departmentData = responce['department'];
  110. this.superDeaprt.email = this.departmentData['email'];
  111. this.superDeaprt.phone = this.departmentData['phone'];
  112. this.superDeaprt.switch_phone = this.departmentData['switch_phone'];
  113. this.superDeaprt.supervisor_id = this.departmentData['supervisor_id'];
  114. console.log(this.superDeaprt.supervisor_id);
  115. console.log(this.departmentData);
  116. },
  117. (error) => {
  118. console.log(error);
  119. }
  120. )
  121. }
  122. onSubmitted(form: NgForm) {
  123. this.formData = form.value;
  124. this.checkSaveClick = true;
  125. if(this.supervisorId){
  126. this.formData['department_id'] = this.superDeaprt.department_id;
  127. }
  128. console.log(this.formData);
  129. this.dashboardService.addItem(this.formData, 'supervisor').subscribe(
  130. (responce) => {
  131. console.log(responce);
  132. this.checkSaveClick = false;
  133. if(this.supervisorId) {
  134. this.toastr.success('تمت التعديل بنجاح');
  135. } else {
  136. this.toastr.success('تمت الاضافه بنجاح');
  137. }
  138. this.location.back();
  139. },
  140. (error) => {
  141. console.log(error);
  142. this.checkSaveClick = false;
  143. this.toastr.error('خطأ في الحفظ !');
  144. }
  145. );
  146. }
  147. }