import { Component, Injector, ViewChild, ViewEncapsulation, Input, Output, EventEmitter, OnInit, AfterViewInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppConsts } from '@shared/AppConsts'; import { AppComponentBase } from '@shared/common/app-component-base'; import { ClinicsServiceProxy, CreateOrEditClinicDto, RouteDefinitionsServiceProxy, CreateOrEditRouteDefinitionDto, RouteTypesServiceProxy, RouteScheduleDetailsServiceProxy, RouteScheduleDetailDto, CreateOrEditRouteScheduleDetailDto, CreateOrEditClinicScheduleDto, LockedEtaScheduleInput, GetClinicScheduleDto} from '@shared/service-proxies/service-proxies'; import { AddressServiceProxy, CreateAddressInput } from '@shared/service-proxies/service-proxies'; import { EmailServiceProxy, CreateEmailInput, AddressTypeServiceProxy, PostalCodeServiceProxy, } from '@shared/service-proxies/service-proxies'; import { RouteSchedulesServiceProxy, CreateOrEditRouteScheduleDto } from '@shared/service-proxies/service-proxies'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { CreateOrEditClinicModalComponent } from './create-or-edit-clinic-modal.component'; import {FormGroup, FormControl} from '@angular/forms'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment-timezone'; import * as _ from 'lodash'; import {Location} from '@angular/common'; import { ClinicScheduleFlyOutComponent } from '@app/shared/layout/flyout/clinic-schedule/clinic-schedule-flyout.component'; import { EntityTypeHistoryModalComponent } from '@app/shared/common/entityHistory/entity-type-history-modal.component'; declare var KTWizard: any; declare var $: any; declare var KTApp: any; declare let swal: any; @Component({ templateUrl: './clinic-schedule.component.html', styleUrls: ['./clinic-schedule.component.less'], encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()], }) export class ClinicScheduleComponent extends AppComponentBase { @Input('selectedCountry') selectedCountry: string; @Input('selectedCity') selectedCity: string; @Input('selectedState') selectedState: string; @Input('postalCodeId') selectedPostalCodeId: number; @Input('postalCodeValue') selectedPostalCodeValue: string; @ViewChild('createOrEditClinicModal', { static: true }) createOrEditClinicModal: CreateOrEditClinicModalComponent; @ViewChild('clinicScheduleFlyOutComponent', { static: false }) clinicScheduleFlyOutComponent: ClinicScheduleFlyOutComponent; @ViewChild('entityTypeHistoryModal', { static: true }) entityTypeHistoryModal: EntityTypeHistoryModalComponent; _entityTypeFullName = 'SprintTek.Laboratory.RouteScheduleDetail'; entityHistoryEnabled = false; goBackHistory: number; active = false; saving = false; id: any; newSchedule = false; //routeScheduleDetail: CreateOrEditRouteScheduleDetailDto = new CreateOrEditRouteScheduleDetailDto(); scheduleContainer: any[] = []; clinic: CreateOrEditClinicDto = new CreateOrEditClinicDto(); address: CreateAddressInput = new CreateAddressInput(); postalInfo: any; routeTypes: any; routeDefinitions: any; designation: any; schedules: any; newSched: any = {}; routeScheduleId: any; constructor( injector: Injector, private _clinicsServiceProxy: ClinicsServiceProxy, private _addressService: AddressServiceProxy, private _emailService: EmailServiceProxy, private _postalCodeService: PostalCodeServiceProxy, private _addressTypeService: AddressTypeServiceProxy, private _routeDefinitionsServiceProxy: RouteDefinitionsServiceProxy, private _routeScheduleDetailsServiceProxy: RouteScheduleDetailsServiceProxy, private _routeSchedulesServiceProxy: RouteSchedulesServiceProxy, private _location:Location, private _router: Router, private route: ActivatedRoute, ) { super(injector); } dynamicArray: Array = []; newDynamicArray: Array = []; newDynamic: any = {}; date: Date; eta: Array = []; cutOff: Array = []; effectivityDate: Array = []; oorRate: Array = []; savedDesignation: Array = []; i: any; savedSchedules: any = []; newSavedSchedules: Array = []; designationSelect: any; scheduleId: any; clinicId: any; lockedEtaInput: LockedEtaScheduleInput = new LockedEtaScheduleInput(); ngOnInit() { this.setIsEntityHistoryEnabled(); //this.paginator.changePage(0); //this.primengTableHelper.showLoadingIndicator(); //moment.tz.setDefault(localStorage.getItem('timeZoneId')); moment.tz.setDefault(null); //moment().tz(localStorage.getItem('timeZoneId')).format(); this.route.paramMap.subscribe(params => { this.id = params.get("id") }) this.showSchedule(); this.goBackHistory = window.history.length; } goBack(){ if(this.goBackHistory > 1){ this._location.back(); } } createClinicSchedule(): void { this.createOrEditClinicModal.show(undefined, this.scheduleId, this.clinic.id); } showSchedule() { let locationId = Number(localStorage.getItem('operatingLocationId')); this.active = true; this._clinicsServiceProxy.getClinicForEdit(this.id).subscribe(result => { //console.log(result); //this.spinnerService.show(); this.clinic = result.clinic; this.address = result.clinic.address; if(this.address.postalCodeId != 0) { this._postalCodeService.getPostalCodeEdit(this.address.postalCodeId).subscribe(resultPostalCode => { //this.spinnerService.show(); this.selectedCountry = resultPostalCode.country; this.selectedState = resultPostalCode.state; this.selectedCity = resultPostalCode.city; this.selectedPostalCodeValue = resultPostalCode.value; }); } }); var that = this; this._clinicsServiceProxy.getClinicSchedule(this.id, locationId).subscribe(result => { this.schedules = result.items; let today = new Date(); //console.log(this.schedules[0]['estimatedTimeOfArrival']); //this.schedules.estimatedTimeOfArrival = result.items.estimatedTimeOfArrival.toDate(); console.log('temp: remove moment tz'); for (let i = 0; i < result.items.length; i++) { //today = moment.utc(new Date()).format('MM/DD/YYYY'); this.schedules[i]['estimatedTimeOfArrival'] = moment(this.schedules[i]['displayEta']).tz(localStorage.getItem('timeZoneId')).format('h:mm a'); //moment(moment(today + " " + this.schedules[i]['estimatedTimeOfArrival'])).tz(localStorage.getItem('timeZoneId')).format(); this.schedules[i]['effectivityDate'] = moment(this.schedules[i]['effectivityDate']).tz(localStorage.getItem('timeZoneId')); if(this.schedules[i]['dateEtaLocked'] != null || this.schedules[i]['dateEtaLocked'] != undefined) { this.schedules[i]['dateEtaLocked'] = moment(this.schedules[i]['dateEtaLocked']).tz(localStorage.getItem('timeZoneId')); } this.schedules[i]['mondayETATime'] = this.schedules[i]['displayMondayETATime']==null ? "" : moment.parseZone(this.schedules[i]['displayMondayETATime']).format('h:mm a'); this.schedules[i]['tuesdayETATime'] = this.schedules[i]['displayTuesdayETATime']==null? "" : moment.parseZone(this.schedules[i]['displayTuesdayETATime']).format('h:mm a'); this.schedules[i]['wednesdayETATime'] = this.schedules[i]['displayWednesdayETATime']==null? "" : moment.parseZone(this.schedules[i]['displayWednesdayETATime']).format('h:mm a'); this.schedules[i]['thursdayETATime'] = this.schedules[i]['displayThursdayETATime']==null ? "" : moment.parseZone(this.schedules[i]['displayThursdayETATime']).format('h:mm a'); this.schedules[i]['fridayETATime'] = this.schedules[i]['displayFridayETATime']==null ? "" : moment.parseZone(this.schedules[i]['displayFridayETATime']).format('h:mm a'); this.schedules[i]['saturdayETATime'] = this.schedules[i]['displaySaturdayETATime']==null ? "" : moment.parseZone(this.schedules[i]['displaySaturdayETATime']).format('h:mm a'); this.schedules[i]['sundayETATime'] = this.schedules[i]['displaySundayETATime']==null ? "" : moment.parseZone(this.schedules[i]['displaySundayETATime']).format('h:mm a'); this.schedules[i]['mondayCutOffTime'] = this.schedules[i]['displayMondayCutOffTime']==null ? "" : moment.parseZone(this.schedules[i]['displayMondayCutOffTime']).format('h:mm a'); this.schedules[i]['tuesdayCutOffTime'] = this.schedules[i]['displayTuesdayCutOffTime']==null ? "" : moment.parseZone(this.schedules[i]['displayTuesdayCutOffTime']).format('h:mm a'); this.schedules[i]['wednesdayCutOffTime'] = this.schedules[i]['displayWednesdayCutOffTime']==null ? "" : moment.parseZone(this.schedules[i]['displayWednesdayCutOffTime']).format('h:mm a'); this.schedules[i]['thursdayCutOffTime'] = this.schedules[i]['displayThursdayCutOffTime']==null ? "" : moment.parseZone(this.schedules[i]['displayThursdayCutOffTime']).format('h:mm a'); this.schedules[i]['fridayCutOffTime'] = this.schedules[i]['displayFridayCutOffTime']==null ? "" : moment.parseZone(this.schedules[i]['displayFridayCutOffTime']).format('h:mm a'); this.schedules[i]['saturdayCutOffTime'] = this.schedules[i]['displaySaturdayCutOffTime']==null ? "" : moment.parseZone(this.schedules[i]['displaySaturdayCutOffTime']).format('h:mm a'); this.schedules[i]['sundayCutOffTime'] = this.schedules[i]['displaySundayCutOffTime']==null ? "" : moment.parseZone(this.schedules[i]['displaySundayCutOffTime']).format('h:mm a'); //this.schedules[i]['estimatedTimeOfArrival'] = moment(this.schedules[i]['displayEta']).format('h:mm a'); this.schedules[i]['cutOff'] = moment(this.schedules[i]['displayCutOff']).tz(localStorage.getItem('timeZoneId')).format('h:mm a'); //this.schedules[i]['cutOff'] = moment(this.schedules[i]['displayCutOff']).format('h:mm a'); if(this.schedules[i]['dateEtaLocked'] != null) { this.schedules[i]['dateEtaLocked'] = moment(this.schedules[i]['dateEtaLocked']).tz(localStorage.getItem('timeZoneId')).format('MM/DD/YYYY, h:mm a'); //console.log(this.schedules[i]['estimatedTimeOfArrival']); //console.log(moment(this.schedules[i]['displayEta']).tz(localStorage.getItem('timeZoneId')).format('h:mm a')); } if(this.schedules[i]['serviceTime'] == null) { this.schedules[i]['serviceTime'] = 0; } if(this.schedules[i]['timeWindowStart'] == null) { this.schedules[i]['timeWindowStart'] = 0; } if(this.schedules[i]['timeWindowEnd'] == null) { this.schedules[i]['timeWindowEnd'] = 0; } } console.log(this.schedules); if(result.items.length == 0) { that._routeSchedulesServiceProxy.getAll( that.clinic.id, undefined, undefined, undefined, undefined, undefined ).subscribe(resultSched => { that.routeScheduleId = resultSched.items; if(resultSched.items.length == 0) { that.newSchedule = true; //routeScheduleDetail.routeScheduleId = undefined; that.scheduleId = undefined; } else { that.newSchedule = false; //routeScheduleDetail.routeScheduleId = that.routeScheduleId[0]['routeSchedule']['id']; that.scheduleId = that.routeScheduleId[0]['routeSchedule']['id']; } }); //routeScheduleDetail.routeScheduleId = undefined; //this.newDynamic = {routeCode: "", routeType: "",eta:"", cutOff:"", effectivityDate:"", designation:"auto",oor:"6.18",routeScheduleId:this.schedules[0]['routeScheduleId']}; } else { result.items.forEach(function (value) { that.scheduleId = value.routeScheduleId; }); } //this.spinnerService.hide(); }); } deleteRouteScheduleDetail(routeScheduleDetail: RouteScheduleDetailDto): void { this.message.confirm( '', '', (isConfirmed) => { if (isConfirmed) { this._routeScheduleDetailsServiceProxy.delete(routeScheduleDetail.id) .subscribe(() => { this.reloadPage(); this.notify.success(this.l('SuccessfullyDeleted')); }); } } ); } reloadPage(): void { this.showSchedule(); } showFlyOut() : void { this.clinicScheduleFlyOutComponent.showFlyout(this.clinic); } lockEta(id: number, value: boolean) { this.lockedEtaInput.id = id; this.lockedEtaInput.etaLocked = value; //console.log(this.lockedEtaInput); this.message.confirm( '', '', (isConfirmed) => { if (isConfirmed) { this._routeScheduleDetailsServiceProxy.lockedEtaSchedule(this.lockedEtaInput) .pipe(finalize(() => { this.saving = false;})) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.reloadPage(); }); } } ); } private setIsEntityHistoryEnabled(): void { let customSettings = (abp as any).custom; this.entityHistoryEnabled = customSettings.EntityHistory && customSettings.EntityHistory.isEnabled && _.filter(customSettings.EntityHistory.enabledEntities, entityType => entityType === this._entityTypeFullName).length === 1; } showHistory(sched: GetClinicScheduleDto): void { this.entityTypeHistoryModal.show({ entityId: sched.id.toString(), entityTypeFullName: this._entityTypeFullName, entityTypeDescription: "Route Schedule - " +sched.routeCode }); } }