import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { FormatSettings } from '@progress/kendo-angular-dateinputs';
import { ComboBoxComponent } from '@progress/kendo-angular-dropdowns';
import { CheckoutOrder } from 'src/app/models/checkout-order.model';
import { Checkout } from 'src/app/models/checkout.model';
import { StoreERP } from 'src/app/models/store.model';
import { User } from 'src/app/models/user.model';
import { LookupService } from 'src/app/services/lookup.service';

@Component({
  selector: 'app-checkout',
  templateUrl: './checkout.component.html',
  styleUrls: ['./checkout.component.css']
})
export class CheckoutComponent implements OnInit, OnDestroy {
  @ViewChild('combo') public combo: ComboBoxComponent;

  public stores: StoreERP[] = [];
  public checkouts: Checkout[] = [];
  public users: User[] = [];
  public checkoutOrders: CheckoutOrder[] = [];
  public value: Date = new Date();
  public format: FormatSettings = {
    displayFormat: 'dd/MM/yyyy',
    inputFormat: 'dd/MM/yyyy',
  };

  public selectStore: StoreERP;
  public selectCheckout: Checkout;
  public selectUser: User;
  public isDisabledCheckout = true;
  public isDisabledUser = true;

  constructor(private lookupService: LookupService) {}

  ngOnInit(): void {
    this.lookupService.getStore().subscribe((data) => (this.stores = data));
  }

  ngOnDestroy(): void {
    throw new Error('Method not implemented.');
   /*****NAO ESQUECER DO UNSUBSCRIBE************/
  }

  fechDataStore(idStore: string) {
    this.lookupService.getCheckout(idStore).subscribe((data) => (this.checkouts = data));
    this.lookupService.getUser(idStore).subscribe((data) => (this.users = data));
  }

  handleStoreChange(value) {
    this.selectStore = value;

    if (value === undefined) {
      this.isDisabledCheckout = true;
      this.checkouts = [];
      this.isDisabledUser = true;
      this.users = [];
    } else {
      this.isDisabledCheckout = false;
      this.lookupService.getCheckout(value.id).subscribe((data) => (this.checkouts = data));
      this.isDisabledUser = false;
      this.lookupService.getUser(value.id).subscribe((data) => (this.users = data));
    }
  }

  handleCheckoutChange(value) {
    this.selectCheckout = value;
  }


  handleUserChange(value) {
    this.selectUser = value;
  }

  findOrders(){
    console.log(this.selectStore);
    console.log(this.selectCheckout);
    console.log(this.selectUser);

  }

}
