import { Component, EventEmitter, Injector, ViewChild, Output, AfterViewInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateAddressInput, PostalCodeServiceProxy, AddressServiceProxy,AddressTypeServiceProxy,AddressTypeListDto} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';
import { finalize } from 'rxjs/operators';
import { AddressFormComponent } from '@app/shared/layout/form/address-form.component';

@Component({
    templateUrl: './create-address-modal.component.html',
    selector: 'createAddressModal',
})
export class CreateAddressModalComponent extends AppComponentBase implements AfterViewInit{

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

    active = false;
    saving = false;
    address:CreateAddressInput = new CreateAddressInput();
    country: any;
    filteredCountries: any;
    city: any;
    filteredCities: any;
    state: any;
    filteredStates: any;
    postal: any;
    filteredPostal: any;
    addressType:any;
    addressTypes:AddressTypeListDto[];
    filteredAddressTypes: any;
     
    
    constructor(
        injector: Injector,
        private _addressService: AddressServiceProxy,

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

        private _postalCodeService: PostalCodeServiceProxy,
        private _addressTypeService: AddressTypeServiceProxy
    ) {
        super(injector);
       
    }

    ngAfterViewInit() {
    }

    show() {
        this.active = true;
        this.modal.show();
        
    }

    onShown(): void {
        this.address = this.addressForm.address;
        document.getElementById('address1Input').focus();
        this.address.addressLine1 = null;
        this.address.addressLine2 = null;
    }

    save(): void {
       
        this.address.addressTypeId = Number((<HTMLInputElement>document.getElementById('addressTypeSelectInput')).value);
        this.address.postalCodeId = Number((<HTMLInputElement>document.getElementById('postalSelectInput')).value);

        // for discussion
        this.address.geocodeId = null;
        
        this.saving = true;
        this._addressService.createAddress(this.address)
            .pipe(finalize(() => this.saving = false))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

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

    // filterPostal(maxcount): void {
    //     this._postalCodeService.getPostalCode(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredPostal = result.items;

    //     });
    //   }
  
    //   filterCountries(maxcount): void {
    //     this._countryService.getCountry(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredCountries = result.items;
    //     });
    //   }
  
    //   filterCities(maxcount): void {
    //     this._cityService.getCity(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredCities = result.items;
    //     });
    //   }
  
    //   filterStates(maxcount): void {
    //     this._stateService.getState(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredStates = result.items;
    //     });
    //   }

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

      

     
}       

