add-hospital.component.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { AuthServiceService } from './../../../shared/auth-service.service';
  2. import { ActivatedRoute, Params } from '@angular/router';
  3. import { Subscription } from 'rxjs';
  4. import { Location } from '@angular/common';
  5. import { HospitalService } from './../../../shared/hospital.service';
  6. import { FormGroup,FormControl, Validators, FormArray, FormBuilder } from '@angular/forms';
  7. import { Component, OnInit, OnDestroy } from '@angular/core';
  8. import { NgxSpinnerService } from 'ngx-spinner';
  9. import { ToastrService } from 'ngx-toastr';
  10. import * as Quill from 'quill';
  11. @Component({
  12. selector: 'app-add-hospital',
  13. templateUrl: './add-hospital.component.html',
  14. styleUrls: ['./add-hospital.component.css']
  15. })
  16. export class AddHospitalComponent implements OnInit, OnDestroy {
  17. constructor(private formBuilder: FormBuilder,
  18. private hospitalService: HospitalService,
  19. private route: ActivatedRoute,
  20. private spinner: NgxSpinnerService,
  21. private toastr: ToastrService,
  22. private authSer: AuthServiceService,
  23. private location: Location) { }
  24. addHospitalForm: FormGroup;
  25. fields: FormArray;
  26. typeMode: boolean = false;
  27. checkSaveClick:boolean = false;
  28. typePageEdit: string = '';
  29. typeCreatePage: string = '';
  30. typeLink = 'إنشاء جديد';
  31. typeFirstLink: string;
  32. typeId: number;
  33. editSubscription: Subscription;
  34. typeCreateSubscription: Subscription;
  35. fieldsData = [];
  36. ngOnInit() {
  37. //show / hide notification search in header
  38. this.authSer.notificationLogin = true;
  39. this.authSer.showSearchHeader = false;
  40. this.authSer.showHeaderLogin = false;
  41. this.authSer.showHeaderDashBoard = true;
  42. this.authSer.showDashboardHeader = true;
  43. this.authSer.internalHeader = false;
  44. //init form data
  45. this.addHospitalForm = new FormGroup({
  46. name: new FormControl(null , Validators.required),
  47. name_en: new FormControl(null , Validators.required),
  48. fields: this.formBuilder.array([ this.createItem() ])
  49. });
  50. this.typeCreateSubscription = this.route.params.subscribe(
  51. (params: Params) => {
  52. this.typeCreatePage = params['typeAdd'];
  53. if(this.typeCreatePage == 'Hospital') {
  54. this.typeFirstLink = 'المستشفيات والمراكز';
  55. } else if(this.typeCreatePage == 'Management') {
  56. this.typeFirstLink = 'الإدارات';
  57. }
  58. }
  59. );
  60. //edit mode
  61. this.editSubscription = this.route.params.subscribe(
  62. (params: Params) => {
  63. if(params['typeHospitalMode'] == 'edithos') {
  64. this.typeLink = 'تعديل';
  65. this.typeFirstLink = 'المستشفيات والمراكز';
  66. this.addHospitalForm = new FormGroup({
  67. name: new FormControl(null , Validators.required),
  68. name_en: new FormControl(null , Validators.required),
  69. fields: this.formBuilder.array([])
  70. });
  71. this.spinner.show();
  72. this.typeMode = true;
  73. this.typePageEdit = 'edithos';
  74. this.typeId = params['editTypePageId'];
  75. this.hospitalService.getHospitalData(this.typeId, 'edithos').subscribe(
  76. (responce) => {
  77. console.log('responcce', responce);
  78. this.fieldsData = responce['hospital_center'].fields;
  79. console.log(this.fieldsData);
  80. this.addHospitalForm.patchValue({
  81. name: responce['hospital_center'].name,
  82. name_en: responce['hospital_center'].name_en,
  83. });
  84. this.fields = this.addHospitalForm.get('fields') as FormArray;
  85. for(let i = 0; i < this.fieldsData.length; i++) {
  86. this.fields.push(
  87. this.formBuilder.group({
  88. title: this.fieldsData[i].title,
  89. title_en: this.fieldsData[i].title_en,
  90. description: this.fieldsData[i].description,
  91. description_en: this.fieldsData[i].description_en,
  92. })
  93. )
  94. }
  95. this.spinner.hide();
  96. },
  97. (error) => {
  98. console.log(error);
  99. }
  100. );
  101. } else if (params['typeHospitalMode'] == 'editman') {
  102. this.typeLink = 'تعديل';
  103. this.typeFirstLink = 'الإدارات';
  104. this.addHospitalForm = new FormGroup({
  105. name: new FormControl(null , Validators.required),
  106. name_en: new FormControl(null , Validators.required),
  107. fields: this.formBuilder.array([])
  108. });
  109. this.spinner.show();
  110. this.typeMode = true;
  111. this.typePageEdit = "editman";
  112. this.typeId = params['editTypePageId'];
  113. this.hospitalService.getHospitalData(this.typeId, 'editman').subscribe(
  114. (responce) => {
  115. console.log('responcce', responce);
  116. this.fieldsData = responce['management'].fields;
  117. this.addHospitalForm.patchValue({
  118. name: responce['management'].name,
  119. name_en: responce['management'].name_en,
  120. });
  121. this.fields = this.addHospitalForm.get('fields') as FormArray;
  122. for(let i = 0; i < this.fieldsData.length; i++) {
  123. this.fields.push(
  124. this.formBuilder.group({
  125. title: this.fieldsData[i].title,
  126. title_en: this.fieldsData[i].title_en,
  127. description: this.fieldsData[i].description,
  128. description_en: this.fieldsData[i].description_en,
  129. })
  130. )
  131. }
  132. this.spinner.hide();
  133. },
  134. (error) => {
  135. console.log(error);
  136. }
  137. );
  138. }
  139. }
  140. );
  141. };
  142. //to make at least on element in form array
  143. createItem(): FormGroup {
  144. return this.formBuilder.group({
  145. title: '',
  146. title_en: '',
  147. description: '',
  148. description_en: '',
  149. });
  150. };
  151. //add more title
  152. onAddTitle() {
  153. this.fields = this.addHospitalForm.get('fields') as FormArray;
  154. this.fields.push(this.createItem());
  155. }
  156. //remove from array form
  157. removeTitle(index: number) {
  158. this.fields = this.addHospitalForm.get('fields') as FormArray;
  159. this.fields.removeAt(index);
  160. }
  161. //submitted form
  162. onSubmitted() {
  163. this.checkSaveClick = true;
  164. console.log(this.addHospitalForm.value);
  165. const formHospitalData = this.addHospitalForm.value;
  166. console.log(formHospitalData.fields[0].title);
  167. if(this.typeCreatePage == 'Hospital') {
  168. if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
  169. this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
  170. this.checkSaveClick = false;
  171. } else {
  172. this.hospitalService.addHospital(formHospitalData, 'hospital').subscribe(
  173. (responce) => {
  174. this.checkSaveClick = false;
  175. console.log(responce);
  176. this.toastr.success('تم الاضافه بنجاح');
  177. this.location.back();
  178. },
  179. (error) => {
  180. this.checkSaveClick = false;
  181. this.toastr.error('خطأ في الإنشاء');
  182. console.log(error);
  183. }
  184. );
  185. }
  186. } else if(this.typeCreatePage == 'Management') {
  187. if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
  188. this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
  189. this.checkSaveClick = false;
  190. } else {
  191. this.hospitalService.addHospital(formHospitalData, 'management').subscribe(
  192. (responce) => {
  193. this.checkSaveClick = false;
  194. console.log(responce);
  195. this.toastr.success('تم الاضافه بنجاح');
  196. this.location.back();
  197. },
  198. (error) => {
  199. this.checkSaveClick = false;
  200. this.toastr.error('خطأ في الاضافه');
  201. console.log(error);
  202. }
  203. );
  204. }
  205. }
  206. if(this.typeMode) {
  207. if(this.typePageEdit == 'edithos'){
  208. if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
  209. this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
  210. this.checkSaveClick = false;
  211. } else {
  212. this.hospitalService.editHospital(this.typeId, formHospitalData, 'hospital').subscribe(
  213. (responce) => {
  214. this.checkSaveClick = false;
  215. console.log(responce);
  216. this.toastr.success('تم التعديل بنجاح ');
  217. }, (error) => {
  218. this.checkSaveClick = false;
  219. console.log(error);
  220. this.toastr.error('فشل التعديل !');
  221. }
  222. );
  223. }
  224. } else if(this.typePageEdit == 'editman'){
  225. if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
  226. this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
  227. this.checkSaveClick = false;
  228. } else {
  229. this.hospitalService.editHospital(this.typeId, formHospitalData, 'management').subscribe(
  230. (responce) => {
  231. this.checkSaveClick = false;
  232. console.log(responce);
  233. this.toastr.success('تم التعديل بنجاح ');
  234. }, (error) => {
  235. this.checkSaveClick = false;
  236. console.log(error);
  237. this.toastr.error('فشل التعديل !');
  238. }
  239. )
  240. }
  241. }
  242. }
  243. };
  244. ngOnDestroy() {
  245. this.editSubscription.unsubscribe();
  246. }
  247. }