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


@Component({
  selector: 'app-ac-report',
  templateUrl: './ac-report.component.html',
  styleUrls: ['./ac-report.component.scss']
})
export class AcReportComponent implements OnInit {

  ignition_data: any;
  fs : any;
  ls : any;
  emailid : any;
  before:any;
  or : any;
  useridd :any;
  time :any;
  device_data:any;
  devicess:any;
  final:any;
  custtype:any;
  cust:boolean=false;
  Load : Boolean = false;
  date : any;
  devicesss:any;
  mb:any;
  logoutbut:boolean;
  dealer:boolean;
  text:any;
  option:any; 
  finall:any;
  Fromdate=new Date();
  todate=new Date();
  options=[];
  logo:any;
  superAdmin:Boolean = false;

  t:any;
  dataSelect: any[];
  vname: string;
  primaryObj: any;
  errorMsg: string;

  constructor(private contactService: ContactService,private router: Router,public dialog: MdDialog) { }

  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.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.contactService.getDevice(this.emailid,this.useridd).subscribe(

      data => {
        this.devicesss = data
     
        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);
          // }

        }
          // console.log("ignition option",this.options);
      });



    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'];


  }



errorDialog:boolean = false;
tableArr =[];
acObject:any=[];
ac_report(){
  this.acObject =[];
  this.Load = true;
  console.log(this.dataSelect);
    if(this.dataSelect.length != 0){
    this.vname = this.dataSelect[0].did;
  
  }else{
    this.vname = '';
  }
  this.errorDialog = false;
  console.log(this.vname);
  this.contactService.acReportMain(this.Fromdate,this.todate,this.useridd,this.vname).subscribe(res=>{
    this.Load = false;
    this.primaryObj = res;
    console.log(this.primaryObj);
    if(this.primaryObj.length != 0){
      for(var i = 0;i<this.primaryObj.length;i++){
        var reportData = {
          vehicleNumber : this.primaryObj[i].VehicleName ? this.primaryObj[i].VehicleName :"",
          action1 : this.primaryObj[i]['1stAction'],
          actionOnDuration  : (this.primaryObj[i]['1stAction'] == 'ON')?this.primaryObj[i]['1stTime'] : this.primaryObj[i]['2stTime'],
          action2: this.primaryObj[i]['2stAction'],
          actionoffDuration : (this.primaryObj[i]['1stAction'] == 'OFF')?this.primaryObj[i]['1stTime'] : this.primaryObj[i]['2stTime'],
          imei : this.primaryObj[i].imei
        }
     
        this.acObject.push(reportData);

    }
    }else{
      this.errorDialog = true;
      this.errorMsg = 'No data Found';
    }
 
     console.log('this.acObject',this.acObject);
  },err=>{
    this.Load = false;
  })

}



exportExcel(){
  
}

additionalDetails(infoObject){
  infoObject.fromDate = this.Fromdate;
  infoObject.toDate = this.todate;
  let dialogRef = this.dialog.open(AcreportInfoComponent, {
    width: '800px',  
    data: {"deviceInfo":infoObject}   
  
  });
}


timeconversion(total_min){
 
  var hours = parseInt(total_min) / 60
  var rhours = Math.floor(hours);
  var minutes = (hours - rhours) * 60;
  var rminutes = Math.round(minutes);

  var Durations = rhours + " " + 'hrs' + " " + rminutes + " " + 'min';
  return Durations ;
}
filterStates(val) {

  // console.log(val)
  // console.log(this.options)

  if (val) {
    const filterValue:any = val
     this.dataSelect = this.options.filter( function(d){
  
      return d.value.toLocaleLowerCase().indexOf(filterValue.toLocaleLowerCase()) > -1;    
    });
    return this.dataSelect;
  }

}

}
