import { Component, Inject, OnInit } from '@angular/core';
import * as moment from 'moment';
import { PageapiService } from '../../pageapi.service';
import { ActivatedRoute, Router } from '@angular/router';
import { NB_WINDOW_CONTEXT } from '@nebular/theme';
 @Component({
  selector: 'ngx-summary-details',
  templateUrl: './summary-details.component.html',
  styleUrls: ['./summary-details.component.scss']
})
export class SummaryDetailsComponent implements OnInit {

  ranges: any = {
    'Today': [moment(), moment()],
    // 'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
    // 'Last 7 Days': [moment().subtract(6, 'days'), moment()],
    // 'Last 30 Days': [moment().subtract(29, 'days'), moment()],
    // 'This Month': [moment().startOf('month'), moment().endOf('month')],
    // 'Last Month': [
    //   moment().subtract(1, 'month').startOf('month'),
    //   moment().subtract(1, 'month').endOf('month')
    // ]
  };
  productlistReturn:any =[];orderId:any=[];cusName:any =[];
  selectedDateRange:any =[];drivers:any =[];summaryaData:any=[];driverval:any=[];returnSummary:any =[];productlist:any=[];
  constructor(@Inject(NB_WINDOW_CONTEXT) public data: any,public _api:PageapiService,private route: ActivatedRoute, private router:Router) {
   console.log(data)
    
  //  this.route.params.subscribe(params => {
      console.log(data) //log the entire params object
      console.log(data.orderID) //log the value of id
      let orderId = data.orderID
      this.orderId = orderId
      this._api.getProductListSummary({orderId:orderId}).subscribe(res => {
        console.log(res)
        if(res.status == 200){

          this.productlist = res.data
     
      

        }
      })

      this._api.getReturnProductListSummary({orderId:orderId}).subscribe(res => {
        console.log(res)
        if(res.status == 200){

          this.productlistReturn = res.data
     
      

        }
      })


    // });

   }

  ngOnInit(): void {

    this.cusName = localStorage.getItem('cusName')
    this.selectedDateRange =[];
    console.log(moment()['_d'])
   
    this.getDriverlistApi()
  }


  back(){
    // let data = { 'date' : localStorage.getItem('dDate') , 'id' : localStorage.getItem('dId') }
    this.router.navigate(['/pages/summary']) 
  }
  save(){

  }

  generatePDF(data){


  }
  dateWiseFilter(){
    console.log(this.selectedDateRange,this.driverval)
    if(this.selectedDateRange.length!=0){


   
  }
    //this.getDriverSummaryAdminList(result)

  }

  mergeOrders(orderItems: any[]) {
    this.summaryaData = orderItems.reduce((acc, curr) => {
      const existingOrder = acc.find((order) => order.orderId === curr.orderId);
      if (existingOrder) {
        existingOrder.total_qty += curr.total_qty;
      } else {
        acc.push(curr);
      }
      return acc;
    }, []);
  }
  getDriverlistApi(){
   // console.log(this.selectedDateRange)
    let param ={}
    this._api.getusers(param).subscribe(res => {
      console.log(res)
      if(res.status == 200){
  
       let passData =  res.data
       let driver = passData.filter(arr=>{return arr.role == 'DRIVER'}) 
       driver.sort((a: any, b: any) => {
        return b.id - a.id;
      });
       driver.forEach((element:any, index) => {
        element.index = index       +1  
        
      });
  
  
  
      this.drivers = driver
      //  console.log(data)
      
      }
    })
  }


  getDriverSummaryAdminList(date){
    let param ={date:new Date(date)}
    this._api.getDriverSummaryAdminList(param).subscribe(res => {
      console.log(res)
      if(res.status == 200){
   
      console.log(res.data)
      
      }
    })
  }

  val(data){
    console.log(data.id)

    let result;
    if(this.selectedDateRange.length != 0){
      result=   this.selectedDateRange.endDate['$d']
    }else{
      
      result =  moment()['_d']
    }
  
    console.log(result)
    let param ={date:new Date(result),id:data.id}
    this._api.getDriverSummaryAdminList(param).subscribe(res => {
      console.log(res)
      if(res.status == 200){
   
      console.log(res.data)

      this.summaryaData = res.data
      this.summaryaData.forEach(element => {
        element.name = element['UserDetails.userName']
        
      });

      this.mergeOrders(  this.summaryaData)
      
      }
    })

    this._api.getReturnDriverSummaryListadmin(param).subscribe(res => {
      console.log(res)
      if(res.status == 200){
   
      console.log(res.data)

      this.returnSummary = res.data

    

     // this.mergeOrders(  this.summaryaData)
      
      }
    })

    
  }


  returnCount(data){
   let result =  this.returnSummary.filter(arr=>{return arr.orderId == data}) 
console.log(result)
   if(result.length>0){

    return result[0].total_amount
   }else{
    return '-'
   }

  }
  
}
