import { Component, OnInit, EventEmitter,Output  } from '@angular/core';
import {ContactService} from '../../../contact.service';
import { Params,Router, ActivatedRoute, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { MyaccountComponent } from '../../../myaccount/myaccount.component';
import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';

@Component({
  selector: 'app-ign-report',
  templateUrl: './ign-report.component.html',
  styleUrls: ['./ign-report.component.scss'],
  providers: [ContactService]
})
export class IgnReportComponent implements OnInit {
  myaccount(){
    let dialogRef = this.dialog.open(MyaccountComponent, {
      width: '903px',
      data: {}
    });
  
    dialogRef.afterClosed().subscribe(result => {
  
      if(result == "succ"){
        // console.log("Updated")
      }
  
    });
  }
  addcus(){
    this.router.navigateByUrl("add");
  }
  geo(){
    this.router.navigateByUrl("geofencing");
    
  }

openNav() {
  /*  document.getElementById("myNav").style.width = "100%";
*/
   this.router.navigateByUrl("location");

}

logout(){
  window.localStorage.clear();
  this.router.navigateByUrl("login");
}
devi(){
  this.router.navigateByUrl("dashboard");

}
soon(){
  this.router.navigateByUrl("const?_i="+window.localStorage.token);
  
}
new(){
  this.router.navigateByUrl("new");
  
}
cond:Boolean = false;
point : any = 0;
 a(){
  // console.log(this.point);
   if(this.point == 0){
    this.cond = true;
    this.point ++;
   }
 else if(this.point % 2 == 0){
  this.cond = true;
  this.point ++;
 }
 else{
   this.cond = false
   this.point ++;
 }

  
}
report(){
  this.cond = false
  this.router.navigateByUrl("device-report");

}
report_speed(){
  // console.log("report speed call")
  this.cond = false
  this.router.navigateByUrl("device-report/device-speed-report");
}

overspeed(){
  this.cond = false
  this.router.navigateByUrl("device-report/overspeed");

}

vehicleRoute(){
  this.router.navigateByUrl("vehicleRoute");

}

dealerInfo(){
  this.cond = false
  this.router.navigateByUrl("dealerInfo");
}


geofancingReport(){
  this.cond = false
  this.router.navigateByUrl("device-report/geofancing");
}

distance_report(){
  this.cond = false
  this.router.navigateByUrl("device-report/distance_report");
}

stoppage_report(){
  this.cond = false
  this.router.navigateByUrl("device-report/stoppage_report");

}

summaryReport(){

  this.cond = false
  this.router.navigateByUrl("device-report/summary-report");
}

ignition_report(){
  this.cond = false
  this.router.navigateByUrl("device-report/ignition_report");


}

routeViolation(){
  this.cond = false
  this.router.navigateByUrl("device-report/routeViolation");

}

alert_report(){
  this.cond = false
  this.router.navigateByUrl("device-report/alert_report");
}

trip_report(){
  this.cond = false
  this.router.navigateByUrl("device-report/trip_report");

}
group(){
  this.cond = false
  this.router.navigateByUrl("group_view");
}



_lineChartData:any;
public info: string;
public prevDatasets;

options = [];
ign(){
  this.router.navigateByUrl("device-report/ign-report");

}

ideal(){
  this.router.navigateByUrl("device-report/ideal-report");
}
from:any;
to:any;
too:any;
neww:any;
before:any;
creategraph(a){
  this.too = new Date(this.date1).setHours(23,59,59,999) 
  this.to = new Date(this.too).toISOString()
  var d = new Date(this.date1);
  let aa = d.setHours(0,0,0,0)
  this.from = new Date(aa).toISOString();

  let r = new Date(this.date1).toISOString();
  // console.log(r);
  this.Load = true;
  this.contactService.ignitionGraph(a.did,this.from,this.to).subscribe(
        
    data => {
      this.Load = false;
      this.device_data = data 
     //console.log(this.device_data); 
     this.lineChartLabels.length = this.device_data.length;  
     this.getdata(this.device_data)
  
    });
  
  }
  mydealer(){
    window.localStorage['token'] =  window.localStorage['Dealer_token'];
    localStorage.removeItem('devices');
    window.localStorage['DataUpdate'] = 'True';
    if(window.localStorage['DataLoaded'] = 'True'){
      window.localStorage['Custumer'] = 'OFF'
      this.router.navigateByUrl("add")
     // this.router.navigateByUrl("const?_status="+"OK");
    }
  }
getdata(final){
  let fi = [];
  let time = [];
  this.lineChartLabels.length = 0;
  for(var q=0;q<final.length;q ++){
    fi.push(final[q].ACC);
    let t = new Date(final[q].insertionTime).toLocaleTimeString();
   // console.log(t);
    //let k = JSON.stringify(final[q].time).split("T")[1].split(".")[0];
    this.lineChartLabels.push(t)
  }
 this.Load = false;
  this.lineChartData = [{
    label: "Ignition",
    data: fi
  }];
  


}




 // lineChart
 public lineChartData:Array<any> = [
  {data: [0, 0, 0, 0, 0, 0, 0], label: 'Ignition'},
];

public lineChartLabels:Array<any> = ['','','','','','',''];
public lineChartOptions:any = {
  responsive: true,
  scales: {
    xAxes: [{
      ticks: {
        beginAtZero: false,
        max: 6000,
        min: 0
      }
    }]  
  }

};
public lineChartColors:Array<any> = [
  { // grey
    backgroundColor: 'rgba(148,159,177,0.2)',
    borderColor: 'rgba(148,159,177,1)',
    pointBackgroundColor: 'rgba(148,159,177,1)',
    pointBorderColor: '#fff',
    pointHoverBackgroundColor: '#fff',
    pointHoverBorderColor: 'rgba(148,159,177,0.8)'
  },
  { // dark grey
    backgroundColor: 'rgba(77,83,96,0.2)',
    borderColor: 'rgba(77,83,96,1)',
    pointBackgroundColor: 'rgba(77,83,96,1)',
    pointBorderColor: '#fff',
    pointHoverBackgroundColor: '#fff',
    pointHoverBorderColor: 'rgba(77,83,96,1)'
  },
  { // grey
    backgroundColor: 'rgba(148,159,177,0.2)',
    borderColor: 'rgba(148,159,177,1)',
    pointBackgroundColor: 'rgba(148,159,177,1)',
    pointBorderColor: '#fff',
    pointHoverBackgroundColor: '#fff',
    pointHoverBorderColor: 'rgba(148,159,177,0.8)'
  }
];
public lineChartLegend:boolean = true;
public lineChartType:string = 'line';



// events
public chartClicked(e:any):void {
  // console.log(e);
}

public chartHovered(e:any):void {
  // console.log(e);
}

  

  constructor(private contactService: ContactService,private router: Router,public dialog: MdDialog) { }
  fs : any;
  ls : any;
  emailid : any;
  or : any;
  useridd :any;
  time :any;
  date1:any = new Date();
  device_data:any;
  devicess:any;
  final:any;
  custtype:any;
  cust:boolean=false;
  Load : Boolean = false;
  date : any;
  devicesss:any;
  finall:any;
  mb:any;
  logoutbut:boolean;
dealer:boolean;
logo:any;
text:any;
  ngOnInit() {
    this.fs = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).fn;
    this.ls = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).ln;
    this.emailid = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).email;
    this.or = JSON.parse(window.atob(window.localStorage.token.split(".")[1]))._orgName;
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
    this.mb =  JSON.parse(window.atob(window.localStorage.token.split('.')[1])).phn;

    this.custtype =  JSON.parse(window.atob(window.localStorage.token.split('.')[1])).isDealer;
  // this.getdev();
    if(this.custtype == true){
      this.cust = true;
    }
    if(this.mb.charAt(0)=="n"){
      this.mb = ' '
    }
    if( window.localStorage['Custumer'] == 'ON'){
      this.logoutbut = false
      this.dealer = true
   }
   else{
       this.logoutbut = true
   }
  
  this.logo=window.localStorage['logo'];
  this.text=window.localStorage['text'];
    this.date = new Date
    this.contactService.getDevice(this.emailid,this.useridd).subscribe(

      data => {
        this.devicesss = data
        /*  this.final = this.devicess.devices[0]; */
        this.finall = (this.devicesss.devices);

        for(let i=0;i<this.devicesss.devices.length;i++){

          // if(this.devicesss.devices[i].type_of_device =="Tracker"){

            let a ={
              value : this.devicesss.devices[i].Device_Name,
              viewValue:this.devicesss.devices[i].Device_Name,
              did:this.devicesss.devices[i].Device_ID,
              id:this.devicesss.devices[i]._id,
              user:this.devicesss.devices[i].user,
              // email: this.devicesss.devices[i].Email_ID,
              // deviceType: this.devicesss.devices[i].type_of_device
            }
            this.options.push(a);

          }

        // }

     

      });

    // ignitionGraph
  }

}
