import { Component, OnInit } from '@angular/core';
import { HitAWodService } from 'src/app/services/hit-a-wod.service';
import { SubscriptionService } from 'src/app/services/subscription.service';
import { Subscription } from 'src/app/models/subscription.model';
import { UtilsService } from 'src/app/services/utils.service';

@Component({
  selector: 'app-business-history-renewals',
  templateUrl: './business-history-renewals.component.html',
  styleUrls: ['./business-history-renewals.component.css']
})
export class BusinessHistoryRenewalsComponent implements OnInit {

  company_id:number;
  subscriptions:Subscription[];
  selected_subscription:Subscription;

  constructor(private api:HitAWodService,
              private subscriptionService:SubscriptionService,
              public utilsService:UtilsService) { }

  ngOnInit(): void {
    this.subscriptionService.currentSubscription.subscribe(
      data => {
        if(data.role.slug === "box_admin" || data.role.slug==="company_admin") {
          this.company_id = data.company.id;
          this.fetchCompanySubscriptionsAndOrders();
        }
      }
    );
  }

  private fetchCompanySubscriptionsAndOrders() {
    this.api.getCompanySubscriptionsAndOrders(this.company_id.toString()).subscribe(
      data => {
        this.subscriptions = data.data;
        if(this.subscriptions.length) this.selected_subscription = this.subscriptions[0];
      }
    );
  }

}
