import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { VehicleServiceProxy, CreateVehicleInput, UserServiceProxy,RoleServiceProxy,
        DriversServiceProxy, UserListRoleDto, UserListDto } from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
declare var $: any;
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'createVehicleModal',
    templateUrl: './create-vehicle-modal.component.html'
})

export class CreateVehicleModalComponent extends AppComponentBase {

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

    @ViewChild('modal', {static: false}) public modal: ModalDirective;

    users: UserListDto[] = [];
    id:any;

    userInput: {
        id:number
    }

    active: boolean = false;
    saving: boolean = false;
    vehicle: CreateVehicleInput = new CreateVehicleInput();

    constructor(
        injector: Injector,
        private _vehicleService: VehicleServiceProxy,
        private _driverService: DriversServiceProxy,
        
    ) {
        super(injector);
    }
    
    show(): void {
        let maxcount = 1000;
        this.active = true;
        this.init();

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

    onShown(): void {
  
        this.vehicle.name = null;
        this.vehicle.licenseNumber = null
        $('#driver').select2();
    }

    init(): void {

    }

    save(): void {
        this.saving = true;  
       // this.vehicle.userId =  Number((<HTMLInputElement>document.getElementById('driver')).value);
        console.log(this.vehicle.userId);
        this.vehicle.userId == 0 ? NaN : Number((<HTMLInputElement>document.getElementById('driver')).value);

        console.log(this.vehicle.userId);
        this._vehicleService.createVehicle(this.vehicle)
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(this.vehicle);
            });
        this.saving = false;
    }

    close(): void {
        this.modal.hide();
        this.active = false;
    }
  
}
