import { Component, EventEmitter, Injector, ViewChild, Output, Input } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateRouteInput, RouteServiceProxy, LocationServiceProxy, LocationListDto, VehicleServiceProxy, VehicleListDto, RouteTemplateServiceProxy, RouteTemplateListDto } 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: './create-routes-controller-modal.component.html',
    selector: 'createRouteControllerModal',
})
export class CreateRouteControllerModalComponent extends AppComponentBase {

   @ViewChild('createModal', {static: false}) modal: ModalDirective;
   @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    route: CreateRouteInput = new CreateRouteInput();

    filteredLocation: any;
    filteredVehicle: any;
    filteredRouteTemplate: any;

    loadDate: Date;
    departure: Date;
    completion: Date;
    routeTemplateInput: any;
    
    locked = false;

    @Input('inputs') inputs: {
        locationId: number;
        loadDate: any;
    } = <any>{};

    @Output() refreshRoute = new EventEmitter<boolean>();
    public DefaultDateValue: Date = new Date();

    constructor(
        injector: Injector,
        private _locationAppService: LocationServiceProxy,
        private _vehicleAppService: VehicleServiceProxy,
        private _routeTemplateAppService: RouteTemplateServiceProxy,
        private _routeService: RouteServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(){
      let maxcount = 1000;
      // this.filterLocation(maxcount);
      
      this.filterRouteTemplate(maxcount);
     
    }

    show() {
        this.active = true;
        //this.routeTemplateType.name = null;
        this.modal.show();
        
    }

    onShown(): void {
        //document.getElementById('cityInput').focus();
         $('.kt-select2').select2();
    }

    save(): void {
        this.saving = true;
        this.route.locationId = this.inputs.locationId;
        this.route.vehicleId  = 0;
        this.route.routeTemplateId = Number((<HTMLInputElement>document.getElementById('routeTemplate')).value);
        // this.route.loadDate = this.inputs.loadDate;
        this.route.loadDate = moment.utc(moment(this.inputs.loadDate).format('YYYY-MM-DD'));
        this.route.completion = null;
        this.route.departure = null;
        this.route.locked = false;

        this._routeService.createRoute(this.route)
                    .pipe(finalize(() => this.saving = false))
                    .subscribe(() => { 
                        this.notify.info(this.l('SavedSuccessfully'));
                        this.refreshRoute.emit();
                        this.close();
                        // this.modalSave.emit(null);
        });
         
        // console.log(this.inputs);
    }

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

    // filterLocation(maxcount): void {
    //     this._locationAppService.getLocation(
    //         undefined,
    //         undefined,
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredLocation = result.items;
    //     });
    // }

    filterRouteTemplate(maxcount): void {
        this._routeTemplateAppService.getRouteTemplate(
            undefined,
            this.inputs.locationId,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            maxcount,
            undefined
        ).subscribe(result => {
            console.log('FILTER ROUTE TEMPLATES: ');
            console.log(result);
            if(result.items.length > 0)
            {
                this.routeTemplateInput = result.items[0].id;
            }
            
            this.filteredRouteTemplate = result.items;
        });
    }
}
