import { Component, Inject, OnInit } from '@angular/core';
import { MdDialogRef, MD_DIALOG_DATA } from '@angular/material';
import { ContactService } from '../../contact.service';
declare var $: any;

@Component({
  selector: 'app-renewal-history',
  templateUrl: './renewal-history.component.html',
  styleUrls: ['./renewal-history.component.scss']
})
export class RenewalHistoryComponent implements OnInit {
  tabelObj: any ;
  Load: boolean;
  main=[];
 
  constructor(public dialogRef: MdDialogRef<RenewalHistoryComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) { 
      // this.main=this.data
      // this.testTable()
    }

  ngOnInit() {
    this.getRenewalHistory()
  }

  getRenewalHistory(){
    this.Load=true
    this.contactService.getRenewalHistory(this.data._id).subscribe(res=>{
      console.log(res);
      this.main=res;
      this.Load=false
      
    })
  }

    testTable(){
  const that = this;
  console.log("inside function");
  // that.Load = true;

   $(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: (mainData,callback) => {

 
      that.contactService.getRenewalHistory(this.data._id).subscribe(res=>{
        console.log(res);
        that.Load = false;
        if(res.length!=0){
          callback({ data: res[0].data });

        }else{
           callback({ data: [] });
        }
        
      })

   
  },
   "columns": [
              {
                  "data": "Device_Name",
                  "render": function(data, type, row) {                   
                    return data?data.device.Device_Name : '';                  
                  }
              },
              {
                  "data": "Device_ID",
                  "render": function(data, type, row) {                   
                    return data?data.device.Device_ID : '';                  
                  }
              },
              {
                  "data": "expired_on",
                  "render": function(data, type, row) {                   
                    return data?data : '';                  
                  }
              },
              {
                  "data": "renewed_on",
                  "render": function(data, type, row) {                   
                    return data?data : '';                  
                  }
              },
             
   
          ],  
          "columnDefs": [
            { className: "dt-body-left", "targets": [ 0,1,2,3] },  
          { "width": "10px", "targets": 0 },
          { "width": "40px", "targets": 1 },
          { "width": "100px", "targets": 2 },
          { "width": "70px", "targets": 3 },
          ] , order: [[ 3, 'desc' ], [ 0, 'asc' ]]
      });

  });

}


}
