import { Component, EventEmitter, Injector, ViewChild, Output } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { UpdateRouteInput, 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: './edit-routes-modal.component.html',
    selector: 'editRouteModal',
})
export class EditRouteModalComponent extends AppComponentBase {

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

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

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

    locationId: any;
    routeTemplateId: any;
    vehicleId: any;
   

    loadDate: Date;
    departure: Date;
    completion: Date;

    locked = false;

    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(id): void {
      this.active = true;
      this.route.id = id;

      this._routeService.getRoute(id, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result)=> {
        console.log(result.items);
        this.loadDate = result.items[0]['loadDate'].toDate();
        this.completion = result.items[0]['completion'].toDate(); 
        this.departure = result.items[0]['departure'].toDate();
        this.locked = result.items[0]['locked'];


        this.locationId = result.items[0]['locationId'];
        this.routeTemplateId = result.items[0]['routeTemplateId'];
        this.vehicleId = result.items[0]['vehicleId'];
        
        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.updateRoute(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.locationId = null;
        this.routeTemplateId = null;
        this.vehicleId = null;
        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;
        });
    }


}
