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

import { Router } from '@angular/router';
import { MyaccountComponent } from '../myaccount/myaccount.component';
import { MdDialog } from '@angular/material';
import { FlashMessagesService } from 'angular2-flash-messages';

declare var $:any;

@Component({
  selector: 'app-notification-master',
  templateUrl: './notification-master.component.html',
  styleUrls: ['./notification-master.component.scss']
})
export class NotificationMasterComponent 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:any;
  todate=new Date();
  options=[];
  identifier:any='notification_master';
  logo:any;
  superAdmin:Boolean = false;
  // notifications=["overspeed",'Geo-Fence','Route','IGN','Fuel'];
  vehicles:any;
  fromdate=new Date();
  notifArray: any =[];
 

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

    // this.fromdate.setHours(0,0,0);


   }

  ngOnInit() {
   
    // this.notifications;
    
    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;
  
      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'];
     var fd = new Date().setHours(0,0,0);
     this.Fromdate = new Date(fd);
     this.firstcall=false;
     this.lastcall= false;
    //  this.getNotification();
    this.tableData = false;
  }

  
notification:any;
tableData:boolean=true;
getNotification(){
  this.notifArray=[];
  var from= this.Fromdate
  var to = this.todate;
  console.log(this.Fromdate,this.todate);
 console.log('this.deviceArr',this.deviceArr);
 var user :any;
 if((this.custtype != true)&&(this.superAdmin != true)){
    user = this.useridd
 }
 var limit = 20;
  this.contactService.filteredNotifications(from,to,this.notification,this.deviceArr,user,this.skip,limit)
    .subscribe(res=>{
      this.tableData = true;
      this.notifArray =res;
      if(this.notifArray == 0){
        this.firstcall=true;
        this.lastcall=(this.skip>0)?false:true;

      }
      console.log(this.notifArray);
    },err=>{
      this.notifArray=[];
      this.firstcall=true;
      this.lastcall=(this.skip>0)?false:true; 
      if(err.status == 500){
        this.tableData = false;
        // this._flashMessagesService.show("No data available", { cssClass: 'alert-danger', timeout: 1500 });
      }
    })
}




deviceObj = [];

deviceSelect: any = [];
deviceName: any;



tab:any;
exportExcel()
{

    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    var table = $('#deviceTable').clone();
    table[0].querySelector("thead").remove();
    table[0].prepend($(".dataTable thead").clone()[0]);
    this.tab = table[0];//document.getElementById('deviceTable'); // id of table

    for(j = 0 ; j < this.tab.rows.length ; j++) 
    {     
        tab_text=tab_text+this.tab.rows[j].innerHTML+"</tr>";
      
    }

    tab_text=tab_text+"</table>";
   
    tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE "); 

    var sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}
deviceArr:any=[];
getReport(event){
  this.deviceArr=[];
  console.log("inside function");
  if(event == 'getExcel'){
    this.exportExcel();
  }else{
    var deviceId: any;
    var tpdata = event ; 
    var that = this;
    var fd = new Date(tpdata.fromDate);
    var td  = new Date(tpdata.toDate);
    this.notification =tpdata.notitype;
    
    if(event.deviceArr.length==0){
      deviceId=[];
    }else{
      deviceId= event.deviceArr;
    }
    this.deviceArr = deviceId; 
    this.Fromdate = fd;
    this.todate = td;
    console.log('event',event);
    this.skip = 1;
    this.firstcall=false;
    this.lastcall= true;
    this.getNotification();
 
  }
 
}


firstcall:boolean=true;
lastcall:boolean=true;
skip:number=1;
next() {
  // this.deviceData =[];
  this.lastcall=false;
  this.skip += 1; 
  // this.sidebarData();
  // this.ngOnInit();
  this.getNotification();   
}
pre() {
  // this.deviceData=[]
  // console.log(this.skip)
  if(this.skip>0){
    this.skip -= 1;   
    this.lastcall=false;
    // this.sidebarData();
    // this.ngOnInit();
    this.getNotification(); 
  }else{
    this.lastcall=true;
    this.firstcall=false;
  }
 
}
}
