import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { PageapiService } from '../pageapi.service';
import * as moment from 'moment';
@Component({
  selector: 'ngx-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent {

  orders:any =[];inprogress:any=[];delivery:any =[];
  constructor(private router: Router,public _api:PageapiService){
    const token = localStorage.getItem('user_token'); 
    console.log(token)
  
          if(!token){
            this.router.navigate(['/auth/login']);
          }

          this.dateWiseFilter()
         
            
  }


  dateWiseFilter() {
    
    let from = (moment().subtract(6, 'days'))['_d']
    let param = {fromDate:from
    ,toDate: moment()['_d']}
    console.log(param)

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

       let passData =  res.data
       passData.sort((a: any, b: any) => {
        return b.id - a.id;
      });
       passData.forEach((element:any, index) => {
        element.index = index       +1  
        element. selected =false
        element. selectedval =0
        element.customer = element['OrderDetails.CusDetails.cusName']

        element.address = element['OrderDetails.CusDetails.address_1']+","+element['OrderDetails.CusDetails.city']+","+element['OrderDetails.CusDetails.pincode']
        element.productName = element['ProductDetails.productName']
        element. orderDate = element.createdAt
      });
      this.orders = passData.filter(arr=>{return arr.status == "ordered"}) 
      this.inprogress = passData.filter(arr=>{return arr.status == "In Progress"}) 
      this.delivery = passData.filter(arr=>{return arr.status == "Delivered"}) 


      //  console.log(data)
    // this.source.load(passData);
      }
    })

  }
}
