import { Component, OnInit, Inject } from '@angular/core';
import { MdDialogRef, MD_DIALOG_DATA } from '@angular/material';
import { ContactService } from '../../../contact.service';

@Component({
  selector: 'app-acreport-info',
  templateUrl: './acreport-info.component.html',
  styleUrls: ['./acreport-info.component.scss']
})
export class AcreportInfoComponent implements OnInit {
  useridd: any;
  Fromdate: any;
  todate: any;
  vehimei: any;
  Load : boolean = false;
  errorMsg: string;

  constructor(public dialogRef: MdDialogRef<AcreportInfoComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) {

      console.log('data=>',data);
      console.log('data.fromDate',data.deviceInfo.fromDate);
      console.log('data.toDate',data.deviceInfo.toDate);
      this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
      this.Fromdate = new Date(data.deviceInfo.fromDate);
      this.todate = new Date(data.deviceInfo.toDate);
      this.vehimei = data.deviceInfo.imei;
      console.log(data.deviceInfo);
      console.log(this.vehimei);
      
     }

  ngOnInit() {
    this.ac_report();
  }


   
tableArr =[];
showError:boolean = false;
ac_report(){
  this.Load = true;
  this.contactService.acReport(new Date(this.Fromdate),new Date(this.todate),this.useridd,this.vehimei).subscribe(res=>{
    this.Load = false;
    var acObject = res;
    console.log(acObject);
    if(res.length != 0){
      for(var i = 0;i<res.length;i++){
        var veh =res[i]._id;
        for(var j = 0;j<res[i].s.length;j++){  
        var tableObj = {vehicleName:veh,action:res[i].s[j].switch,actionTime : res[i].s[j].timestamp}
        this.tableArr.push(tableObj);    
        }
  
      }
    }else{
      console.log('no data found') ;
      

    }

    console.log(this.tableArr);
  },err=>{
    // this.Load = false;
    this.showError = true;
    this.errorMsg = "Unable to fetch detail , please try after sometime";
  })

}

}
