import { Component, OnInit } from '@angular/core'; import { ApplicationMaster, ApplicationProfileViewModel } from '../../application-fts/application-fts'; import { DropDownsHR } from '../../../_helpers/dropdowns.class'; import { RootService } from '../../../_services/root.service'; import { RiBranchService } from '../ri-branch.service'; import { AuthenticationService } from '../../../_services/authentication.service'; @Component({ selector: 'app-add-edit', templateUrl: './add-edit.component.html', styles: [] }) export class AddEditComponent implements OnInit { public loading: boolean = true; public application: ApplicationMaster = new ApplicationMaster(); public previewApplication: number = 2; public dropDowns: DropDownsHR = new DropDownsHR(); public cnicMask: string = "00000-0000000-0"; public mobileMask: string = "0000-0000000"; public officers: any[] = []; public selectedFiltersModel: any = {}; public savingApplication = false; constructor(private _rootService: RootService, private _riService: RiBranchService, private _authenticationService: AuthenticationService) { } private loadDropdownValues = () => { this.getDivisions('0'); this.getDistricts('0'); this.getTehsils('0'); this.getPandSOfficers('section'); } ngOnInit() { this.loadDropdownValues(); } onSubmit(value) { this.savingApplication = true; this._riService.submitApplication(this.application).subscribe((response: any) => { if (response.application) { this.application.Id = response.application.Id; if (response.barCode) { this.application.TrackingNumber = response.application.TrackingNumber; let barcode = response.barCode; this.application.barcode = barcode; //Proocess Further window.scroll(0, 0); } } this.savingApplication = false; }, err => { this.handleError(err); } ); } public searchProfile = () => { if (!this.application.CNIC) { this.mapProfileToApplicant(null); return; } this._riService.searchProfile(this.application.CNIC).subscribe((data: any) => { if (data == 'Invalid') { this.mapProfileToApplicant(null); } if (data) { this.mapProfileToApplicant(data); } }); } public dropdownValueChanged = (value, filter) => { if (filter == 'office') { this.application.ForwardingOfficer_Id = value.Id; this.application.ForwardingOfficerName = value.DesignationName; } } public mapProfileToApplicant = (profile: ApplicationProfileViewModel) => { if (profile) { this.application.EmployeeName = profile.EmployeeName ? profile.EmployeeName : ''; this.application.FatherName = profile.FatherName ? profile.FatherName : ''; this.application.DateOfBirth = profile.DateOfBirth ? new Date(profile.DateOfBirth) : new Date(2000, 1, 1); this.application.Gender = profile.Gender ? profile.Gender : 'Select Gender'; this.application.MobileNo = profile.MobileNo ? profile.MobileNo : ''; this.application.EMaiL = profile.EMaiL ? profile.EMaiL : ''; this.application.Department_Id = profile.Department_Id ? profile.Department_Id : this.application.Department_Id; this.application.DepartmentName = profile.Department_Id == 28 ? 'Specialized Healthcare & Medical Education' : 'Primary & Secondary Healthcare Department'; this.dropDowns.selectedFiltersModel.departmentDefault = profile.Department_Id == 28 ? { Name: 'Specialized Healthcare & Medical Education', Id: 28 } : profile.Department_Id == 25 ? { Name: 'Primary & Secondary Healthcare Department', Id: 25 } : this.dropDowns.selectedFiltersModel.department; let designations = this.dropDowns.designations as any[]; let designation = designations.find(x => x.Id == profile.Designation_Id); if (designation) { this.application.Designation_Id = profile.Designation_Id ? profile.Designation_Id : this.application.Designation_Id; this.application.designationName = designation.Name; this.dropDowns.selectedFiltersModel.designation = { Name: designation.Name, Id: designation.Name.Id }; } if (profile.HealthFacility_Id && profile.HfmisCode.length == 19) { this.getDivisions('0'); this.getDistricts('0'); this.getTehsils('0'); this.getHealthFacilities(profile.HfmisCode.substring(0, 9), profile.HfmisCode); } if (this.application.ApplicationType_Id == 3) { this.application.CurrentScale = profile.CurrentGradeBPS ? profile.CurrentGradeBPS : 0; this.application.SeniorityNumber = profile.SeniorityNo ? profile.SeniorityNo : ''; } } else { this.application.EmployeeName = ''; this.application.FatherName = ''; this.application.DateOfBirth = new Date(2000, 1, 1); this.application.Gender = 'Select Gender'; this.application.MobileNo = ''; this.application.EMaiL = ''; this.application.Department_Id = 25; this.application.DepartmentName = 'Primary & Secondary Healthcare Department'; this.application.Designation_Id = 0; this.application.designationName = ''; this.dropDowns.selectedFiltersModel.designation = { Name: 'Select Designation', Id: 0 }; this.dropDowns.selectedFiltersModel.healthFacility = { Name: 'Select Health Facility', Id: 0 }; this.application.fromHealthFacility = ''; this.application.HfmisCode = ''; this.application.HealthFacility_Id = 0; this.application.fromHealthFacility = ''; } } private getPandSOfficers = (type: string) => { this.officers = []; this._rootService.getPandSOfficers(type).subscribe((res: any) => { this.officers = res; }, err => { this.handleError(err); } ); } private getDivisions = (code: string) => { this.dropDowns.divisions = []; this.dropDowns.divisionsData = []; this._rootService.getDivisions(code).subscribe((res: any) => { this.dropDowns.divisions = res; this.dropDowns.divisionsData = this.dropDowns.divisions.slice(); }, err => { this.handleError(err); } ); } private getDistricts = (code: string) => { this.dropDowns.districts = []; this.dropDowns.districtsData = []; this._rootService.getDistricts(code).subscribe((res: any) => { this.dropDowns.districts = res; this.dropDowns.districtsData = this.dropDowns.districts.slice(); }, err => { this.handleError(err); } ); } private getTehsils = (code: string) => { this.dropDowns.tehsils = []; this.dropDowns.tehsilsData = []; this._rootService.getTehsils(code).subscribe((res: any) => { this.dropDowns.tehsils = res; this.dropDowns.tehsilsData = this.dropDowns.tehsils.slice(); }, err => { this.handleError(err); } ); } private getHealthFacilities = (hfmisCode: string, profileHfmisCode?: string) => { this._rootService.getHealthFacilities(hfmisCode).subscribe((res: any) => { this.dropDowns.healthFacilities = res; this.dropDowns.healthFacilitiesData = this.dropDowns.healthFacilities.slice(); if (profileHfmisCode) { this.setProfileDefaultValues(profileHfmisCode); } }, err => { this.handleError(err); } ); } public setProfileDefaultValues = (profileHfmisCode) => { if (profileHfmisCode) { let divisions = this.dropDowns.divisions as any; let division = divisions.find(x => x.Code == profileHfmisCode.substring(0, 3)); if (division) { this.dropDowns.selectedFiltersModel.division = { Code: division.Code, Name: division.Name }; } let districts = this.dropDowns.districts as any; let district = districts.find(x => x.Code == profileHfmisCode.substring(0, 6)); if (district) { this.dropDowns.selectedFiltersModel.district = { Code: district.Code, Name: district.Name }; } let tehsils = this.dropDowns.tehsils as any; let tehsil = tehsils.find(x => x.Code == profileHfmisCode.substring(0, 9)); if (tehsil) { this.dropDowns.selectedFiltersModel.tehsil = { Code: tehsil.Code, Name: tehsil.Name }; } let healthFacilities = this.dropDowns.healthFacilities as any; let healthFacility = healthFacilities.find(x => x.HfmisCode == profileHfmisCode); if (healthFacility) { this.dropDowns.selectedFiltersModel.healthFacility = { Id: healthFacility.Id, HfmisCode: healthFacility.HfmisCode, Name: healthFacility.Name }; this.application.HfmisCode = healthFacility.HfmisCode; this.application.HealthFacility_Id = healthFacility.Id; this.application.fromHealthFacility = healthFacility.Name; } } } printApplication() { let html = document.getElementById('applicationPrint').innerHTML; var mywindow = window.open('', 'PRINT', 'height=600,width=900'); if (mywindow) { mywindow.document.write(` Application`); mywindow.document.write(''); mywindow.document.write(''); mywindow.document.write(html); mywindow.document.write(` `); mywindow.document.write(''); /* mywindow.document.close(); // necessary for IE >= 10 mywindow.focus(); // necessary for IE >= 10 mywindow.print(); mywindow.close(); */ } } private handleError(err: any) { this.savingApplication = false; this.loading = false; if (err.status == 403) { this._authenticationService.logout(); } } }