import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { CreateEmailModalComponent } from './create-email-modal.component';
// import { UpdateAddressTypeModalComponent } from './edit-address-type-modal.component';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { EmailListDto, EmailServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
    templateUrl: './email.component.html',
    // styleUrls: ['./.udcomponent.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class EmailComponent extends AppComponentBase {

    @ViewChild('createEmailModal', {static: false}) createEmailModal: CreateEmailModalComponent;
    // @ViewChild('updateAddressTypeModal') updateAddressTypeModal: UpdateAddressTypeModalComponent;
    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;

    //Filters
    filters :{
        id: number;
        value: string;
        contactId:number;
        currentPage:number;
    } = <any>{};

     

    constructor(
        injector: Injector,
        private _emailService: EmailServiceProxy
    ) {
        super(injector);
    }

    getEmails(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }

        this.spinnerService.show();

        this._emailService.getEmailList(
            this.filters.id,
            this.filters.contactId,
            this.filters.value,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).subscribe((result) => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.spinnerService.hide();
        });
    }

    createEmail(): void {
        this.createEmailModal.show();
    }

    // reloadPage(): void {
    //     this.paginator.changePage(this.paginator.getPage());
    // }

    // deleteAddressType(addressType: AddressTypeListDto): void {
    //     this.message.confirm(
    //         this.l('DeleteAddressTypeWarningMessage'),
    //         isConfirmed => {
    //             if (isConfirmed) {
    //                 this._addressTypeService.deleteAddressType(addressType.id).subscribe(() => {
    //                     this.reloadPage();
    //                     this.notify.info(this.l('SuccessfullyDeleted'));
                        
    //                 });
                    
    //             }
    //         }
    //       );
    // }
}
