import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    UserLocationServiceProxy, UpdateUserLocationInput, UserLocationListDto
} 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: 'editUserLocationModal',
    templateUrl: './edit-user-location-modal.component.html'
})
export class EditUserLocationModalComponent extends AppComponentBase {

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

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

  userLocation: UpdateUserLocationInput = new UpdateUserLocationInput();

  locations: LocationListDto[] = [];

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

  

  txtLocation: any;

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

  constructor(
    injector: Injector,
    private _userLocationService: UserLocationServiceProxy,
    private _locationService: LocationServiceProxy,
        
  ) {
    super(injector);
  }

  show(userLocationId: any, userId: any): void {
    this.active = true;
    let maxcount = 1000;
    this._userLocationService.getUserLocation(userLocationId, undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result)=> {
    this.userLocation.id = result.items[0]['id'];
              

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

  }

  onShown(): void {
   // this.nameInput.nativeElement.focus();
   $('.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.userLocation.locationId = this.txtLocation.id;
    this.userLocation.locationId = Number((<HTMLInputElement>document.getElementById('txtLocation')).value);
    this._userLocationService.updateUserLocation(this.userLocation)
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        console.log(this.userLocation);
        this.close();
        this.modalSave.emit(this.userLocation);
      });
    this.saving = false;
  }

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