import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, Input, TemplateRef } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ControllerRouteDetailServiceProxy,UpdateRouteInput,RouteServiceProxy} from '@shared/service-proxies/service-proxies';
import { RouteDetailModal } from './route-detail-modal.component';
import { AppSessionService } from '@shared/common/session/app-session.service';
import * as moment from 'moment';
import { Table } from "primeng/table";
import { LazyLoadEvent } from "primeng/primeng";
import { Paginator } from 'primeng/components/paginator/paginator';
import { HttpClient } from '@angular/common/http';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
declare var $: any;
declare var jquery:any;
@Component({
    templateUrl: 'route-detail.component.html',
    encapsulation: ViewEncapsulation.None,
    styleUrls: ['./route-detail.component.less'],
    animations: [appModuleAnimation()],
    selector:'routeDetailComponent'
})

export class RouteDetailComponent extends AppComponentBase implements OnInit {
    saving = false;
    routeId:number = null;
    details:boolean = false;
    _isLab:boolean = false;
    routeCode:string;
    vehicleName:string;
    loadDate:Date;
    locationName:string;
    // completion:any;
    route: UpdateRouteInput = new UpdateRouteInput();
    
    @ViewChild('routeDetailModal', { static: true }) routeDetailModal: RouteDetailModal;
    @Input() hfRouteDetails : TemplateRef<any>;
    @Input() labRouteDetails : any;
    @ViewChild("dataTable", {static: true}) dataTable:Table
    @ViewChild("paginator", {static: true}) paginator: Paginator
    // locations: LocationListDto[] = [];
	constructor(
        injector: Injector,
        private id:ActivatedRoute,
        private _controllerRouteDetails: ControllerRouteDetailServiceProxy,
        private _controllerRoute: RouteServiceProxy,
        private _appSessionService :AppSessionService,
        // private _routeService: RouteServiceProxy
        
    ) {
        super(injector);
    }

    ngOnInit(){
        this._isLab = abp.features.isEnabled('App.RouteDetails');
        this.id.paramMap.subscribe(params => {
            this.routeId = Number(params.get('id'));
        });

        this.getDetails();
        
    }

    getDetails(){
        this._controllerRouteDetails.getControllerRouteDetail(this.routeId)
        .subscribe(result=>{
            this.routeCode = result.routeTemplate.name;
            this.route.vehicleId = result.vehicleId;
            this.route.completion = result.completion;
            this.route.departure = result.departure;
            this.route.loadDate = result.loadDate;
            this.route.locked = result.locked;
            this.route.routeTemplateId = result.routeTemplateId;
            this.route.locationId = result.locationId;
            this.route.id = this.routeId;
            this.locationName = result.location.name;
            if(result.vehicle){
                this.vehicleName = result.vehicle.name;
            }
            this.loadDate = moment(result.loadDate).toDate();
            this.labRouteDetails = result.routeTemplate.name;
        });
    }
    
    updateRoute(){
        this._controllerRoute.updateRoute(this.route)
            .pipe(finalize(() => this.saving = false))
            .subscribe(() => { 
                this.notify.info(this.l('SavedSuccessfully'));
        });
    }

    setDeparture(){
        if(!this.route.locked){
            if(this.route.departure){
                this.route.departure = null;
            } else {
                this.route.departure = moment.utc(moment(new Date()).format('YYYY-MM-DD h:m a'));
            }
            this.updateRoute();
        } else {
            this.notify.info(this.l('Unlocked Loadlist First'));
        }
       
       
    }

    setCompletion(){
        if(!this.route.locked){
            if(this.route.completion){
                this.route.completion = null;
            } else {
                this.route.completion = moment.utc(moment(new Date()).format('YYYY-MM-DD h:m a'));
            }
            this.updateRoute();
        } else {
            this.notify.info(this.l('Unlocked Loadlist First'));
        }
        
    }

    ngAfterViewInit(){

        $('.kt-select2').select2();
        $('.selectpicker').selectpicker();
    }

    onShown(){
        
    }


}