import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    ProximityServiceProxy, UpdateProximityInput, ProximityListDto
} from '@shared/service-proxies/service-proxies';
import { LocationListDto, LocationServiceProxy } from '@shared/service-proxies/service-proxies';
import { AddressTypeListDto, AddressTypeServiceProxy } from '@shared/service-proxies/service-proxies';

import * as _ from 'lodash';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
declare var $: any;

@Component({
    selector: 'editProximityModal',
    templateUrl: './edit-proximity-modal.component.html'
})
export class EditProximityModalComponent extends AppComponentBase {

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

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

  proximity: UpdateProximityInput = new UpdateProximityInput();

  locations: LocationListDto[] = [];

  addressTypes: AddressTypeListDto[] = [];
  
  
  locationInput: {
    id: number;
    name: string;
  };

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

  txtAddressType: any;
  txtLocation: any;

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

  constructor(
    injector: Injector,
    private _proximityService: ProximityServiceProxy,
    private _locationService: LocationServiceProxy,
    private _addressTypeService: AddressTypeServiceProxy
        
  ) {
    super(injector);
  }

  show(proximityId: any): void {
    this.active = true;

    this._proximityService.getProximity(proximityId, undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result)=> {
      this.proximity.value = result.items[0]['value'];
      this.proximity.id = result.items[0]['id'];
      //this.addressTypeInput.id = result.items[0]['addressTypeId'];
      //this.addressTypeInput = result.items[0];
      //this.txtAddressType = result.items[0]['addressType']['name'];
      this.txtAddressType = result.items[0]['addressTypeId'];
      this.txtLocation = result.items[0]['locationId'];
      this.modal.show();
              




      
    });

  }

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

   this.showAllLocation(maxcount);
   this.showAllAddressType(maxcount);
                              
   
  }

  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);

    });

  }

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

    this._addressTypeService.getAddressType(undefined, undefined, undefined, maxcount, undefined).subscribe((result) => {
        this.addressTypes = result.items;
        //this.proximity.addressTypeId = result.items[0]['id'];
        //this.txtAddressType = result.items[0]['name'];
        //console.log(this.proximity.addressTypeId);

    });

  }


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

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