import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { ClientPreferencesServiceProxy, CreateOrEditClientPreferenceDto, PreferencesServiceProxy } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as moment from 'moment';


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

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


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

    active = false;
    saving = false;

    clientPreference: CreateOrEditClientPreferenceDto = new CreateOrEditClientPreferenceDto();

    filteredPreferences: any;


    constructor(
        injector: Injector,
        private _clientPreferencesServiceProxy: ClientPreferencesServiceProxy,
        private _preferencesServiceProxy: PreferencesServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(){
        this.filterUserPreferences();
        $('.kt-select2').select2({ 
            placeholder: "Select.."
        });
    }

    show(clientPreferenceId?: number): void {

        if (!clientPreferenceId) {
            this.clientPreference = new CreateOrEditClientPreferenceDto();
            this.clientPreference.id = clientPreferenceId;

            this.active = true; 
            this.modal.show();
        } else {
            this._clientPreferencesServiceProxy.getClientPreferenceForEdit(clientPreferenceId).subscribe(result => {
                this.clientPreference = result.clientPreference;

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

    save(): void {
            this.saving = true;
            this.clientPreference.preferenceId =  Number((<HTMLInputElement>document.getElementById('selectedPreference')).value) ? Number((<HTMLInputElement>document.getElementById('selectedPreference')).value) : null;
            this._clientPreferencesServiceProxy.createOrEdit(this.clientPreference)
             .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();
    }

    filterUserPreferences(): void {
        this._preferencesServiceProxy.getAll(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredPreferences = result.items;
            console.log(this.filteredPreferences);
        });
      }
}
