import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, Output ,Input} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ControllerRouteDetailServiceProxy,ShippingPackageServiceProxy,ControllerScheduleDetailDto,GetCompaniesListItemDto,OrderPackageListDto} from '@shared/service-proxies/service-proxies';
import { AppSessionService } from '@shared/common/session/app-session.service';
import { ClinicNotes } from './clinic-notes.component';
import { Table } from "primeng/table";
import { LazyLoadEvent } from "primeng/primeng";
import { Paginator } from 'primeng/components/paginator/paginator';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
declare var $: any;
declare var jquery:any;
@Component({
    selector:'lab-route-detail',
    templateUrl: 'lab-route-detail.component.html',
    encapsulation: ViewEncapsulation.None,
    styleUrls: ['./route-detail.component.less'],
    animations: [appModuleAnimation()]
})

export class LabRouteDetailComponent extends AppComponentBase implements OnInit {

    routeId:number = null;
    packages: Array<{id: number, name: string}> = new Array();
    data:any;
    packageCount: Array<{id: number, quantity: number}> = new Array();
    filterText = '';
    // data: Array<ControllerOrderDetailsAntechDto> [];
    @ViewChild("dataTable", {static: true}) dataTable:Table
    @ViewChild("paginator", {static: true}) paginator: Paginator
    @ViewChild('clinicNotes', { static: true }) clinicNotes: ClinicNotes;
    @Input() labRouteDetails : any;

    newDto : ControllerScheduleDetailDto = new ControllerScheduleDetailDto();
    newPackage :OrderPackageListDto = new OrderPackageListDto();
   
    packagesList:Array<OrderPackageListDto> = new Array();

	constructor(
        injector: Injector,
        private id:ActivatedRoute,
        private _controllerRouteDetails: ControllerRouteDetailServiceProxy,
        private _controllerRoutePackages: ShippingPackageServiceProxy,
        private _appSessionService :AppSessionService,
        
        
    ) {
        super(injector);
    }

    ngOnInit(){
        this.id.paramMap.subscribe(params => {
            this.routeId = Number(params.get('id'));
        });
    }

    getLabPackages(){
        this.packages = [];
        this._controllerRoutePackages.getShippingPackage(undefined,undefined,undefined,undefined,undefined,undefined).subscribe(result=>{

            result.items.forEach(element => {
                if(element.name !== 'Default'){
                    this.packages.push(element);
                }
            });
        });
    }

    getTotal(array){
        var data = array.map(i=>i.quantity);

        data.reduce((a, b) => a + b, 0);

        return data;
    }
    getRouteDetails(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.getLabPackages();
        this.spinnerService.show();

        this._controllerRouteDetails.getRouteSchedDetails(
            undefined,
            this.routeId,
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
        .pipe(finalize(() => this.spinnerService.hide() ))
        .subscribe(result => {
            for (let index = 0; index <  result.items.length; index++) {

                for (let key = 0; key < result.items[index].orderPackages.length; key++) {
                    if(result.items[index].orderPackages[key].package.name != "Default"){
                        result.items[index].packageCount += result.items[index].orderPackages[key].quantity;
                    }
                    
                }
                
            }
            result.items.push(this.newDto);
            var lastKey = result.items.length - 1;
            result.items[lastKey].id = 1;
            result.items[lastKey].action = "Delivery";
            result.items[lastKey].company = new GetCompaniesListItemDto();
            result.items[lastKey].company.name = result.items[0].deliveryDetail.contact.company.name;
            result.items[lastKey].company.accountNo = result.items[0].deliveryDetail.contact.company.name;
            result.items[lastKey].pickupDetail = result.items[0].deliveryDetail;
            result.items[lastKey].deliveryDetail = result.items[0].deliveryDetail;
            result.items[lastKey].orderStatus = null;
            result.items[lastKey].willCall = null;
            result.items[lastKey].automatic = null;
            result.items[lastKey].orderPackages = null;
            result.items[lastKey].completion = result.items[0].completion;
            result.items[lastKey].orderExtras = result.items[0].orderExtras;
            this.primengTableHelper.totalRecordsCount = result.totalCount + 1;
            this.primengTableHelper.records = result.items;
        });
    }

    toggleMoreInfo(event){
        var element = event.currentTarget.nextSibling;
        var selector = document.querySelectorAll('.more-info-container.active');
        var hover = document.querySelectorAll('.more-btn.hover');

        if(element.classList.contains('active')){
            event.currentTarget.classList.remove('hover');
            element.classList.remove('active');
        } else {
            if(selector.length > 0){
                selector[0].classList.remove('active');
                hover[0].classList.remove('hover');
                // event.currentTarget.classList.remove('hover');
            } 
            element.classList.add('active');
            event.currentTarget.classList.add('hover');
        }

    }
    ConvertToInt(val){
        return parseInt(val);
    }

    onShown(){
        
    }


}