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

@Component({
  selector: 'ngx-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.scss']
})
export class ProductFormComponent implements OnInit {
  userForm: FormGroup | any;  formData:any =[]
  constructor(@Inject(NB_WINDOW_CONTEXT) public data: any,public _api:PageapiService,private windowService: NbWindowService, private windowRef: NbWindowRef) {

    console.log(this.data)
   }

  ngOnInit(): void {

    this.userForm = new FormGroup({

     


      productCategory: new FormControl('', [Validators.required]),
      productType: new FormControl('', [Validators.required]),
      productName: new FormControl('', [Validators.required]),
      description:new FormControl('', [Validators.required]),
      
    });
    if(this.data.hasOwnProperty('id')){
      // this.userForm.controls['password'].clearValidators();
      // this.userForm.controls['password'].updateValueAndValidity();
      }
    this.userForm.patchValue(this.data)
  }

  cancel(){
    this.windowService
    console.log( this.windowService)
    this.windowRef.close();
  }
  onSubmit(){

    console.log(this.userForm.value)
    let tmp = this.userForm.value
    this.formData =[]
    this.formData= new FormData();
    if(this.data.hasOwnProperty('id')){
      //this.formData.append('id', this.data.id);
      tmp.id = this.data.id
    }  
    // this.formData.append('userName',tmp.userName) 
    // this.formData.append('email', tmp.email);
    // this.formData.append('password', tmp.password);
    // this.formData.append('role', tmp.role);
    // this.formData.append('mobileNo', tmp.mobileNo);  

    this._api.CreateProductDetails (tmp).subscribe(res => {
      if (res.status == 200) {
console.log(res)
this.windowRef.close(1);
      }else{
        this.windowRef.close(0);
      }
    })

}
}
