import { Component, OnInit } from '@angular/core';
import { ContactService } from '../contact.service';
declare var moment:any;
declare var $: any;
@Component({
  selector: 'app-inactive-admin',
  templateUrl: './inactive-admin.component.html',
  styleUrls: ['./inactive-admin.component.scss']
})
export class InactiveAdminComponent implements OnInit {
noDays;
data_descip;
tabelObj;
inactiveSup=[];
Load:boolean=false
  constructor(private contactService:ContactService) { }
  
  ngOnInit() {
    this.testTable()
  }

  search(){
     
    console.log(this.noDays);
    
    if(this.noDays==null || this.noDays==undefined){
      this.data_descip = 'Please enter number of days';
      launch_toast()
    }else{
      this.Load = true;
      var from=moment(new Date()).subtract(this.noDays, "days"); 
      var to= new Date().toISOString()
      console.log(from,new Date(to).toISOString());
      
      this.contactService.get('/users/getInactiveAdmins?from='+new Date(from).toISOString()+"&to="+to).subscribe((res:any)=>{
        console.log(res);
        this.inactiveSup=res;
         this.Load = false;
        this.tabelObj.ajax.reload();
      })
    }
    function launch_toast() {
      // console.log(divid);
      var x = document.getElementById("toast")
      //console.log(x);
      x.className = "show";
      setTimeout(function () { x.className = x.className.replace("show", ""); }, 4500);
    }
  }

  testTable() {
  const that = this;
  console.log("inside function");
  $(document).ready(function() {
     that.tabelObj = $('#deviceTable').DataTable({
              
          "processing": false,
          "searching": true, 
          pagingType: 'full_numbers',
          pageLength: 25,
          serverSide: false,
          responsive: true,
          "scrollY":'60vh',  
          "scrollCollapse": true, 
          
          // "deferLoading": 25 ,  
          lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
            "rowCallback": function(row: Node, data: any | Object, index: number){

            },
             ajax: (dataTablesParameters,callback) => {
                callback({ data: that.inactiveSup });
           
            },  
      
          "columns": [
              {
                  "data": "_id",
                  "render": function(data, type, row) {                   
                    return row.first_name?row.first_name: ''+' '+row.last_name?row.last_name: '';                  
                  }
              },
               {
                  "data": "phone",
                  "render": function(data, type, row) { 
                   
                    return data?data:''               
                  }
              },
               {
                  "data": "email",
                  "render": function(data, type, row) { 
                   return data?data:''                    
                  }
              },
              
    

          ],  
          
      });
      })
      }
      tab
      exportToExcel(){
        
	    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];
  
	    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, ""); 

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

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

	    return (sa);
  
      }


}
