import { Component, ViewChild, Injector, Output, EventEmitter, OnInit} from '@angular/core'; import { ModalDirective, TabsetComponent } from 'ngx-bootstrap'; import { catchError, finalize } from 'rxjs/operators'; import { RouteHandOffScheduleDetailsServiceProxy, CreateOrEditRouteHandOffScheduleDetailDto, RouteHandOffDefinitionsServiceProxy, RouteDefinitionsServiceProxy, CreatePostalCodeInput, CreateContactInput, CreateAddressInput, PostalCodeServiceProxy, GetRouteHandOffDefinitionForViewDto, Types } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { ActivatedRoute } from '@angular/router'; import { ContactFormMainComponent } from '@app/shared/layout/formMain/contact.form.component'; import { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; import { RouteHandOffScheduleDetailRouteTemplateLookupTableModalComponent } from './routeHandOffScheduleDetail-routeTemplate-lookup-table-modal.component'; import { throwError } from 'rxjs'; //import { RouteHandOffScheduleDetailRouteTypeLookupTableModalComponent } from './routeHandOffScheduleDetail-routeType-lookup-table-modal.component'; @Component({ selector: 'createOrEditRouteHandOffScheduleDetailModal', templateUrl: './create-or-edit-routeHandOffScheduleDetail-modal.component.html' }) export class CreateOrEditRouteHandOffScheduleDetailModalComponent extends AppComponentBase implements OnInit { @ViewChild(ContactFormMainComponent, { static: true }) contactForm; @ViewChild(AddressFormComponent, { static: true }) addressForm; @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild('#routeHandOffScheduleTab', { static: true }) staticTabs: TabsetComponent; @ViewChild('routeHandOffScheduleDetailRouteTemplateLookupTableModal', { static: true }) routeHandOffScheduleDetailRouteTemplateLookupTableModal: RouteHandOffScheduleDetailRouteTemplateLookupTableModalComponent; //@ViewChild('routeHandOffScheduleDetailRouteTypeLookupTableModal', { static: true }) routeHandOffScheduleDetailRouteTypeLookupTableModal: RouteHandOffScheduleDetailRouteTypeLookupTableModalComponent; @Output() modalSave: EventEmitter = new EventEmitter(); routeHandOffScheduleDetail: CreateOrEditRouteHandOffScheduleDetailDto = new CreateOrEditRouteHandOffScheduleDetailDto(); postalCodes: CreatePostalCodeInput = new CreatePostalCodeInput(); contact: CreateContactInput = new CreateContactInput(); address: CreateAddressInput = new CreateAddressInput(); routeTemplateName = ''; routeTypeRouteTypeCode = ''; active = false; saving = false; activeTabIndex: number = 0; routeTypes: any; routeDefinitions: any; maxCount = 1000; edit = false; eta: any; etaMonday: any; etaTuesday: any; etaWednesday: any; etaThursday: any; etaFriday: any; etaSaturday: any; etaSunday: any; effectivityDate: any; contactId: any; paramId: any; firstName: string; lastName: string; phoneNumber: string; emailAddress: string; showSearchOnRouteCode: boolean; sundayIsAuto: any; mondayIsAuto: any; tuesdayIsAuto: any; wednesdayIsAuto: any; thursdayIsAuto: any; fridayIsAuto: any; saturdayIsAuto: any; today = new Date(); dayList: Array = [ {id: 1, name: 'Sunday'}, {id: 2, name: 'Monday'}, {id: 3, name: 'Tuesday'}, {id: 4, name: 'Wednesday'}, {id: 5, name: 'Thursday'}, {id: 6, name: 'Friday'}, {id: 7, name: 'Saturday'} ]; dayListSel: Array = []; daySelect: any; constructor( injector: Injector, private _routeHandOffScheduleDetailsServiceProxy: RouteHandOffScheduleDetailsServiceProxy, private _routeHandOffDefinitionsServiceProxy: RouteHandOffDefinitionsServiceProxy, private _activatedRoute: ActivatedRoute, private _routeDefinitionsServiceProxy: RouteDefinitionsServiceProxy, private _postalCodeServiceProxy: PostalCodeServiceProxy ) { super(injector); } ngOnInit() { this.getRouteDefinitions(); } show(routeHandOffScheduleDetails: any, routeHandOffScheduleDetailId?: any): void { this.dayList= [ {id: 1, name: 'Sunday'}, {id: 2, name: 'Monday'}, {id: 3, name: 'Tuesday'}, {id: 4, name: 'Wednesday'}, {id: 5, name: 'Thursday'}, {id: 6, name: 'Friday'}, {id: 7, name: 'Saturday'} ]; this.dayListSel= []; let that = this; this.showSearchOnRouteCode = true; $(document).ready(function() { $('#routeDefinition').on('select2:select', function (e) { let data = e.params.data; let selectedRouteDefintion = that.routeDefinitions.find(i => i.routeTemplateId == data.id); that.routeHandOffScheduleDetail.routeTemplateId = selectedRouteDefintion.routeTemplateId; that.routeHandOffScheduleDetail.routeCode = selectedRouteDefintion.routeTemplateName; that.routeHandOffScheduleDetail.routeTypeId = selectedRouteDefintion.routeDefinition.routeTypeId; that.getAllDropDown(); }); }); if (routeHandOffScheduleDetails == undefined) { this.getParamId(); this.routeHandOffScheduleDetail.isActive = true; this.routeHandOffScheduleDetail.oneTime = true; this.eta = this.today; this.routeTemplateName = ''; this.routeTypeRouteTypeCode = ''; this.getAllDropDown(); this.active = true; this.modal.show(); this.firstName = ''; this.lastName = ''; this.phoneNumber = ''; this.emailAddress = ''; this.contactId = 0; } else { console.log('pasok'); console.log(routeHandOffScheduleDetailId); console.log(routeHandOffScheduleDetails); this.today = moment(routeHandOffScheduleDetails.today).tz(localStorage.getItem('timeZoneId')).toDate(); this.edit = true; this.routeHandOffScheduleDetail = routeHandOffScheduleDetails; this.paramId = routeHandOffScheduleDetailId; this.active = true; this.modal.show(); let dateEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.estimatedTimeOfArrival; this.eta = this.today; if(routeHandOffScheduleDetails.mondayETATime != null) { this.mondayIsAuto = 1; this.dayListSel.push(2); const index: number = this.dayList.findIndex(x => x.id == 2); if (index !== -1) { this.dayList.splice(index, 1); } let mondayEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.mondayETATime; this.etaMonday = routeHandOffScheduleDetails.mondayETATime == null ? null : moment(mondayEta).toDate(); } else { this.mondayIsAuto = 0; } if(routeHandOffScheduleDetails.tuesdayETATime != null) { this.tuesdayIsAuto = 1; this.dayListSel.push(3); const index: number = this.dayList.findIndex(x => x.id == 3); if (index !== -1) { this.dayList.splice(index, 1); } let tuesdayEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.tuesdayETATime; this.etaTuesday = routeHandOffScheduleDetails.tuesdayETATime == null ? null : moment(tuesdayEta).toDate(); } else { this.tuesdayIsAuto = 0; } if(routeHandOffScheduleDetails.wednesdayETATime != null) { this.wednesdayIsAuto = 1; this.dayListSel.push(4); const index: number = this.dayList.findIndex(x => x.id == 4); if (index !== -1) { this.dayList.splice(index, 1); } let wednesdayEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.wednesdayETATime; this.etaWednesday = routeHandOffScheduleDetails.wednesdayETATime == null ? null : moment(wednesdayEta).toDate(); } else { this.wednesdayIsAuto = 0; } if(routeHandOffScheduleDetails.thursdayETATime != null) { this.thursdayIsAuto = 1; this.dayListSel.push(5); const index: number = this.dayList.findIndex(x => x.id == 5); if (index !== -1) { this.dayList.splice(index, 1); } let thursdayEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.thursdayETATime; this.etaThursday = routeHandOffScheduleDetails.thursdayETATime == null ? null : moment(thursdayEta).toDate(); } else { this.thursdayIsAuto = 0; } if(routeHandOffScheduleDetails.fridayETATime != null) { this.fridayIsAuto = 1; this.dayListSel.push(6); const index: number = this.dayList.findIndex(x => x.id == 6); if (index !== -1) { this.dayList.splice(index, 1); } let fridayEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.fridayETATime; this.etaFriday = routeHandOffScheduleDetails.fridayETATime == null ? null : moment(fridayEta).toDate(); } else { this.fridayIsAuto = 0; } if(routeHandOffScheduleDetails.saturdayETATime != null) { this.saturdayIsAuto = 1; this.dayListSel.push(7); const index: number = this.dayList.findIndex(x => x.id == 7); if (index !== -1) { this.dayList.splice(index, 1); } let saturdayEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.saturdayETATime; this.etaSaturday = routeHandOffScheduleDetails.saturdayETATime == null ? null : moment(saturdayEta).toDate(); } else { this.saturdayIsAuto = 0; } if(routeHandOffScheduleDetails.sundayETATime != null) { this.sundayIsAuto = 1; this.dayListSel.push(1); const index: number = this.dayList.findIndex(x => x.id == 1); if (index !== -1) { this.dayList.splice(index, 1); } let sundayEta = this.today.toDateString() + ' ' + routeHandOffScheduleDetails.sundayETATime; this.etaSunday = routeHandOffScheduleDetails.sundayETATime == null ? null : moment(sundayEta).toDate(); } else { this.sundayIsAuto = 0; } this.effectivityDate = routeHandOffScheduleDetails.effectivityDate.toDate(); this.firstName = routeHandOffScheduleDetails.contact.firstName; this.lastName = routeHandOffScheduleDetails.contact.lastName; this.contactId = routeHandOffScheduleDetails.contactId; this.getAllDropDown(); } this.daySelect = this.dayList.length > 0 ? this.dayList[0].id : 0; } save(): void { this.saving = true; this.routeHandOffScheduleDetail.routeHandOffDefinitionId = Number(this.paramId); this.routeHandOffScheduleDetail.routeTypeId = Number((document.getElementById('routeType')).value); this.routeHandOffScheduleDetail.effectivityDate = moment(this.effectivityDate).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.oorRate = this.routeHandOffScheduleDetail.serviceTime || this.routeHandOffScheduleDetail.oorRate == null ? 0 : this.routeHandOffScheduleDetail.oorRate; this.routeHandOffScheduleDetail.dateEtaLocked = this.routeHandOffScheduleDetail.dateEtaLocked != null ? moment.utc(this.routeHandOffScheduleDetail.dateEtaLocked) : this.routeHandOffScheduleDetail.dateEtaLocked; var etaMoment = moment(this.eta).tz(localStorage.getItem('timeZoneId')); if (this.mondayIsAuto == 1) { var mondayEtaMoment = moment(this.etaMonday).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.mondayETATime = new Date(mondayEtaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; } else { this.routeHandOffScheduleDetail.mondayETATime = null; } if (this.tuesdayIsAuto == 1) { var tuesdayEtaMoment = moment(this.etaTuesday).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.tuesdayETATime = new Date(tuesdayEtaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; } else { this.routeHandOffScheduleDetail.tuesdayETATime = null; } if (this.wednesdayIsAuto == 1) { var wednesdayEtaMoment = moment(this.etaWednesday).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.wednesdayETATime = new Date(wednesdayEtaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; } else { this.routeHandOffScheduleDetail.wednesdayETATime = null; } if (this.thursdayIsAuto == 1) { var thursdayEtaMoment = moment(this.etaThursday).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.thursdayETATime = new Date(thursdayEtaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; } else { this.routeHandOffScheduleDetail.thursdayETATime = null; } if (this.fridayIsAuto == 1) { var fridayEtaMoment = moment(this.etaFriday).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.fridayETATime = new Date(fridayEtaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; } else { this.routeHandOffScheduleDetail.fridayETATime = null; } if (this.saturdayIsAuto == 1) { var saturdayEtaMoment = moment(this.etaSaturday).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.saturdayETATime = new Date(saturdayEtaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; } else { this.routeHandOffScheduleDetail.saturdayETATime = null; } if (this.sundayIsAuto == 1) { var sundayEtaMoment = moment(this.etaSunday).tz(localStorage.getItem('timeZoneId')); this.routeHandOffScheduleDetail.sundayETATime = new Date(sundayEtaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; } else { this.routeHandOffScheduleDetail.sundayETATime = null; } this.routeHandOffScheduleDetail.estimatedTimeOfArrival = etaMoment; this.routeHandOffScheduleDetail.etaTime = new Date(etaMoment.format()).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric"}) as any; if (this.routeHandOffScheduleDetail.routeTemplateId === undefined || this.routeHandOffScheduleDetail.routeTemplateId == null) { this.routeHandOffScheduleDetail.routeTemplateId = 0; this.saving = false; } if (this.routeHandOffScheduleDetail.routeTypeId === 0) { this.saving = false; (document.getElementById('routeTypeDiv')).style.color = '#FF3300'; } if (!this.routeHandOffScheduleDetail.effectivityDate.isValid()) { this.saving = false; (document.getElementById('effectiveDate')).style.color = '#FF3300'; } this.routeHandOffScheduleDetail.contactId = this.contactId; console.log('paso'); console.log(this.routeHandOffScheduleDetail); if (this.saving == true) { this._routeHandOffScheduleDetailsServiceProxy.createOrEdit(this.routeHandOffScheduleDetail) .pipe(catchError((err) => { console.log(err); return throwError(err); }), finalize(() => { this.saving = false; })) .subscribe(result => { this.notify.info(this.l('SavedSuccessfully')); this.modalSave.emit(null); this.close(); }, (error) => { console.log(error); return throwError(error); }); } } setRouteTemplateIdNull() { this.showSearchOnRouteCode = true; this.routeHandOffScheduleDetail.routeTemplateId = null; this.routeHandOffScheduleDetail.routeCode = ''; this.routeTemplateName = ''; } // setRouteTypeIdNull() { // this.routeHandOffScheduleDetail.routeTypeId = null; // this.routeTypeRouteTypeCode = ''; // } close(): void { this.edit = false; this.routeHandOffScheduleDetail = new CreateOrEditRouteHandOffScheduleDetailDto(); this.effectivityDate = null; this.active = false; this.modal.hide(); } getAllDropDown() { jQuery(document).ready(function () { $('.kt-select2').select2(); $('.select2-container').css('width', '100%'); }); this.getRouteTypes(); this.getRouteDefinitions(); } getRouteTypes() { this._routeHandOffDefinitionsServiceProxy.getAllRouteTypeForLookupTable('', 1, undefined, undefined, this.maxCount).subscribe(result => { this.routeTypes = result.items; }); } getRouteDefinitions() { this._routeDefinitionsServiceProxy.getAll( undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, this.maxCount ).subscribe(result => { this.routeDefinitions = result.items; }); } getParamId() { this._activatedRoute.paramMap.subscribe(params => { this.paramId = params.get('id'); }); } openSelectRouteTemplateModal() { this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.filterText = this.routeHandOffScheduleDetail.routeCode; this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.id = this.routeHandOffScheduleDetail.routeTemplateId; this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.displayName = this.routeHandOffScheduleDetail.routeCode; this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.displayName = this.routeTemplateName; this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.show(); } getNewRouteTemplateId() { this.showSearchOnRouteCode = false; this.routeHandOffScheduleDetail.routeTemplateId = this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.id; this.routeHandOffScheduleDetail.routeCode = this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.displayName; this.routeTemplateName = this.routeHandOffScheduleDetailRouteTemplateLookupTableModal.displayName; } addField(day){ if(day == 1){ let sundayEta = this.eta; this.etaSunday = sundayEta; this.sundayIsAuto = 1; } if(day == 2){ let mondayEta = this.eta; this.etaMonday = mondayEta; this.mondayIsAuto = 1; } if(day == 3){ let tuesdayEta = this.eta; this.etaTuesday = tuesdayEta; this.tuesdayIsAuto = 1; } if(day == 4){ let wednesdayEta = this.eta; this.etaWednesday = wednesdayEta; this.wednesdayIsAuto = 1; } if(day == 5){ let thursdayEta = this.eta; this.etaThursday = thursdayEta; this.thursdayIsAuto = 1; } if(day == 6){ let fridayEta = this.eta; this.etaFriday = fridayEta; this.fridayIsAuto = 1; } if(day == 7){ let saturdayEta = this.eta; this.etaSaturday = saturdayEta; this.saturdayIsAuto = 1; } this.dayListSel.push(day); const index: number = this.dayList.findIndex(x => x.id == day); if (index !== -1) { this.dayList.splice(index, 1); } this.daySelect = this.dayList.length > 0 ? this.dayList[0].id : 0; } checkDay(day: number){ if(this.dayListSel.find(x => x == day) != undefined){ return true; }else{ return false; } } removeField(day){ let name: string = ''; if(day == 1){ this.sundayIsAuto = 0; name = 'Sunday'; } if(day == 2){ this.mondayIsAuto = 0; name = 'Monday'; } if(day == 3){ this.tuesdayIsAuto = 0; name = 'Tuesday'; } if(day == 4){ this.wednesdayIsAuto = 0; name = 'Wednesday'; } if(day == 5){ this.thursdayIsAuto = 0; name = 'Thursday'; } if(day == 6){ this.fridayIsAuto = 0; name = 'Friday'; } if(day == 7){ this.saturdayIsAuto = 0; name = 'Saturday'; } let dd = {id : day, name : name}; this.dayList.push(dd); const index: number = this.dayListSel.findIndex(x => x == day); if (index !== -1) { this.dayListSel.splice(index, 1); } this.eta = this.today; this.daySelect = this.dayList[0].id; } }