import { Component, Inject, OnInit } from '@angular/core';
import { NB_WINDOW_CONTEXT, NbWindowRef, NbWindowService } from '@nebular/theme';
import { PageapiService } from '../../pageapi.service';

@Component({
  selector: 'ngx-order-summary-form',
  templateUrl: './order-summary-form.component.html',
  styleUrls: ['./order-summary-form.component.scss']
})
export class OrderSummaryFormComponent implements OnInit {
  customer:any =[];
  product:any =[];
  setData:any =[];
  submitBtn: boolean= false;
  constructor(@Inject(NB_WINDOW_CONTEXT) public data: any,public _api:PageapiService,private windowService: NbWindowService, private windowRef: NbWindowRef) { 

    console.log(this.data)
   
    this.customer = this.data.name
    this.product = this.data.product

    this._api.orderDetails_observable$.subscribe((res) => {
      console.log('booked', res);

      if(res){
 this. setData =   res

 this.getAnswerProduct()
      }
  
    })

   
  }

  ngOnInit() {

    this.customer = this.data.name
    this.product = this.data.product
  }

  save(){
    this.submitBtn = true
    console.log(this.data,this.setData)
   let cus =  this.data.customerData['CusDetails.id']
 let role = localStorage.getItem('user_role')
 this.data.product.forEach(element => {
  delete element.isNew;
  console.log(element)
  element.productName = element.name

 if(element.hasOwnProperty('primaryId')){
  element.productId = element.id

  element.id = element.primaryId
 }else{
  element.pid = element.id
  element.productId = element.id
 delete  element.id

 }
});
console.log(this.data.product)
 let param;
 if(this.setData != undefined){
 if(this.setData.hasOwnProperty('id')){
  cus = this.setData['OrderDetails.CusDetails.id']
   param ={id:this.setData.id,customerId:cus,status:'ordered' , product: this.data.product ,createdBy:role ,  orderId:this.setData.orderId }
 }else{
  param ={customerId:cus,status:'ordered' , product: this.data.product ,createdBy:role }
 }
}else{
  param ={customerId:cus,status:'ordered' , product: this.data.product ,createdBy:role }
}
  
   
  
 
console.log(param)
    
    this._api.CreateOrder (param).subscribe(res => {
      if (res.status == 200) {
console.log(res)
 this.windowRef.close(1);
      }else{
        this.windowRef.close(0);
      }
    })
  }
  cancel(){
    this.windowService
    console.log( this.windowService)
    this.windowRef.close();
  }


  getAnswerProduct(){

  }
}
