import { ActivatedRoute, Params } from '@angular/router'; import { Location } from '@angular/common'; import { DashboardService } from './../../../shared/dashboard.service'; import { AuthServiceService } from './../../../shared/auth-service.service'; import { FormGroup, NgForm } from '@angular/forms'; import { Component, OnInit, ViewChild } from '@angular/core'; import { UserService } from '../../../shared/user.service'; import { ToastrService } from 'ngx-toastr'; import { NgxSpinnerService } from 'ngx-spinner'; @Component({ selector: 'app-supervisor-add', templateUrl: './supervisor-add.component.html', styleUrls: ['./supervisor-add.component.css'] }) export class SupervisorAddComponent implements OnInit { constructor(private userSer: UserService, private dashboardService: DashboardService, private toastr: ToastrService, private location: Location, private spinner: NgxSpinnerService, private route: ActivatedRoute, public authSer: AuthServiceService) { } formData: any; checkSaveClick: boolean = false; superDeaprt = { email: '', phone: '', switch_phone: '', department_id: '', supervisor_id: '', } departments = []; //all departments list view department = []; //department for edit supervisorList = []; departmentData = []; supervisorId: number; departmentName: string = ''; checkSaveDisabled: boolean = false; typeLink: string = ''; 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.internalHeader = false; //get user departments this.userSer.getTrainningServiceDepartments().subscribe( (responce) => { console.log(responce); this.departments = responce['departments']; if(this.departments.length == 0) { this.toastr.warning('جميع الأقسام لديها مشرفين !'); } }, (error) => { console.log(error); } ); this.route.params.subscribe( (params: Params) => { this.supervisorId = +params['superEditId']; } ); if(this.supervisorId) { this.spinner.show(); this.typeLink = 'تعديل'; this.dashboardService.getItemData(this.supervisorId, 'supervisor').subscribe( (responce) =>{ console.log('get data of one', responce); this.department = responce['department']; this.superDeaprt.email = this.department['email']; this.superDeaprt.phone = this.department['phone']; this.superDeaprt.switch_phone = this.department['switch_phone']; this.superDeaprt.department_id = this.department['id']; this.superDeaprt.supervisor_id = this.department['supervisor_id']; this.getSupervisors(this.department['id']); this.departmentName = this.department['name']; console.log(this.superDeaprt.department_id); this.spinner.hide(); }, (error) => { console.log(error); } ) } else { this.typeLink = 'إضافه'; } } getSupervisors(departmentId: number) { this.userSer.getSupervisorsList(departmentId).subscribe( (responce) => { console.log(responce); this.supervisorList = responce['users']; if(this.supervisorList.length == 0) { this.toastr.warning(' القسم ليس لديه مشرفين !'); } }, (error) => { console.log(error); } ); } changeDepartment(event) { console.log(event.target.value); this.getSupervisors(event.target.value); this.userSer.getDepartmentData(event.target.value).subscribe( (responce) => { this.departmentData = responce['department']; this.superDeaprt.email = this.departmentData['email']; this.superDeaprt.phone = this.departmentData['phone']; this.superDeaprt.switch_phone = this.departmentData['switch_phone']; this.superDeaprt.supervisor_id = this.departmentData['supervisor_id']; console.log(this.superDeaprt.supervisor_id); console.log(this.departmentData); }, (error) => { console.log(error); } ) } onSubmitted(form: NgForm) { this.formData = form.value; this.checkSaveClick = true; if(this.supervisorId){ this.formData['department_id'] = this.superDeaprt.department_id; } console.log(this.formData); this.dashboardService.addItem(this.formData, 'supervisor').subscribe( (responce) => { console.log(responce); this.checkSaveClick = false; if(this.supervisorId) { this.toastr.success('تمت التعديل بنجاح'); } else { this.toastr.success('تمت الاضافه بنجاح'); } this.location.back(); }, (error) => { console.log(error); this.checkSaveClick = false; this.toastr.error('خطأ في الحفظ !'); } ); } }