import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { PageapiService } from '../../pageapi.service';

@Component({
  selector: 'ngx-mixed-form',
  templateUrl: './mixed-form.component.html',
  styleUrls: ['./mixed-form.component.scss']
})
export class MixedFormComponent implements OnInit {
 
  setData:any =[];
  fieldTitle:any =[];
  @Output() childButtonEvent = new EventEmitter();

  inputFieldGroups: {id:any, qty: string, productSpec: string }[] = [{id:'', qty: '0', productSpec: '' }]; // Array to hold input field groups
  constructor(public _api:PageapiService) { 

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

      if(res != null){
 this. setData =   res
 this.getAnswerProduct(this.setData.orderId)
 let result = localStorage.getItem('viewOrder')
 if(result == '1'){
   this.fieldTitle = 'View Order'
 }else{
   this.fieldTitle = 'Edit Order'
 }
 if( this. setData['ProductDetails.productName'] == "Mixed Gas"){


//  this.inputFieldGroups[0].qty =  this. setData.qty
//  this.inputFieldGroups[0].productSpec  =  this. setData.productSpec
//  this.inputFieldGroups.push({ qty:  this. setData.qty, productSpec:  this. setData.productSpec });
}
      }else{
        localStorage.setItem('viewOrder','0')
        let result = localStorage.getItem('viewOrder')
        if(result == '1'){
          this.fieldTitle = 'View Order'
        }else{
          this.fieldTitle = 'Edit Order'
        }
      }
  
    })
  }

  ngOnInit() {
    let result = localStorage.getItem('viewOrder')
    if(result == '1'){
      this.fieldTitle = 'View Order'
    }else{
      this.fieldTitle = 'Edit Order'
    }
  }
  addInputFieldGroup() {
    this.inputFieldGroups.push({id:null, qty: '0', 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);
  }

  pass(){
    this.childButtonEvent.emit( this.inputFieldGroups);
  }

  getAnswerProduct(orderId){

    console.log(orderId)

    

    let param={orderId:orderId}
    this._api.getProductList(param).subscribe(res => {
   
      if(res.status == 200){

let result = res.data

console.log(result)
let final = result.filter(arr=>{return arr.productId == 6}) 

if(final.length>0){
  this.inputFieldGroups=[];

// this.inputFieldGroups=[];
final.forEach(element => {
 // if(element.ProductDetails.length>0){


// if(element.productId =="6"){
  this.inputFieldGroups.push({id:element.id, qty: element.qty, productSpec: element.productSpec })
 //  }
//}

});

}
console.log( this.inputFieldGroups)

this.inputFieldGroups = this.inputFieldGroups.filter(arr=>{return arr.productSpec != null}) 

//  if( this.inputFieldGroups.length>0){
//   this.inputFieldGroups.push({id:null, qty: '0', productSpec: '' });
//  }
// this.product.forEach(element => {
//   result.forEach(e => {
//     console.log(e,element)
//   if(element.productName ==  e.ProductDetails[0].productName ){
//     //id: 110
//     element.id =  e.id
//     element.qty =  parseInt(e.qty)
//   }
// });

// });
//       }

//     })

//     this.product.forEach(res => {
      
  
//       res.name = res.productName
//  res.qty = res.qty
//  res.price = this.setData['OrderDetails.CusDetails.cusType']
//  this.invoice.additionalDetails = 'https://vnms.in/services/'
 
//   this.invoice.products.push(res)
 

 
 
// })
//console.log(this.product,this.invoice.products)
  }
})
  }
  

}
