import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { GridModule } from '@progress/kendo-angular-grid';
import { DatePickerModule } from '@progress/kendo-angular-dateinputs';
import { ButtonsModule } from '@progress/kendo-angular-buttons';
import { Form990ReportApiService } from '../../services/form990-report-api.service';
import { FunctionalExpenseStatementDto } from '../../models/form990-report.model';

@Component({
  selector: 'app-form990-report-page',
  standalone: true,
  imports: [CommonModule, FormsModule, GridModule, DatePickerModule, ButtonsModule],
  templateUrl: './form990-report-page.component.html',
})
export class Form990ReportPageComponent implements OnInit {
  from: Date = new Date(new Date().getFullYear(), 0, 1);
  to: Date   = new Date(new Date().getFullYear(), 11, 31);
  statement: FunctionalExpenseStatementDto | null = null;
  loading = false;

  constructor(private api: Form990ReportApiService) {}

  ngOnInit(): void {
    this.load();
  }

  load(): void {
    this.loading = true;
    const fmt = (date: Date): string => {
      const year  = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day   = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    };
    this.api.getFunctionalExpenses(fmt(this.from), fmt(this.to)).subscribe({
      next: (statement) => {
        this.statement = statement;
        this.loading = false;
      },
      error: () => {
        this.loading = false;
      },
    });
  }
}
