import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild, Input, ViewContainerRef } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TagTypeServiceProxy, UpdateTagTypeInput, LocatorServiceProxy, MoveOrder} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';

import * as moment from 'moment';
import { MapFlyOutComponent } from '@app/shared/layout/flyout/map/map-flyout.component';
@Component({
    selector: 'moveOrder',
    templateUrl: './move-order.component.html'
})
export class MoveOrderModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal', {static: false}) modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    @ViewChild('mapFlyOutComponent', {static: false}) mapFlyOutComponent: MapFlyOutComponent;
    @Input() filters: {
        locationFilter: number;
        statusFilter: number;
        typeFilter: number;
        startDate: moment.Moment;
        endDate: moment.Moment;
    } = <any>{};
    orderId:number;
    active = false;
    saving = false;
    movetoRouteId:number;
    data: any;
    
    move: MoveOrder = new MoveOrder();
    // tagType: UpdateTagTypeInput = new UpdateTagTypeInput();

    constructor(
        injector: Injector,
        private _locatorAppService: LocatorServiceProxy,
        private viewContainerRef: ViewContainerRef
    ) {
        super(injector);
    }

    getParentComponent() {
        return this.viewContainerRef[ '_data' ].componentView.component.viewContainerRef[ '_view' ].component
    }

    show(orderId): void {
        console.log(orderId);
        this.orderId = orderId;
        // this.tagType = tagTypeRecord;
        // // console.log(this.orderStatusType);
        this.getOption(orderId);
        this.active = true;
        this.modal.show();
    }

    getOption(orderId){
        this._locatorAppService.getRouteListDetail(orderId, moment(this.filters.startDate), moment(this.filters.endDate), this.filters.locationFilter)
        .subscribe(result=>{
            this.data = result.items;
        });
    }
    onShown(): void {
        this.movetoRouteId = undefined;
        // document.getElementById('Code').focus();
    }

    save(): void {
        this.saving = true;
        this.move.orderId = this.orderId;
        this.move.routeId = this.movetoRouteId;
        this._locatorAppService.moveOrder(this.move)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
                this.getParentComponent().mapFlyOutComponent.plotRouteMarker();
            });
        this.close();
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
