import { Component, OnInit } from '@angular/core';
import { NbComponentStatus, NbDialogRef, NbDialogService, NbGlobalPhysicalPosition, NbGlobalPosition, NbToastrService } from '@nebular/theme';
import { PageapiService } from '../../pageapi.service';

@Component({
  selector: 'ngx-add-order',
  templateUrl: './add-order.component.html',
  styleUrls: ['./add-order.component.scss']
})
export class AddOrderComponent implements OnInit {
  setData:any =[];
  position: NbGlobalPosition = NbGlobalPhysicalPosition.TOP_RIGHT;
  preventDuplicates = false;
  status: NbComponentStatus = 'primary'; 
  index = 1;
  destroyByClick = true;
  duration = 2000;
  hasIcon = true;

  heading:any =[];product:any =[];  inputFieldGroups:any = [{id:'', qty: '1', productSpec: '' }]; orderId:any =[];
  constructor(private toastrService: NbToastrService,private dialogRef: NbDialogRef<AddOrderComponent> ,public _api:PageapiService,private dialogService: NbDialogService) {

  
  }

 ngOnInit() {
  this.heading = "Add Return"
   console.log('dialogRef', this.dialogRef.componentRef.instance);
let answer:any  =  this.dialogRef.componentRef.instance
   this.getProductlistApi( answer.details)
  }



  getProductlistApi(param){
    let data ={type : param} 
 
    this._api.getProducts(data).subscribe(res => {
      console.log(res)
      if(res.status == 200){

       let passData =  res.data
       let product = passData 
console.log(product)
       product.forEach((element:any, index) => {
        element.index = index  +1       
        element.qty = 0;
        element.pId = element.id
        
      });

      this.product = product
     // this.productfull = product
     
      //  console.log(data)
       
      }else{
        this.product = [];
      }
    })
  }

  addInputFieldGroup() {
    this.inputFieldGroups.push({id:null, qty: '1', productSpec: '' }); // Add a new input field group to the array
    //this.childButtonEvent.emit( this.inputFieldGroups);
  }
  
  removeInputFieldGroup(index: number) {
    console.log(  this.inputFieldGroups)
    this.inputFieldGroups.splice(index, 1); // Remove the input field group at the specified index
    //this.childButtonEvent.emit( this.inputFieldGroups);
  }


  cancel(){
    this.dialogRef.close();
  }

  save(){

    let product=  this.inputFieldGroups.filter((arr:any)=>{return arr.qty != 0}) 
    console.log(product)

    if(product.length>0){


    let temp:any =[]
    this.orderId = localStorage.getItem('orderId')
    let cus = localStorage.getItem('cusId')
    let driverId = localStorage.getItem('driverId')
    product.forEach((element:any) => {
      temp.push({orderId: this.orderId,productId:element.id,cusId:cus,qty:element.qty,driverId:driverId,returnTime:new Date(),createdBy:driverId,cylinderNo:element.productSpec})
    });
    
   console.log(temp)
   let paramData ={product:temp}
   this._api.CreateReturn (paramData).subscribe(async res => {
    if (res.status == 200) {
console.log(res)

this.dialogRef.close(1);
    }
  })
}else{
  this.showToast('success', 'Data Changed Successfully', '');
  this.dialogRef.close();
}
  }


  private showToast(type: NbComponentStatus, title: string, body: string) {
    const config = {
      status: type,
      destroyByClick: this.destroyByClick,
      duration: this.duration,
      hasIcon: this.hasIcon,
      position: this.position,
      preventDuplicates: this.preventDuplicates,
    };
    const titleContent = title ? ` ${title}` : '';

    this.index += 1;
    this.toastrService.show(
      body,
      `${titleContent}`,
      config);
  }

}
