import { Component, EventEmitter, Injector, ViewChild, Output, Input } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { WillCallServiceServiceProxy, Route4MeServiceProxy, GetClinicResultDto, GetRouteByClinicResultDto, CreateCallinInput, GetRouteByETAResultDto, CreateOptimizeInputDto } from '@shared/service-proxies/service-proxies'; import { ModalDirective } from 'ngx-bootstrap'; import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper'; import { finalize } from 'rxjs/operators'; import * as jquery from 'jquery'; import * as moment from 'moment'; @Component({ templateUrl: './call_in_modal.component.html', selector: 'callInModal', styleUrls: ['./calls_modal.component.less'], providers : [WillCallServiceServiceProxy, Route4MeServiceProxy] }) export class CallInModalComponent extends AppComponentBase { @ViewChild('callInModal', {static: false}) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; hide: boolean = false; txtFilter: any; clinics: GetClinicResultDto[]; routes: GetRouteByETAResultDto[]; CreateCallinInput = new CreateCallinInput(); OptimizeInput = new CreateOptimizeInputDto(); clinicId:number; routeCode:string; routeId:number; hide_callin: boolean = true; currently_selected: string; eta_selected: string; load_date: Date; cut_off: string; @Input('inputs') inputs: { locationId: number; loadDate: any; } = {}; constructor( injector: Injector, private _callIn : WillCallServiceServiceProxy, private _route4me : Route4MeServiceProxy ) { super(injector); } hideHistory() { if (this.hide == true) { this.hide = false; } else { this.hide = true; } } showHideCallinList() { if (this.hide_callin == true) { this.hide_callin = false; } else { this.hide_callin = true; } } ngOnInit(){ this.currently_selected = 'NONE'; this.hide_callin = false; } searchForClinic(): void { this._callIn.getClinicForCallBack(this.txtFilter,undefined) .subscribe(result => { this.clinics = result; }); } show(clinicId?: number) { this._callIn.getClinicForCallBackWithId(undefined,clinicId) .subscribe(result=>{ this.txtFilter = result[0] this.clinicId = this.txtFilter.id this.active = true; this.modal.show(); this._callIn.getRoutesByETA(this.txtFilter.id, moment(localStorage.getItem('loadDate')).utc(), parseInt(localStorage.getItem('operatingLocationId')), undefined) .subscribe(result => { var fresult = JSON.parse(JSON.stringify(result)); var key; for(key in fresult) { fresult[key].cutOff = moment.tz(localStorage.getItem('loadDate') + ' ' + fresult[0].cutOff, localStorage.getItem('timeZone')).format('hh:mm A'); } this.routes = fresult; // console.log(this.routes +" routes"); // console.log('F RESULT: '); // console.log(fresult); if(Object.keys(fresult).length > 0) { var that = this; that.currently_selected = fresult[0].routeCode; that.eta_selected = fresult[0].etaOriginal; that.cut_off = moment.tz(localStorage.getItem('loadDate') + ' ' + fresult[0].cutOff, localStorage.getItem('timeZone')).format('hh:mm A'); $('.itemCode').addClass('active'); } else{ this.currently_selected = 'NONE'; } } ); }); } getRoutes(event) : void { this.clinicId = event.id this._callIn.getRoutesByETA(this.clinicId, moment(localStorage.getItem('loadDate')).utc(), parseInt(localStorage.getItem('operatingLocationId')), undefined) .subscribe(result => { var fresult = JSON.parse(JSON.stringify(result)); var key; for(key in fresult) { fresult[key].cutOff = moment.tz(localStorage.getItem('loadDate') + ' ' + fresult[0].cutOff, localStorage.getItem('timeZone')).format('hh:mm A'); } this.routes = fresult; console.log(this.routes +" routes"); console.log('F RESULT: '); console.log(fresult); if(Object.keys(fresult).length > 0) { var that = this; that.currently_selected = fresult[0].routeCode; that.eta_selected = fresult[0].etaOriginal; that.cut_off = moment.tz(localStorage.getItem('loadDate') + ' ' + fresult[0].cutOff, localStorage.getItem('timeZone')).format('hh:mm A'); $('.itemCode').addClass('active'); } else{ this.currently_selected = 'NONE'; } } ); } clinicChange(routeCode: string) { this.currently_selected = routeCode; $(this).addClass('active'); } // show() { // this.active = true; // this.modal.show(); // } save(): void { let timeId = localStorage.getItem('timeZoneId'); // this.CreateCallinInput.routeCode = (document.getElementById('routeList')).value; this.CreateCallinInput.routeCode = this.currently_selected; this.CreateCallinInput.clinicId = this.clinicId; //this.CreateCallinInput.loadDate = moment(localStorage.getItem('loadDate')); this.CreateCallinInput.requestorName = (document.getElementById('txtRequestorName')).value; this.CreateCallinInput.specialInstructions = (document.getElementById('notes')).value; this.CreateCallinInput.loadDate = moment.tz(localStorage.getItem('loadDate'), timeId); this._callIn.createCallIn(this.CreateCallinInput) .subscribe((result) => { // this.OptimizeInput.routeId = result.routeId; // this.OptimizeInput.isReOptimize = true; // this._route4me.optimize(this.OptimizeInput) // .subscribe((optimizeResult) => { // this.notify.info(this.l('Route ' + this.CreateCallinInput.routeCode + ' has been re-optimized')); // this.close(); // }); this.notify.info(this.l('SavedSuccessfully')); this.close(); }); } saveNew(): void { var that = this; let timeId = localStorage.getItem('timeZoneId'); // this.CreateCallinInput.routeCode = (document.getElementById('routeList')).value; this.CreateCallinInput.routeCode = this.currently_selected; this.CreateCallinInput.clinicId = this.clinicId; //this.CreateCallinInput.loadDate = moment(localStorage.getItem('loadDate')); this.CreateCallinInput.requestorName = (document.getElementById('txtRequestorName')).value; this.CreateCallinInput.specialInstructions = (document.getElementById('notes')).value; this.CreateCallinInput.loadDate = moment.tz(localStorage.getItem('loadDate'), timeId); this._callIn.createCallIn(this.CreateCallinInput) .subscribe((result) => { // this.OptimizeInput.routeId = result.routeId; // this.OptimizeInput.isReOptimize = true; // this._route4me.optimize(this.OptimizeInput) // .subscribe((optimizeResult) => { // this.notify.info(this.l('Route ' + this.CreateCallinInput.routeCode + ' has been re-optimized')); // this.close(); // }); $('#clinicList').val(''); $('#txtRequestorName').val(''); that.routes = []; $('#notes').val(''); $('#clinicList').focus(); that.txtFilter = ''; this.notify.info(this.l('SavedSuccessfully')); }); } onShown(): void { $('.kt-select2').select2(); } close(): void { this.active = false; this.modal.hide(); } }