import { Component, ViewChild, Injector, Output, EventEmitter, OnInit, AfterViewInit} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { HolidaysServiceProxy, CreateOrEditHolidayDto, LocationServiceProxy, SelectionType, HolidayDto, LocationNameDto, RoutesForCalendarDto } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import {ChipsModule} from 'primeng/chips'; import { HolidayAddLocationComponent } from './holiday-add-location/holiday-add-location.component'; import { CalendarAddRouteComponent } from './calendar-add-route/calendar-add-route.component'; declare var $ : any @Component({ selector: 'createOrEditHolidayModal', templateUrl: './create-or-edit-holiday-modal.component.html', styleUrls: ['./create-or-edit-holiday-modal.component.less'] }) export class CreateOrEditHolidayModalComponent extends AppComponentBase implements OnInit{ @ViewChild('createOrEditModal', { static: false }) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); @ViewChild('holidayAddLocation', {static: true}) holidayAddLocation: HolidayAddLocationComponent; @ViewChild('calendarAddRoute', {static: true}) calendarAddRoute: CalendarAddRouteComponent; active = false; saving = false; selectionType : any = 0; locations : LocationNameDto[] = []; routes: RoutesForCalendarDto[] = []; locationNames : string[]; routeNames : string[]; filteredLocation: any; from: any ; fromDate: any = moment(); fromTime: any; toDate: any = moment(); toTime: any; to: any; isActive: number = 1; today = new Date(); showLoc : boolean; showRoute: boolean = false; actionTypes : any; selectedAction: any = 0; notHoliday : boolean = false; notAll: boolean = false; activeTabIndex: number = 0; _isLab: boolean holiday: CreateOrEditHolidayDto = new CreateOrEditHolidayDto(); constructor( injector: Injector, private _holidaysServiceProxy: HolidaysServiceProxy, private _locationAppService: LocationServiceProxy, ) { super(injector); this._isLab = abp.features.isEnabled('App.RouteDetails'); } ngOnInit(): void { } onShow(){ let maxcount = 1000; this.filterLocation(maxcount); } onShown(EventEmitter){ $('.kt-select2').select2(); $("#selectionType").select2({ minimumResultsForSearch: Infinity }); this.holiday.selectionType == undefined ? $("#selectionType").val(SelectionType[0]) : $("#selectionType").val(SelectionType[this.holiday.selectionType]); $("#selectionType").trigger('change'); this.hideShowLocation() let that = this; $('#selectionType').on('select2:select', function (e) { if($('#selectionType').val() == 'ALL'){ $("#locationDiv").hide(); this.showLoc = false; that.showRoute = false; that.notAll = false; } else{ $("#locationDiv").show(); this.showLoc = true; that.notAll = true; if(that.selectedAction == 2) { if(that.locations.length != 0) { that.showRoute = true } } } }); } hideAdjustmentFiled(actionId: any){ this.selectedAction = actionId; if(this.selectedAction != 0) { this.notHoliday = true; } else { this.notHoliday = false; } } onOptionsSelected(value:string){ let actionId = Number(value) this.hideAdjustmentFiled(actionId); //alert(this.notAll); if(actionId == 2) { if(this.notAll == true) { if(this.locations.length != 0) { this.showRoute = true; } } } else { this.showRoute = false; } } hideShowLocation(){ let that = this; if($('#selectionType').val() == 'ALL'){ $("#locationDiv").hide(); this.showLoc = false; that.showRoute = false; that.notAll = false; } else{ $("#locationDiv").show(); this.showLoc = true; that.notAll = true; if(that.selectedAction == 2) { if(that.locations.length != 0) { that.showRoute = true } } } } filterLocation(maxcount): void { this._locationAppService.getLocationTableOnly( undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.filteredLocation = result; }); } show(holiday?: HolidayDto): void { this.locations = []; this.routes = []; this.getActionTypes(); if (!holiday) { this.holiday = new CreateOrEditHolidayDto(); this.holiday.from = moment().startOf('day'); this.holiday.to = moment().startOf('day'); this.holiday.color = '#000000'; this.fromTime = new Date(Date.now()); this.toTime = new Date(Date.now()); this.selectedAction = 0; $("#location").hide(); this.active = true; this.modal.show(); } else { this.holiday.id = holiday.id; this.holiday.code = holiday.code; this.holiday.color = holiday.color; this.holiday.description = holiday.description; this.isActive = holiday.isActive this.holiday.selectionType = holiday.selectionType; this.locations = holiday.locations this.locationNames = this.locations.map(c => c.name); this.routes = holiday.routes this.routeNames = this.routes.map(c => c.routeCode); this.selectedAction = holiday.actionType; if(holiday.actionType != 0) { this.notHoliday = true; if(holiday.actionType == 2 && this.routes.length != 0) { this.showRoute = true var result = this.locations.reduce((unique, o) => { if(!unique.some(obj => obj.id === o.id)) { unique.push(o); } return unique; },[]); this.locations = result; this.locationNames = this.locations.map(c => c.name); } } else { this.notHoliday = false; } if(this.holiday.selectionType != 0) { this.notAll = true; } this.holiday.cutOffAdjustment = holiday.cutOffAdjustment; //this.holiday.locationIds = holiday.locations; this.fromDate= holiday.from.utc(false).format('ddd MMM DD YYYY') this.toDate= holiday.to.utc(false).format('ddd MMM DD YYYY') let from = holiday.from.utc(false); let to = holiday.to.utc(false);; this.fromTime = new Date(from.year(),from.month(),from.date(),from.hours(),from.minutes()) this.toTime = new Date(to.year(),to.month(),to.date(),to.hour(),to.minutes()) //this.toTime = new Date(new Date(this.holiday.to.format('H:mm a')).toISOString()); this.active = true; this.modal.show(); // this._holidaysServiceProxy.getHolidayForEdit(holidayId).subscribe(result => { // this.holiday = result.holiday; // this.fromDate= this.holiday.from.utc(false).format('ddd MMM DD YYYY') // this.toDate= this.holiday.to.utc(false).format('ddd MMM DD YYYY') // let from = this.holiday.from.utc(false); // let to = this.holiday.to.utc(false);; // this.fromTime = new Date(from.year(),from.month(),from.date(),from.hours(),from.minutes()) // this.toTime = new Date(to.year(),to.month(),to.date(),to.hour(),to.minutes()) // //this.toTime = new Date(new Date(this.holiday.to.format('H:mm a')).toISOString()); // this.isActive = this.holiday.isActive // this.active = true; // this.modal.show(); // }); } this.modal } buildHolidayObject(){ this.selectionType = $("#selectionType").val() == "" ? undefined : $("#selectionType").val(); let fromTime = new Date(this.fromTime).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric" }) as any; let toTime = new Date(this.toTime).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric" }) as any; this.from = moment.utc(this.fromDate.utc(false).toDate().toDateString() + ' ' + fromTime); this.to = moment.utc(this.toDate.utc(false).toDate().toDateString() + ' ' +toTime); //this.locationIds = this.locations.map(c => c.id); this.holiday.selectionType = this.selectionType; this.holiday.isActive = this.isActive ? 1 : 0 this.holiday.from = this.from; this.holiday.to = this.to; if(this.selectionType != SelectionType.ALL){ this.holiday.locationIds = this.locations.map(c => c.id); if(this.selectedAction == 2) { this.holiday.routeIds = this.routes.map(c => c.id); } } } save(): void { this.saving = true; this.buildHolidayObject(); if(this.holiday.from.isBefore(this.holiday.to)){ this.holiday.actionType = this.selectedAction; this._holidaysServiceProxy.createOrEdit(this.holiday) .pipe(finalize(() => { this.saving = false;})) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } else{ abp.message.warn('Date from cannot be set before or equal to Date to'); this.saving = false } } addNewLocation(){ if(this.locations){ this.holidayAddLocation.show(this.locations.map(c => c.id)); } else{ this.holidayAddLocation.show(null); } } addLocation(newLocations : LocationNameDto[]){ newLocations.map(c => { this.locations.push(c) }) this.locationNames = this.locations.map(c => c.name) if(this.locations != null && this.selectedAction == 2) { this.showRoute = true; } } removeLocation(item){ this.locations = this.locations.filter(c => c.name != item.value); this.routes = this.routes.filter(c => c.locationId != item.id); } close(): void { this.active = false; this.showRoute = false; this.notAll = false; this.notHoliday = false; this.modal.hide(); } addNewRoute(){ if(this.routes){ this.calendarAddRoute.show(this.locations.map(c => c.id), this.routes.map(c => c.id)); } else{ this.calendarAddRoute.show(null); } } addRoute(newRoutes : RoutesForCalendarDto[]){ newRoutes.map(c => { this.routes.push(c) }) this.routeNames = this.routes.map(c => c.routeCode) } removeRoute(item){ this.routes = this.routes.filter(c => c.routeCode != item.value); } getActionTypes() : void { this._holidaysServiceProxy.getActionType().subscribe(result => { this.actionTypes = result; //console.log(this.actionTypes); }); } replaceUnderscore(value: string) { let result = value.split('_').join(' '); return result; } }