import { Component, OnInit } from '@angular/core';
import { ContactService } from '../contact.service';
import { FlashMessagesService } from 'angular2-flash-messages';
import { Router } from '@angular/router';

@Component({
  selector: 'app-edit-vehicle-type',
  templateUrl: './edit-vehicle-type.component.html',
  styleUrls: ['./edit-vehicle-type.component.scss']
})
export class EditVehicleTypeComponent implements OnInit {

  brand:any="";
  description:any;
  model:any;
  iconObj:any = {
    value : '',
    viewValue : ''
  };
  tank_size:any;
  voltage_calibration=[];
  mileage:any;
  IconType = [
    { value: 'car', viewValue: 'Car' },
    { value: 'bike', viewValue: 'Bike' },
    { value: 'bus', viewValue: 'Bus' },
    { value: 'pickup', viewValue: 'Pickup' },
    { value: 'tractor', viewValue: 'Tractor' },
    { value: 'truck', viewValue: 'Truck' },
    { value: 'truck', viewValue: 'Heavy Truck' },
    { value: 'tanker', viewValue: 'Tanker' },
    { value: 'jcb', viewValue: 'JCB' },
    { value: 'roadroller', viewValue: 'Roadroller' },
    { value : 'user' ,viewValue : 'User'}
  ];

  
  vehId: any;
  editvehTypeObj:any;
  IconType1: any;
  setTimeOut: any;
  savedInt = 10;
  useridd: any;
  vehtypeID: any;

  constructor(public contactService: ContactService, public _flashMessagesService : FlashMessagesService,public router:Router) { }

  ngOnInit() {
  this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;

  this.editvehTypeObj =  JSON.parse(localStorage.getItem('vehType')); 
  this.vehtypeID = this.editvehTypeObj._id;
  console.log("EditVehType=>",this.editvehTypeObj);
  this.brand=this.editvehTypeObj.brand;
  this.description=this.editvehTypeObj.description;
  this.model=this.editvehTypeObj.model;
 // this.IconType1= this.editvehTypeObj.iconType;
  this.tank_size = this.editvehTypeObj.tank_size;
//  this.getIconTypeObj(this.editvehTypeObj.iconType);
    this.iconObj.value = this.editvehTypeObj.iconType;
   
  // this.voltage_calibration=[];

  // if(this.IconType1 == 'car'){
  

  // }
  for(var i = 0 ;i<this.editvehTypeObj.voltage_calibration.length;i++){

          this.voltage_calibration.push(this.editvehTypeObj.voltage_calibration[i]);

          console.log(this.voltage_calibration);
  }
  console.log(this.voltage_calibration);
  this.mileage = this.editvehTypeObj.mileage;


   
  }

  click1(event){
    if((event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 9 || event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39){
      
            }
            else
              event.preventDefault();
          }

  addRow(voltage,fuel){
    var obj={};
    //let contact =  this.voltage_calibration({voltage:0,fuel:0});
    obj={voltage:voltage,fuel:fuel};
    this.voltage_calibration.push(JSON.parse(JSON.stringify(obj)));
  }


  deleteRow(index){
    console.log(index);
    this.voltage_calibration.splice(index, 1);
    console.log("delete voltage index = >",this.voltage_calibration);
  }

  editVehicleType(signUp_form){
    // console.log(signUp_form.value);
    // console.log("Voltage Calibration =>",this.voltage_calibration);
    var voltage_calibration_arr =[];
        for(var i = 0 ;i<this.voltage_calibration.length;i++ ){
          var voltage_calibration_obj = {
            fuel:this.voltage_calibration[i].fuel,
            voltage: this.voltage_calibration[i].voltage
          }
           voltage_calibration_arr.push(voltage_calibration_obj);
        }
        console.log(voltage_calibration_arr);
    var updateVehType = { 
      "_id": this.vehtypeID,
      "brand": signUp_form.value.brand1,
      "description": signUp_form.value.description1,
      "model": signUp_form.value.model1,
      "voltage_calibration": voltage_calibration_arr,
      "tank_size": signUp_form.value.tank_size1,
      "mileage": signUp_form.value.mileage1,
      "iconType": signUp_form.value.neww,
    }
    this.contactService.updateVehType(updateVehType)
      .subscribe(res=>{
        console.log(res);
    //    this._flashMessagesService.show("Vehicletype  configuratiuon changed", { cssClass: 'alert-success', timeout: 2000 });  
        this.router.navigateByUrl("VehicleType");
      },err=>{
        console.log(err);
        this._flashMessagesService.show("Values couldn't changed , Please try after sometime!!!", { cssClass: 'alert-danger', timeout: 2000 });  
      })

    console.log(updateVehType);

  
  }


}
