import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HitAWodService } from 'src/app/services/hit-a-wod.service';
import { BoxOrder } from 'src/app/models/box-order.model';
import { UserSubscription } from 'src/app/models/user-subscription.model';
import { User } from 'src/app/models/user.model';
import { SubscriptionService } from 'src/app/services/subscription.service';
import { UtilsService } from 'src/app/services/utils.service';

@Component({
  selector: 'app-billing-detail',
  templateUrl: './billing-detail.component.html',
  styleUrls: ['./billing-detail.component.css']
})
export class BillingDetailComponent implements OnInit {

  order_id:string;
  order:BoxOrder;
  invoiceHTML: any;
  loading: boolean = true;
  subscription_id: number;

  edit_codi:boolean = false;
  edit_date:boolean = false;

  emitted_date:Date;
  invoice_codi:string;

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

  ngOnInit(): void {
    this.order_id = this.activatedRoute.snapshot.paramMap.get("orderId");
    this.subscriptionService.currentSubscription.subscribe(
      data => {
        if(data.role.slug === "box_admin" || data.role.slug==="company_admin") {
          this.getOrder();
        }
        else {
          this.utilsService.showMessage("error", "Debes ser administrador", "Por favor selecciona una suscripción en el que seas administrador.");
        }
      }
    );
  }
  
  private getOrder(){
    this.loading = true;
    this.api.getOrder(parseInt(this.order_id)).subscribe( o => {
      this.order = o;
      if(this.order.invoice) {
        this.emitted_date = new Date(this.order.invoice.emitted_date);
      }
      this.loading = false;
      this.checkSubscription();
      /* if (!this.invoiceHTML) {
        this.api.getHTMLInvoice(this.order.invoice_id.toString()).subscribe( i => {
          this.invoiceHTML = i;
        });
      } */ // TODO PDF
    });
  }

  private checkSubscription() {
    if (this.order && this.order.lines) {
      this.order.lines.forEach( l => {
        if (l.user_subscription_id) this.subscription_id = l.user_subscription_id;
      });
    }
  }

  updateInvoice() {
    this.edit_codi = false;
    this.edit_date = false;
    let body = {
      "invoice_prefix": this.order.invoice.invoice_prefix,
      "invoice_serie": this.order.invoice.invoice_serie,
      "invoice_number": this.order.invoice.invoice_number,
      "emitted_date": this.dateFormat(this.emitted_date)
    }
    this.api.updateInvoice(this.order.invoice.id, this.order.box_id.toString(),body).subscribe( i => {
      this.order.invoice = i;
      this.emitted_date = new Date(i.emitted_date);
    });
  }

  dateFormat(date: Date) {
    if (!date) return null;
    const month = (date.getMonth()+1)<=9 ? '0'+(date.getMonth()+1) : (date.getMonth()+1);
    const day = date.getDate()<=9 ? '0'+date.getDate() : date.getDate()
    return date.getFullYear() + '-' + month + '-' + day;
  }
}
