123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- import { AuthServiceService } from './../../../shared/auth-service.service';
- import { ActivatedRoute, Params } from '@angular/router';
- import { Subscription } from 'rxjs';
- import { Location } from '@angular/common';
- import { HospitalService } from './../../../shared/hospital.service';
- import { FormGroup,FormControl, Validators, FormArray, FormBuilder } from '@angular/forms';
- import { Component, OnInit, OnDestroy } from '@angular/core';
- import { NgxSpinnerService } from 'ngx-spinner';
- import { ToastrService } from 'ngx-toastr';
- import * as Quill from 'quill';
- @Component({
- selector: 'app-add-hospital',
- templateUrl: './add-hospital.component.html',
- styleUrls: ['./add-hospital.component.css']
- })
- export class AddHospitalComponent implements OnInit, OnDestroy {
- constructor(private formBuilder: FormBuilder,
- private hospitalService: HospitalService,
- private route: ActivatedRoute,
- private spinner: NgxSpinnerService,
- private toastr: ToastrService,
- private authSer: AuthServiceService,
- private location: Location) { }
- addHospitalForm: FormGroup;
- fields: FormArray;
- typeMode: boolean = false;
- checkSaveClick:boolean = false;
- typePageEdit: string = '';
- typeCreatePage: string = '';
- typeLink = 'إنشاء جديد';
- typeFirstLink: string;
- typeId: number;
- editSubscription: Subscription;
- typeCreateSubscription: Subscription;
- fieldsData = [];
-
- ngOnInit() {
- //show / hide notification search in header
- this.authSer.notificationLogin = true;
- this.authSer.showSearchHeader = false;
- this.authSer.showHeaderLogin = false;
- this.authSer.showHeaderDashBoard = true;
- this.authSer.showDashboardHeader = true;
- this.authSer.internalHeader = false;
-
- //init form data
- this.addHospitalForm = new FormGroup({
- name: new FormControl(null , Validators.required),
- name_en: new FormControl(null , Validators.required),
- fields: this.formBuilder.array([ this.createItem() ])
- });
- this.typeCreateSubscription = this.route.params.subscribe(
- (params: Params) => {
- this.typeCreatePage = params['typeAdd'];
- if(this.typeCreatePage == 'Hospital') {
- this.typeFirstLink = 'المستشفيات والمراكز';
- } else if(this.typeCreatePage == 'Management') {
- this.typeFirstLink = 'الإدارات';
- }
- }
- );
- //edit mode
- this.editSubscription = this.route.params.subscribe(
- (params: Params) => {
- if(params['typeHospitalMode'] == 'edithos') {
- this.typeLink = 'تعديل';
- this.typeFirstLink = 'المستشفيات والمراكز';
- this.addHospitalForm = new FormGroup({
- name: new FormControl(null , Validators.required),
- name_en: new FormControl(null , Validators.required),
- fields: this.formBuilder.array([])
- });
- this.spinner.show();
- this.typeMode = true;
- this.typePageEdit = 'edithos';
- this.typeId = params['editTypePageId'];
- this.hospitalService.getHospitalData(this.typeId, 'edithos').subscribe(
- (responce) => {
- console.log('responcce', responce);
- this.fieldsData = responce['hospital_center'].fields;
- console.log(this.fieldsData);
- this.addHospitalForm.patchValue({
- name: responce['hospital_center'].name,
- name_en: responce['hospital_center'].name_en,
- });
- this.fields = this.addHospitalForm.get('fields') as FormArray;
- for(let i = 0; i < this.fieldsData.length; i++) {
- this.fields.push(
- this.formBuilder.group({
- title: this.fieldsData[i].title,
- title_en: this.fieldsData[i].title_en,
- description: this.fieldsData[i].description,
- description_en: this.fieldsData[i].description_en,
- })
- )
- }
- this.spinner.hide();
- },
- (error) => {
- console.log(error);
- }
- );
- } else if (params['typeHospitalMode'] == 'editman') {
- this.typeLink = 'تعديل';
- this.typeFirstLink = 'الإدارات';
- this.addHospitalForm = new FormGroup({
- name: new FormControl(null , Validators.required),
- name_en: new FormControl(null , Validators.required),
- fields: this.formBuilder.array([])
- });
- this.spinner.show();
- this.typeMode = true;
- this.typePageEdit = "editman";
- this.typeId = params['editTypePageId'];
- this.hospitalService.getHospitalData(this.typeId, 'editman').subscribe(
- (responce) => {
- console.log('responcce', responce);
- this.fieldsData = responce['management'].fields;
- this.addHospitalForm.patchValue({
- name: responce['management'].name,
- name_en: responce['management'].name_en,
- });
- this.fields = this.addHospitalForm.get('fields') as FormArray;
- for(let i = 0; i < this.fieldsData.length; i++) {
- this.fields.push(
- this.formBuilder.group({
- title: this.fieldsData[i].title,
- title_en: this.fieldsData[i].title_en,
- description: this.fieldsData[i].description,
- description_en: this.fieldsData[i].description_en,
- })
- )
- }
- this.spinner.hide();
- },
- (error) => {
- console.log(error);
- }
- );
- }
- }
- );
- };
- //to make at least on element in form array
- createItem(): FormGroup {
- return this.formBuilder.group({
- title: '',
- title_en: '',
- description: '',
- description_en: '',
- });
- };
-
- //add more title
- onAddTitle() {
- this.fields = this.addHospitalForm.get('fields') as FormArray;
- this.fields.push(this.createItem());
- }
- //remove from array form
- removeTitle(index: number) {
- this.fields = this.addHospitalForm.get('fields') as FormArray;
- this.fields.removeAt(index);
- }
- //submitted form
- onSubmitted() {
- this.checkSaveClick = true;
- console.log(this.addHospitalForm.value);
- const formHospitalData = this.addHospitalForm.value;
- console.log(formHospitalData.fields[0].title);
- if(this.typeCreatePage == 'Hospital') {
- if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
- this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
- this.checkSaveClick = false;
- } else {
- this.hospitalService.addHospital(formHospitalData, 'hospital').subscribe(
- (responce) => {
- this.checkSaveClick = false;
- console.log(responce);
- this.toastr.success('تم الاضافه بنجاح');
- this.location.back();
- },
- (error) => {
- this.checkSaveClick = false;
- this.toastr.error('خطأ في الإنشاء');
- console.log(error);
- }
- );
- }
- } else if(this.typeCreatePage == 'Management') {
- if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
- this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
- this.checkSaveClick = false;
- } else {
- this.hospitalService.addHospital(formHospitalData, 'management').subscribe(
- (responce) => {
- this.checkSaveClick = false;
- console.log(responce);
- this.toastr.success('تم الاضافه بنجاح');
- this.location.back();
- },
- (error) => {
- this.checkSaveClick = false;
- this.toastr.error('خطأ في الاضافه');
- console.log(error);
- }
- );
- }
- }
- if(this.typeMode) {
- if(this.typePageEdit == 'edithos'){
- if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
- this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
- this.checkSaveClick = false;
- } else {
- this.hospitalService.editHospital(this.typeId, formHospitalData, 'hospital').subscribe(
- (responce) => {
- this.checkSaveClick = false;
- console.log(responce);
- this.toastr.success('تم التعديل بنجاح ');
- }, (error) => {
- this.checkSaveClick = false;
- console.log(error);
- this.toastr.error('فشل التعديل !');
- }
- );
- }
- } else if(this.typePageEdit == 'editman'){
- if(formHospitalData.fields[0].title == '' || formHospitalData.fields[0].description == '') {
- this.toastr.warning('أدخل عنوان ووصف واحد علي الاقل !');
- this.checkSaveClick = false;
- } else {
- this.hospitalService.editHospital(this.typeId, formHospitalData, 'management').subscribe(
- (responce) => {
- this.checkSaveClick = false;
- console.log(responce);
- this.toastr.success('تم التعديل بنجاح ');
- }, (error) => {
- this.checkSaveClick = false;
- console.log(error);
- this.toastr.error('فشل التعديل !');
- }
- )
- }
- }
- }
- };
- ngOnDestroy() {
- this.editSubscription.unsubscribe();
- }
- }
|