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

@Component({
  selector: 'ngx-assign-form',
  templateUrl: './assign-form.component.html',
  styleUrls: ['./assign-form.component.scss']
})
export class AssignFormComponent   {

 
  driverForm:any;
  drivers:any=[];
  constructor(@Inject(NB_WINDOW_CONTEXT) public data: any,public _api:PageapiService,private windowService: NbWindowService, private windowRef: NbWindowRef) {
         console.log(this.data)
    this.driverForm = new FormGroup({
      
 
      driver: new FormControl('', [ Validators.required])
    
    });

    this.getDriverlistApi()
  }

  close() {
    this.windowRef.close();
  }
  onSubmit(){
    console.log( this.data , this.driverForm.value)
    let tmp=[];
    this.data.forEach(element => {
      tmp.push({id:element.id ,driverId: this.driverForm.value.driver ,  status:'Out For Delivery' })
      
    });
 
   let  param ={driver:tmp}
    
     
      
     
    
   
       
       this._api.CreateOrder (param).subscribe(res => {
         if (res.status == 200) {
   console.log(res)
    this.windowRef.close(1);
         }else{
           this.windowRef.close(0);
         }
       })

  }

  getDriverlistApi(){
    let param ={}
    this._api.getusers(param).subscribe(res => {
      console.log(res)
      if(res.status == 200){

       let passData =  res.data
       let driver = passData.filter(arr=>{return arr.role == 'DRIVER'}) 
       driver.forEach((element:any, index) => {
        element.index = index       +1  
        
      });

      this.drivers = driver
      //  console.log(data)
      
      }
    })
  }
  checkType(data){
    console.log(data)
  }

}
