import { Component, OnInit } from '@angular/core';
import { ContactService } from '../contact.service';
import { MdDialog, MdSnackBar } from '@angular/material';
import { Router, ActivatedRoute } from '@angular/router';


declare var $:any; 

@Component({
  selector: 'app-product-summary',
  templateUrl: './product-summary.component.html',
  styleUrls: ['./product-summary.component.scss']
})
export class ProductSummaryComponent implements OnInit {
  Fromdate: any;
  todate: any;
  superAdmin: any;
  fs: any;
  ls: any;
  emailid: any;
  or: any;
  mb: any;
  useridd: any;
  custtype: any;
  deviceArr: any;

  constructor(public dialog: MdDialog,public snackBar: MdSnackBar,private contactService: ContactService,private router: Router, private route: ActivatedRoute,) { }
  identifier : any= "product_summary";
  Load : 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.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;
  }

  addProduct(){
    this.router.navigateByUrl("product-summary/add_product");   
  }

  getReport(ev){
    console.log(ev);

  }

  tabelObj:any; 

  testTable() {
    const that = this;
    console.log("inside function");
    $(document).ready(function() {
       that.tabelObj = $('#deviceTable').DataTable({
        // dom: 'Bfrtip',
        // buttons: [
        //     'copy', 'csv', 'excel', 'pdf', 'print'
        // ]  ,
            "processing": true,
            "searching": true, 
            pagingType: 'full_numbers',
            pageLength: 25,
            serverSide: false,
            "scrollY":'65vh',  
            "scrollCollapse": true,     
            lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
            ajax: (dataTablesParameters,callback) => {
              console.log('temptemptemptemptemp',dataTablesParameters);
                var deviceID;
                that.Load = true;
               

           
              var from_date = that.Fromdate;
              var to_date = that.todate;
              var user = that.useridd;
              var temp = {
              from_date : from_date,
              to_date : to_date,
              user : user
              }
              var suburl = "";
              suburl += "/notifs/ignitionReport?from_date=" + temp.from_date +'&to_date=' + temp.to_date + '&_u=' + temp.user;
              if(that.deviceArr.length != 0){  
                suburl +='&device='+that.deviceArr;
              }
  
              if((temp.from_date != undefined)&&(temp.to_date != undefined)){
                that.contactService.get(suburl).subscribe(resp => {   
                
                  that.Load = false;
                  callback({data : resp });
              }, err => {   
                that.Load= false;    
                console.log("error",err) ;     
              });
  
              }else{
                let ta = [];
                that.Load = false;
                callback({data : ta });
              }
            
          },  
        
            "columns": [
                {
                    "data": "vehicleName",
                    "render": function(data, type, row) {
                        
                      return data?data : '';                  
                    }
                },
                {
                    "data": "switch",
                    render: function(data, type, row) {
                      return data?data:'';        
                    }
                },
                {
                   "data": "timestamp",
                    render: function(data, type, row) {
                      data?data:''; 
                  
                    }
                },          
              {
                "data": "address",
                "defaultContent": "_id",
                render: function(data, type, row) {        
                  return data?data:""
                }
            }    
            ,
            {
              "data": "address",
              "defaultContent": "_id",
              render: function(data, type, row) {        
                return data?data:""
              }
          }    ,
          {
            "data": "address",
            "defaultContent": "_id",
            render: function(data, type, row) {        
              return data?data:""
            }
        }    ,   {
          "data": "address",
          "defaultContent": "_id",
          render: function(data, type, row) {        
            return data?data:""
          }
      }                       
            ],  
            "columnDefs": [
              { className: "dt-body-left", "targets": [ 0,1,2,3 ] },
         
            ] 
        });
  
    });
  
  }

}
