
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { DateRangeService } from '@progress/kendo-angular-dateinputs';
import { WindowState } from '@progress/kendo-angular-dialog';
import { Observable } from 'rxjs';
import { PartnerSales } from 'src/app/models/partners-sales.model';
import { Partners } from 'src/app/models/partners.model';
import { LookupService } from 'src/app/services/lookup.service';
import { PartnerService } from 'src/app/services/partner.service';

@Component({
  selector: 'app-commission',
  templateUrl: './commission.component.html',
  styleUrls: ['./commission.component.scss']
})
export class CommissionComponent implements OnInit {

  formFilter: FormGroup;
  partners$: Observable<Partners[]>;
  selectedPartner: Partners;

  @ViewChild('daterange', { read: DateRangeService })
  public service: DateRangeService;
  public range = { start: new Date(), end: new Date() };
  public windowState: WindowState = 'maximized';
  public opened = false;
  public urlPrintPreOrder = '';

  constructor(
    private readonly lookupService: LookupService,
    private readonly partnerService: PartnerService,
  ) {
    this.formFilter = new FormGroup({
      cpf: new FormControl(null),
      name: new FormControl(null),
      partner: new FormControl(null),
      type: new FormControl('T'),
    });
  }

  ngOnInit(): void {
    this.partners$ = this.partnerService.getPartnerByQuery('T', null, null);
  }

  public openClose(isOpened: boolean): void {
    if (isOpened) {

      let partnerId = -1;
      if (this.selectedPartner !== null && this.selectedPartner !== undefined) {
        partnerId = this.selectedPartner.id;
      }
      let intType = 99;
      const type = this.formFilter.get('type').value;
      switch (type) {
        case 'P':
          intType = 1;
          break;
        case 'M':
          intType = 2;
          break;
        case 'B':
          intType = 3;
          break;
        default:
          intType = 99;
          break;
      }
      console.log("tipo: " + intType);
      this.urlPrintPreOrder =
        `http://172.35.0.219:8068/Viewer/{action}?partnerId=${partnerId}&type=${type}&start=${this.range.start.toUTCString()}&end=${this.range.end.toUTCString()}`;

      //  `http://localhost:52986/Viewer/{action}?partnerId=${this.selectedPartner.id}
      // &start=${this.range.start.toUTCString()}&end=${this.range.end.toUTCString()}`;
    }
    this.opened = isOpened;
  }

  public selectPartner(partner: any) {
    console.log(partner);
    this.selectedPartner = null;
    if (partner) {
      this.selectedPartner = partner;
    }
  }


}
