import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    VehicleLocationServiceProxy, UpdateVehicleLocationInput, VehicleLocationListDto
} from '@shared/service-proxies/service-proxies';
import { LocationListDto, LocationServiceProxy } from '@shared/service-proxies/service-proxies';

import * as _ from 'lodash';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import * as jquery from 'jquery';

@Component({
    selector: 'editVehicleLocationModal',
    templateUrl: './edit-vehicle-location-modal.component.html'
})
export class EditVehicleLocationModalComponent extends AppComponentBase {

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

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

  vehicleLocation: UpdateVehicleLocationInput = new UpdateVehicleLocationInput();

  locations: LocationListDto[] = [];

  
  
  locationInput: {
    id: number;
    name: string;
  };

  

  txtLocation: any;

  active: boolean = false;
  saving: boolean = false;

  constructor(
    injector: Injector,
    private _vehicleLocationService: VehicleLocationServiceProxy,
    private _locationService: LocationServiceProxy,
        
  ) {
    super(injector);
  }

  show(vehicleLocationId: any, vehicleId: any): void {
    this.active = true;
    this._vehicleLocationService.getVehicleLocation(vehicleLocationId, undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result)=> {
      this.vehicleLocation.id = result.items[0]['id'];
              

              this._locationService.getLocation(result.items[0]['locationId'], undefined, undefined, undefined, undefined).subscribe((resultLocations)=> { 
                      this.txtLocation = resultLocations.items[0];
                      this.vehicleLocation.locationId = resultLocations.items[0]['id'];
                      this.vehicleLocation.vehicleId = vehicleId;
                      console.log(this.vehicleLocation);
              });
      
      this.modal.show();
    });

  }

  onShown(): void {
   // this.nameInput.nativeElement.focus();
   let maxcount = 1000;
   this.showAllLocation(maxcount);
   $('.kt-select2').select2();
   
  }

  showAllLocation(maxcount): void {
    this.active = true;

    this._locationService.getLocation(undefined, undefined, undefined, maxcount, undefined).subscribe((result) => {
        this.locations = result.items;
        //this.proximity.locationId = result.items[0]['id'];
        //console.log(this.proximity.locationId);

    });

  }

 


  save(): void {
    this.saving = true;
    //this.vehicleLocation.locationId = this.txtLocation.id;
    this.vehicleLocation.locationId = Number((<HTMLInputElement>document.getElementById('txtLocation')).value);
    this._vehicleLocationService.updateVehicleLocation(this.vehicleLocation)
      .subscribe(() => {
        //console.log(this.vehicleLocation);
        this.notify.info(this.l('SavedSuccessfully'));
        console.log(this.vehicleLocation);
        this.close();
        this.modalSave.emit(this.vehicleLocation);
      });
    this.saving = false;
  }

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