import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { CreateAddressModalComponent } from './create-address-modal.component';
import { UpdateAddressModalComponent } from './edit-address-modal.component';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { AddressListDto, AddressServiceProxy } 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';
import { AddressFormComponent } from '@app/shared/layout/form/address-form.component';

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

    @ViewChild('createAddressModal', {static: false}) createAddressModal: CreateAddressModalComponent;
    @ViewChild('updateAddressModal', {static: false}) updateAddressModal: UpdateAddressModalComponent;
    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;
    @ViewChild(AddressFormComponent, {static:false}) addressForm;

    //Filters
    filters :{
        id: number;
        name: string;
    } = <any>{};

     

    constructor(
        injector: Injector,
        private _addressService: AddressServiceProxy
    ) {
        super(injector);
    }

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

            return;
        }

        this.spinnerService.show();

        this._addressService.getAddress(
            this.filters.id,
            undefined,
            this.filters.name,
            undefined,
            undefined,
            undefined,
            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();
            console.log(result.items);
        });
    }

    createAddress(): void {
        this.createAddressModal.show();
    }

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

    deleteAddress(address: AddressListDto): void {
        this.message.confirm(
            this.l('DeleteAddressTypeWarningMessage'),
            isConfirmed => {
                if (isConfirmed) {
                    this._addressService.deleteAddress(address.id).subscribe(() => {
                        this.reloadPage();
                        this.notify.info(this.l('SuccessfullyDeleted'));
                        
                    });
                    
                }
            }
          );
    }
}
