import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { ClinicsServiceProxy, CreateOrEditClinicDto } from '@shared/service-proxies/service-proxies';
import { AddressServiceProxy, CreateAddressInput } from '@shared/service-proxies/service-proxies';
import { EmailServiceProxy, CreateEmailInput } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as moment from 'moment';
import { AddressFormComponent } from '@app/shared/layout/form/address-form.component';
import { AddressFormMainComponent } from '../../../shared/layout/formMain/address-form.component';
declare var KTWizard: any;
declare var $: any;
declare var KTApp: any;


@Component({
    selector: 'createOrEditClinicModal',
    templateUrl: './create-or-edit-clinic-modal.component.html'
})
export class CreateOrEditClinicModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective;

    @ViewChild(AddressFormMainComponent, {static:false}) addressForm;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    @Output()iconChange = new EventEmitter<boolean>();

    active = false;
    saving = false;

    options: {
        startStep: 1,
        manualStepForward: false
    };

    clinic: CreateOrEditClinicDto = new CreateOrEditClinicDto();
    address: CreateAddressInput = new CreateAddressInput();


    constructor(
        injector: Injector,
        private _clinicsServiceProxy: ClinicsServiceProxy,
        private _addressService: AddressServiceProxy,
        private _emailService: EmailServiceProxy,
    ) {
        super(injector);

    }

    show(clinicId?: number): void {
         $(document).ready(() => {
                    //ktwizard('kt_wizard_v2', this.options);
                     this.KTWizard2();
                    
                    $("#visibleToDriver").text("No");
                    $("#Clinic_ClinicNotesVisibleToDriver").click(function(){
                             //var check = $("input[type='checkbox']").is(":checked");
                             if ($('#Clinic_ClinicNotesVisibleToDriver').is(":checked"))
                             {
                             $("#visibleToDriver").text("Yes");
                             }
                             else {
                             $("#visibleToDriver").text("No");
                             }
                             
                             
                    }); 


                    $("#addressTypeSelectInput").change(function(){
                        this.selectedAddressType = $("#addressTypeSelectInput option:selected").text();
                        $("#selectedAddressType").text(this.selectedAddressType);
                    });

                    $("#countrySelectInput").change(function(){
                            this.selectedCountry = $("#countrySelectInput option:selected").text();
                            $("#selectedCountryContacts").text(this.selectedCountry);
                    });

                    $("#postalSelectInput").change(function(){
                            this.selectedPostalCode = $("#postalSelectInput option:selected").text();
                            $("#selectedPostalCodeContacts").text(this.selectedPostalCode);       
                    });
                    
                    $("#stateSelectInput").change(function(){
                            this.selectedState = $("#stateSelectInput option:selected").text();
                            $("#selectedStateContacts").text(this.selectedState);
                    });
                    $("#citySelectInput").change(function(){
                            this.selectedCity = $("#citySelectInput option:selected").text();
                            $("#selectedCityContacts").text(this.selectedCity);
                    });
                     
        });
        if (!clinicId) {
            this.clinic = new CreateOrEditClinicDto();
            this.clinic.id = clinicId;

            this.active = true;
            this.modal.show();
        } else {
            this._clinicsServiceProxy.getClinicForEdit(clinicId).subscribe(result => {
                this.clinic = result.clinic;
                this.address = result.clinic.address;
                console.log(result);
                this.active = true;
                this.modal.show();
            });
        }

    }
    ngAfterViewInit() {
        
        //this.address = this.addressForm.address;

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

            //this.clinic.companyName = 'testAllNews';
            //this.clinic.accountNumber = 'testAll123News';
            //this.address.addressLine1 = 'testAll123News';
            //this.address.addressLine2 = 'test123News';
            //this.address.postalCodeId = 2;
            //this.address.geocodeId = 2;
            //this.address.addressTypeId = 2;
            //this.clinic.email = 'cards@email.com';

			//this.clinic.address = this.address;
 

            this._clinicsServiceProxy.createOrEdit(this.clinic)
             .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();
    }


    // Class definition
    KTWizard2(): void  {
        // Base elements
        var wizardEl;
        var formEl;
        var validator;
        var wizard;
          
        // Private functions
        var initWizard = function () {
            // Initialize form wizard
            wizard = new KTWizard('kt_wizard_v2', {
                startStep: 1,
            });

            // Change event
            wizard.on('change', function(wizard) {
                KTUtil.scrollTop();    
            });
        }

            
       

        wizardEl = KTUtil.get('kt_wizard_v2');
        formEl = $('#kt_form').serialize();
        
        return initWizard();
        //return initValidation();
        //return initSubmit();
    }   
}
