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


declare var $:any;
declare var moment :any; 

@Component({
  selector: 'app-order-summary',
  templateUrl: './order-summary.component.html',
  styleUrls: ['./order-summary.component.scss']
})
export class OrderSummaryComponent 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= "order_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;
    this.testTable()
  }

  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) => {
                that.contactService.getOederSummary().subscribe(resp => {         
                  that.Load = false;
                  callback({data : resp });
              }, err => {   
                that.Load= false; 
                callback({data : [] });   
                console.log("error",err) ;     
              });
          },  
          "rowCallback": function(row: Node, data: any | Object, index: number){     
            $('#orderDetail', row).bind('click', () => {    
              // console.log('ExpenseTypeDetail=>',et);
              console.log(data);

              let dialogRef = that.dialog.open(OrderDetailsComponent, {
                width : "700px",
                data: data?data.CartData:[]
              });
            
              dialogRef.afterClosed().subscribe(result => {
                if(result == "success"){
                  console.log("success");   
                }
              });  
            
              
            }); 

          },
        
            "columns": [
           
                {
                    "data": "purchased_on",
                    "render": function(data, type, row) {
                      return data ? moment(data).format('llll') : "--";
                                    
                    }
                },
                {
                    "data": "buyer_first_name",
                    "visible" : true,
                    render: function(data, type, row) {
                      console.log(row);
                      var fn = data?data:'';
                      var ln = row.buyer_last_name?row.buyer_last_name:'';
                      console.log(ln);
                      return  fn+ " " +  ln;        
                    }
                },
              //   {
              //     "data": "buyer_last_name",
              //     "visible" : true,
              //     render: function(data, type, row) {
              //       return row.buyer_first_name?row.buyer_first_name:''+ " "+ data.buyer_last_name?data.buyer_last_name:data.buyer_last_name;        
              //     }
              // },
                {
                   "data": "phone",
                    render: function(data, type, row) {
                     return data?data:''; 
                  
                    }
                }, 
                {
                  "data": "email",
                   render: function(data, type, row) {
                    return data?data:''; 
                 
                   }
               },          
              {
                "data": "shippingAddress",
                "defaultContent": "_id",
                render: function(data, type, row) {        
                  return data.city + ' ' + data.state +' ' + data.country + ' ' + data.pincode ; 
                }
            }    
   ,
          {
            "data": "status",
            "defaultContent": "_id",
            render: function(data, type, row) {        
              return data?data:"Processed";
            }
        }    ,   {
          "data": "CartData",
          "defaultContent": "_id",
          render: function(data, type, row) {        
            return '<i id="orderDetail" title="Order Detail" style="font-size:20px" class="fas fa-info"></i>'      
          }
      }                       
            ],  
            "columnDefs": [
              { className: "dt-body-left", "targets": [ 0,1,2,3 ] },
              { className: "dt-body-center", "targets": [ 6 ] },
         
            ] 
        });
  
    });
  
  }



}
