import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { FabAlign } from '@progress/kendo-angular-buttons';
import { Observable } from 'rxjs';
import { Customer } from 'src/app/models/customer.model';
import { CustomerService } from 'src/app/services/customer.service';

@Component({
  selector: 'app-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.scss']
})
export class CustomerComponent implements OnInit {
  public listItems: Array<string> = [
    'Nome',
    'CPF',
    'Telefone',
    'Celular'
  ];
  public listFields: Array<string> = [
    'name',
    'document',
    'phone',
    'cellPhone'
  ];
  public topEnd: FabAlign = { vertical: 'top', horizontal: 'end' };
  customer$: Observable<Customer[]>;
  public errorObject = null;
  public isloading = false;
  public searchForm: FormGroup;

  constructor(private customerService: CustomerService) {
    this.searchForm = new FormGroup({
      search: new FormControl(),
      textSearch: new FormControl(),
    });
  }

  ngOnInit(): void {
  }

  searchCustomer(event){
    if (event.keyCode === 13) {
      console.log(this.searchForm.value);
      const indice = this.listItems.indexOf(this.searchForm.get('search').value);
      const field =  this.listFields[indice];
      const textSearh = this.searchForm.get('textSearch').value;
      this.customer$ = this.customerService.getCustomerByQuery(field, textSearh);
     // const filter: FilterProduct = { brands: [], text: this.searchProduct.value };
     // this.getSaleProducts(filter);
    }

  }

}
