import { Component, Injector, Input, Output, EventEmitter, OnInit, AfterViewInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateAddressInput, AddressTypeServiceProxy, PostalCodeServiceProxy, UpdateAddressInput } from '@shared/service-proxies/service-proxies';

@Component({
    selector: 'addressForm',
    templateUrl: './address-form.component.html',
    styleUrls: ['adress-form.component.less']
})
export class AddressFormMainComponent extends AppComponentBase implements OnInit {

    @Input() address: UpdateAddressInput = new UpdateAddressInput();

    constructor(
        injector: Injector,

        // private _countryService: CountryServiceProxy,
        // private _cityService: CityServiceProxy,
        // private _stateService: StateServiceProxy,

        private _addressTypeService: AddressTypeServiceProxy,
        private _postalCodeService: PostalCodeServiceProxy,
    ) {
        super(injector);
    }  
    filteredCountries: any;
    filteredCities: any;
    filteredStates: any;
    filteredPostal: any;
    filteredAddressType: any; 
    selectedPostalCode: any;

    filterChange=false

    ngOnInit() {
        var that=this;
        console.log(this.address.id);
        $('.kt-select2').select2();
        let maxcount = 1000;
        this.filterAddressTypes(maxcount);
        this.filterPostal();

        $("#postalSelectInput").change(function(){
            that.selectedPostalCode = $("#postalSelectInput option:selected").text();
            that.filterChange=true;
            that.selectedPostalCode=$.trim(that.selectedPostalCode)
            that.filterCountries(that.selectedPostalCode);
            that.filterStates(that.selectedPostalCode);
            that.filterCities(that.selectedPostalCode);
        })

        // this.filterCountries(maxcount);
        // this.filterPostal(maxcount);
        // this.filterCities(maxcount);
        // this.filterStates(maxcount);
    } 
       
    filterPostal(): void {
        this._postalCodeService.getDistinctPostalCode(
            this.selectedPostalCode
        ).subscribe(result => {
            this.filteredPostal = result.items;
        });
      }
  
      filterCountries(postalCode:string): void {
        this._postalCodeService.getDistinctCountry(
            postalCode,
            undefined
        ).subscribe(result => {
            this.filteredCountries = result.items;
            $("#selectedCountryContacts").text(this.filteredCountries[0]['country']);
            
        });
      }

         filterStates(postalCode: string): void {
        this._postalCodeService.getDistinctState(
            postalCode,
            undefined
        ).subscribe(result => {
            this.filteredStates = result.items;
            $("#selectedStateContacts").text(this.filteredStates[0]['state']);
        });
      }  
  
      filterCities(postalCode: string): void {
        this._postalCodeService.getDistinctCity(
            postalCode,
            undefined
        ).subscribe(result => {
            this.filteredCities = result.items;
            $("#selectedCityContacts").text(this.filteredCities[0]['city']);
        });
      }
  
    


      filterAddressTypes(maxcount): void {
        this._addressTypeService.getAddressType(
            undefined,
            undefined,
            undefined,
            maxcount,
            undefined
        ).subscribe(result => {
            this.filteredAddressType = result.items;
        });
      }
}
