import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { RouteDefinitionsServiceProxy, CreateOrEditRouteDefinitionDto, RouteTypesServiceProxy, RouteDefinitionRouteTypeLookupTableDto, ContactServiceProxy, ContactListDto, LocationServiceProxy, LocationListDto, RouteTemplateTypeServiceProxy, RouteTemplateTypeListDto, UserServiceProxy, RoleServiceProxy, DriversServiceProxy, UserListRoleDto, UserListDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as moment from 'moment';
import { RouteDefinitionRouteTypeLookupTableModalComponent } from './routeDefinition-routeType-lookup-table-modal.component';
declare var KTWizard: any;
declare var $: any;
declare var KTApp: any;

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

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

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

    active = false;
    saving = false;

    routeDefinition: CreateOrEditRouteDefinitionDto = new CreateOrEditRouteDefinitionDto();
    locations: LocationListDto[] = [];
    templateTypes: RouteTemplateTypeListDto[] = [];
    users: UserListDto[] = [];
    contacts: ContactListDto[] = [];
    routeTypes: any;

    routeTypeRouteTypeCode = '';
    routeTemplateName = '';
    currentDateTime: Date;
    eta : any;
    cutOff: any;


    constructor(
        injector: Injector,
        private _routeDefinitionsServiceProxy: RouteDefinitionsServiceProxy,
        private _contactServiceProxy: ContactServiceProxy,
        private _driverService: DriversServiceProxy,
        private _locationServiceProxy: LocationServiceProxy,
        private _routeTemplateTypeServiceProxy: RouteTemplateTypeServiceProxy,

    ) {
        super(injector);

    }

    show(routeDefinitionId?: number): void {
         
            
       

        if (!routeDefinitionId) {
            this.routeDefinition = new CreateOrEditRouteDefinitionDto();
            this.getAllDropDown();
            this.routeDefinition.id = routeDefinitionId;
            this.routeTypeRouteTypeCode = '';
            this.routeTemplateName = '';
            this.eta = '';            
           
                     

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

            this._routeDefinitionsServiceProxy.getRouteDefinitionForEdit(routeDefinitionId).subscribe(result => {
                
                this.getAllDropDown();
                

                this.routeDefinition = result.routeDefinition;
                //this.routeDefinition.routeCode = result.routeTypeRouteTypeCode;
                this.routeDefinition.routeCode = result.routeTemplateName;
                //this.routeDefinition.eta = this.routeDefinition.eta;
            
                this.eta = result.routeDefinition.eta.toDate();
                this.cutOff = moment(moment(this.currentDateTime).format('ddd MMM DD YYYY') + ' ' + result.routeDefinition.cutOff + ':00').toDate();
                //this.routeDefinition.eta = this.eta;
                this.routeDefinition.cutOff = this.cutOff;
                //console.log(this.eta); 
                
                this.active = true;
                this.modal.show();
            });
        }

            
        
    }

    save(): void {
            this.saving = true;
            //this.currentDateTime = moment(moment().toISOString()).toDate();
            this.routeDefinition.defaultContactId = Number((<HTMLInputElement>document.getElementById('routeStop')).value);
            this.routeDefinition.defaultUserId = Number((<HTMLInputElement>document.getElementById('driver')).value);
            this.routeDefinition.locationId = Number((<HTMLInputElement>document.getElementById('txtLocation')).value);
            this.routeDefinition.routeTemplateTypeId = 1;
            this.routeDefinition.routeTypeId = Number((<HTMLInputElement>document.getElementById('routeType')).value);
            //this.eta = moment(moment(this.routeDefinition.eta).toISOString()).toDate();
            this.routeDefinition.eta = moment.utc(moment(this.currentDateTime).format('YYYY-MM-DD') + ' ' + moment(this.eta).format('HH:mm:ss'));
            this.routeDefinition.cutOff =  moment(this.routeDefinition.cutOff).format('HH:mm:ss');
            console.log(this.routeDefinition.eta);
            this._routeDefinitionsServiceProxy.createOrEdit(this.routeDefinition)
                         .pipe(finalize(() => { this.saving = false;}))
                         .subscribe(() => {
                            this.notify.info(this.l('SavedSuccessfully'));
                            this.close();
                            this.modalSave.emit(null);
            });
    }

        openSelectRouteTypeModal() {
        this.routeDefinitionRouteTypeLookupTableModal.id = this.routeDefinition.routeTypeId;
        this.routeDefinitionRouteTypeLookupTableModal.displayName = this.routeTypeRouteTypeCode;
        this.routeDefinitionRouteTypeLookupTableModal.show();
    }
    


        setRouteTypeIdNull() {
        this.routeDefinition.routeTypeId = null;
        this.routeTypeRouteTypeCode = '';
    }
        setRouteTemplateIdNull() {
        this.routeDefinition.routeTemplateId = null;
        this.routeTemplateName = '';
    }


        getNewRouteTypeId() {
        this.routeDefinition.routeTypeId = this.routeDefinitionRouteTypeLookupTableModal.id;
        this.routeTypeRouteTypeCode = this.routeDefinitionRouteTypeLookupTableModal.displayName;
    }
    


        getAllDropDown() {
            jQuery(document).ready(function () { 
                $('.kt-select2').select2();
                $('.select2-container').css('width', '100%');

            });
            

            let maxcount = 1000;
            let that = this;
            this._contactServiceProxy.getContactAddress().subscribe(result => {
                    this.contacts = result.items;
            });

            this._driverService.getDrivers(undefined, undefined, undefined, undefined,undefined,
                maxcount,undefined).subscribe((result) => {
                    this.users = result.items;
            });

            this._locationServiceProxy.getLocation(undefined, undefined, undefined, undefined, undefined, undefined, undefined)
                .subscribe(result => {
                    that.locations = result.items;
            });

            this._routeDefinitionsServiceProxy.getAllRouteTypeForLookupTable(
                undefined,
                undefined,
                undefined,
                maxcount
            ).subscribe(result => {
                
                that.routeTypes = result.items;


            });
        }

      


    close(): void {

        this.active = false;
        this.modal.hide();
    }
}
