import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    ProximityServiceProxy, CreateProximityInput
} 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';




@Component({
    selector: 'createProximityModal',
    templateUrl: './create-proximity-modal.component.html'
})
export class CreateProximityModalComponent extends AppComponentBase {

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

    locations: LocationListDto[] = [];

    addressTypes: AddressTypeListDto[] = [];

    locationInput: {
      id: number;
      
    };

    addressTypeInput: {
      id: number;
      
         
    };

    active: boolean = false;
    saving: boolean = false;
    proximity: CreateProximityInput = new CreateProximityInput();
    
    constructor(
        injector: Injector,
        private _proximityService: ProximityServiceProxy,
        private _locationService: LocationServiceProxy,
        private _addressTypeService: AddressTypeServiceProxy
        
    ) {
        super(injector);
    }

    show() {
        this.active = true;
        this.init();
        
        

        this.modal.show();
    }

    onShown(): void {
        //document.getElementById('valueInput').focus();
        let maxcount = 1000;
        this.proximity.value = null;
// this.proximity.locationId = null;

        this.proximity.addressTypeId = null;
        this.showAllAddressType(maxcount);
        this.showAllLocation(maxcount);
        
        $('.kt-select2').select2();
        $('#txtLocation').select2();
        
        
    }

    init(): void {
        
    }

    save(): void {
       
        this.saving = true;

        // this.proximity.locationId = Number((<HTMLInputElement>document.getElementById('locationInput')).value);

        this.proximity.addressTypeId = Number((<HTMLInputElement>document.getElementById('addressTypeInput')).value);

        //this.proximity.addressTypeId = jquery('#addressTypeInput').select2().value;
        this._proximityService.createProximity(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.active = false;
        this.modal.hide();

    }

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

      this._locationService.getLocation(undefined, undefined, undefined, maxcount, undefined).subscribe((result) => {
          this.locations = result.items;
          console.log(this.locations);


      });

    }

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

      this._addressTypeService.getAddressType(undefined, undefined, undefined, maxcount, undefined).subscribe((result) => {
          this.addressTypes = result.items;
          console.log(this.addressTypes);

      });

    }

}
