import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-dashboard-finance',
  templateUrl: './dashboard-finance.component.html',
  styleUrls: ['./dashboard-finance.component.scss']
})
export class DashboardFinanceComponent implements OnInit {
  
  public autoRefresh = false;
  public selectedPeriod = 'month';
  public showFinanceKPIs = true;
  public showTrendAnalysis = true;
  
  public financeKPIs = [
    { title: 'Chiffre d\'Affaires', value: '€125,430', icon: 'euro_symbol', color: '#4AA8DC', trend: 'up', variation: 15 },
    { title: 'Bénéfices', value: '€32,150', icon: 'trending_up', color: '#10B981', trend: 'up', variation: 22 },
    { title: 'Dépenses', value: '€93,280', icon: 'trending_down', color: '#EF4444', trend: 'down', variation: 5 },
    { title: 'ROI', value: '34.5%', icon: 'percent', color: '#F59E0B', trend: 'up', variation: 8 }
  ];
  
  public chartData = {
    revenue: [
      { period: 'Jan', value: 1800000 },
      { period: 'Fév', value: 2100000 },
      { period: 'Mar', value: 1950000 },
      { period: 'Avr', value: 2300000 },
      { period: 'Mai', value: 2500000 },
      { period: 'Jun', value: 2200000 }
    ],
    expenses: [
      { category: 'Personnel', value: 45, color: '#4AA8DC' },
      { category: 'Infrastructure', value: 25, color: '#8DA969' },
      { category: 'Marketing', value: 20, color: '#F59E0B' },
      { category: 'Autres', value: 10, color: '#EF4444' }
    ]
  };
  
  public recentTransactions = [
    { date: '15/01/2024', description: 'Paiement client ABC', category: 'Revenus', type: 'income', amount: 5000, status: 'validated' },
    { date: '14/01/2024', description: 'Achat matériel', category: 'Dépenses', type: 'expense', amount: -1200, status: 'processed' },
    { date: '13/01/2024', description: 'Facture hébergement', category: 'Services', type: 'expense', amount: -89, status: 'pending' }
  ];
  
  public projectBudgets = [
    { name: 'Projet Alpha', code: 'PA-001', allocated: 50000, spent: 35000, remaining: 15000, progress: 70, status: 'on-track' },
    { name: 'Projet Beta', code: 'PB-002', allocated: 30000, spent: 28000, remaining: 2000, progress: 93, status: 'at-risk' },
    { name: 'Projet Gamma', code: 'PG-003', allocated: 75000, spent: 45000, remaining: 30000, progress: 60, status: 'on-track' }
  ];

  constructor() { }

  ngOnInit(): void {
  }

  toggleAutoRefresh(): void {
    this.autoRefresh = !this.autoRefresh;
  }

  refreshFinanceData(): void {
    console.log('Actualisation des données financières');
  }

  changePeriod(period: string): void {
    this.selectedPeriod = period;
  }

  getVariationClass(trend: string): string {
    return trend === 'up' ? 'trend-positive' : trend === 'down' ? 'trend-negative' : '';
  }

  trackByKpiId(index: number, kpi: any): string {
    return kpi.title;
  }

  trackByTransactionId(index: number, transaction: any): string {
    return transaction.date + transaction.description;
  }

  trackByBudgetId(index: number, budget: any): string {
    return budget.code;
  }

  formatAmount(amount: number): string {
    return new Intl.NumberFormat('fr-FR', {
      style: 'currency',
      currency: 'EUR'
    }).format(amount);
  }

  getStatusClass(status: string): string {
    switch (status) {
      case 'validated':
      case 'on-track':
        return 'status-success';
      case 'processed':
        return 'status-info';
      case 'pending':
        return 'status-warning';
      case 'at-risk':
        return 'status-danger';
      default:
        return 'status-default';
    }
  }

  getStatusLabel(status: string): string {
    switch (status) {
      case 'validated': return 'Validé';
      case 'processed': return 'Traité';
      case 'pending': return 'En attente';
      case 'on-track': return 'Dans les temps';
      case 'at-risk': return 'À risque';
      default: return status;
    }
  }

  getCurrentTime(): Date {
    return new Date();
  }
}