import { Component, EventEmitter, Injector, ViewChild, Output } 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-modal.component.html',
    selector: 'createRouteModal',
})
export class CreateRouteModalComponent 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;

    locked = false;

    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.filterVehicle(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 = Number((<HTMLInputElement>document.getElementById('location')).value);
        this.route.vehicleId = Number((<HTMLInputElement>document.getElementById('vehicle')).value);
        this.route.routeTemplateId = Number((<HTMLInputElement>document.getElementById('routeTemplate')).value);
        this.route.loadDate = moment.utc(moment(this.loadDate).format('YYYY-MM-DD'));
        this.route.completion = moment.utc(moment(this.loadDate).format('YYYY-MM-DD') + ' ' + moment(this.completion).format('HH:mm:ss'));
        this.route.departure = moment.utc(moment(this.loadDate).format('YYYY-MM-DD') + ' ' + moment(this.departure).format('HH:mm:ss'));
        this.route.locked = this.locked;

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

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

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

    filterRouteTemplate(maxcount): void {
        this._routeTemplateAppService.getRouteTemplate(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            maxcount,
            undefined
        ).subscribe(result => {
            this.filteredRouteTemplate = result.items;
        });
    }

    filterVehicle(maxcount): void {
        this._vehicleAppService.getVehiclePaged(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            maxcount,
            undefined
        ).subscribe(result => {
            this.filteredVehicle = result.items;
        });
    }


}
