import { Component, OnInit } 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';
import * as moment from 'moment';

@Component({
  selector: 'app-ideal-report',
  templateUrl: './ideal-report.component.html',
  styleUrls: ['./ideal-report.component.scss'],
  providers: [ContactService]
})
export class IdealReportComponent implements OnInit {
  public barChartOptions:any = {
    scaleShowVerticalLines: false,
    responsive: true,
    
  };
  myaccount(){

    let dialogRef = this.dialog.open(MyaccountComponent, {
      width: '903px',
      data: {}
    });
  
    dialogRef.afterClosed().subscribe(result => {
  
      if(result == "succ"){
        // console.log("Updated")
      }
  
    });
  }
  Load:boolean = false;
  device_data:any;
  data:any;
  neww:any;
  before:any;
  date1: Date = new Date();

  creategraph(neww){
    // console.log(neww);


/* let rr = new Date(this.date1).setHours(0,0,0,0) */
let rr = new Date().setHours(0,0,0,0) -1 ;
let r = new Date(rr).toISOString();
// console.log(r);
this.Load = true;
/* https://www.adnateiot.com/gps/getGpsIdlingReport?imei=865205032256181&to=2018-01-31T18:30:00.000Z */
this.contactService.getIdealData(neww.did,r).subscribe(
      
  data => {
    this.Load = false;
    this.device_data = data 
  //  console.log(this.device_data); 
   this.barChartLabels.length = this.device_data.length;  
   this.getdata(this.device_data)
   //this.lineChartLabels.length = this.device_data.length;  
  // this.getdata(this.device_data)
/*
0
:
{Total_idle_time: 21.778455, Date: "2018-01-24T09:43:58.180Z"}
1
:
{Total_idle_time: 20.732545833333333, Date: "2018-01-25T09:43:58.180Z"}
2
:
 */
  });
  }
  ign(){
    this.router.navigateByUrl("device-report/ign-report");
  
  }
  getdata(final){
    let fi = [];
    let time = [];
    var day =0; 
    var month = 0; 
     var year = 0;
      var final_date =null;
    this.barChartLabels.length = 0;
   /*  for(let q = final.length; q >0 ;q -- ) */
   for(let q = 0; q <final.length ;q ++ )
    {
      fi.push(final[q].Total_idle_time.toFixed(2));
    /*   let t = new Date(final[q].Date).toLocaleTimeString();
      console.log(t); */
      //var g = new Date(final[q].Date);
          
     // day = g.getDate()+1; 
      // month = g.getMonth()+1; 
       // year = g.getFullYear();
      
        // final_date = day+"/"+month+"/"+year
         final_date=moment(final[q].Date).add(1,'ms').format('DD/MM/YYYY');

      //let k = JSON.stringify(final[q].time).split("T")[1].split(".")[0];
      this.barChartLabels.push(final_date)
    }
   this.Load = false;
    this.barChartData = [{
      label: "Idle (Hours)",
      data: fi
    }];
    
  // console.log(this.barChartData)
  
  }
  addcus(){
    this.router.navigateByUrl("add");
  }
  geo(){
    this.router.navigateByUrl("geofencing");
    
  }

  logout(){
    window.localStorage.clear();
    this.router.navigateByUrl("login");
  }
  devi(){
    this.router.navigateByUrl("dashboard");
  
  }
  soon(){
    this.router.navigateByUrl("const?_i="+window.localStorage.token);
    
  }

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


  }

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

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

  Ddetail(){
    this.router.navigateByUrl("driverdetail");
  }
  DModel(){
    this.router.navigateByUrl("deviceModel");
  }
  VType(){
    this.router.navigateByUrl("VehicleType");
  }
  
  

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

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

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

  group(){
    this.cond = false
    this.router.navigateByUrl("group_view");
  }
  alert_report(){
    this.cond = false
    this.router.navigateByUrl("device-report/alert_report");
  }
summaryReport(){
 
  this.cond = false
  this.router.navigateByUrl("device-report/summary-report");
}
  new(){
    this.router.navigateByUrl("new");
    
  }

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

  }

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

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

}
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");
  }
}
report_speed(){
  // console.log("report speed call")
  this.cond = false
  this.router.navigateByUrl("device-report/device-speed-report");
}
ideal(){
  this.router.navigateByUrl("device-report/ideal-report");
}
openNav() {
  /*  document.getElementById("myNav").style.width = "100%";
*/
   this.router.navigateByUrl("location");

}
  public barChartLabels = [];
  public barChartType:string = 'bar';
  public barChartLegend:boolean = true;
 
  public barChartData = [
    {data: [0, 0, 0, 0, 0, 0, 0], label: 'Idle'}
   ];
 
  // events
  public chartClicked(e:any):void {
    // console.log(e);
  }
 
  public chartHovered(e:any):void {
    // console.log(e);
  }
 
  public randomize():void {
    // Only Change 3 values
    let data = [
      Math.round(Math.random() * 100),
      59,
      80,
      (Math.random() * 100),
      56,
      (Math.random() * 100),
      40];
    let clone = JSON.parse(JSON.stringify(this.barChartData));
    clone[0].data = data;
    this.barChartData = clone;

  }
  constructor(private contactService: ContactService,private router: Router,public dialog: MdDialog) { }
fs:any;
ls:any;
emailid:any;
or:any;
useridd:any;
custtype:any;
cust:boolean = false;
time :any;
devicesss:any;
finall:any;
options = [];
mb:any;
logoutbut:boolean;
dealer:boolean;
logo:any;
text:any;
superAdmin:Boolean = false;
  ngOnInit() {
    this.superAdmin = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).isSuperAdmin;
    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.mb =  JSON.parse(window.atob(window.localStorage.token.split('.')[1])).phn;

    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
    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.time = 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 ={
              dname : this.devicess.devices[i].Device_Name,
              did:this.devicess.devices[i].Device_ID,
              id:this.devicess.devices[i]._id,
              user:this.devicess.devices[i].user
            }
            this.options.push(a); */
            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);

          // }

        }

     

      });

  }

}
