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;
  organisationFlag: any;
  superAdmin: any;
  isDealer: any;
  ignitionObj=[];
digitalInput
  constructor(public dialogRef: MdDialogRef<AcreportInfoComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) {
    this.digitalInput=data.deviceInfo
      // 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.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id; 
    this.organisationFlag = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).isOrganisation  ;     
      this.superAdmin = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).isSuperAdmin;
      this.isDealer = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).isDealer;
      // if(this.digitalInput==1)
      // {
      //   this.getIgnitionReport();
      // }else{
      //   this.AcReport()
      // }
      this.ignitionObj=this.digitalInput
  }

  cancel(){
    this.dialogRef.close('close');

  }

  AcReport(){
    console.log(this.data);
    var fdate = new Date(this.data.deviceInfo.fromDate);
    var tdate = new Date(this.data.deviceInfo.toDate);

    // var ptt = new Date(fdate).setHours(0, 0, 0);
    // var ttt = new Date(tdate).setHours(23,59,59);
    var payload = {
      user : this.useridd,
      device : this.data.deviceInfo.Device_ID,
      fromDate : fdate,
      toDate:tdate
    }
    var suburl = "";
     suburl += "/notifs/acReport?from_date=" + payload.fromDate +'&to_date=' + payload.toDate + '&_u=' + payload.user +'&device='+payload.device;
              if((payload.fromDate != undefined)&&(payload.toDate != undefined)){
                this.contactService.get(suburl).subscribe(resp => {  
                  console.log(resp[0].s)
                this.ignitionObj = resp[0].s;
                })
              }

  }

   getIgnitionReport(){
    console.log(this.data);
    var fdate = new Date(this.data.deviceInfo.fromDate);
    var tdate = new Date(this.data.deviceInfo.toDate);

    // var ptt = new Date(fdate).setHours(0, 0, 0);
    // var ttt = new Date(tdate).setHours(23,59,59);
    var payload = {
      user : this.useridd,
      device : this.data.deviceInfo.Device_ID,
      fromDate : fdate,
      toDate:tdate
    }
    console.log(payload);
    this.contactService.getIgnition(payload).subscribe(res=>{
      console.log(res);
      this.ignitionObj = res;

    })
  }
}
