@extends('layouts.comptabilite')

@section('title', 'Dashboard Comptabilité')
@section('page-title', 'Tableau de Bord Comptabilité')

@section('content')
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="card bg-success text-white h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Revenus Total</h6>
                        <h3 class="mb-0">{{ number_format($stats['total_revenue'], 0, ',', ' ') }}</h3>
                    </div>
                    <i class="bi bi-cash-stack fs-1 opacity-50"></i>
                </div>
                <small>{{ number_format($stats['revenue_month'], 0, ',', ' ') }} XOF ce mois</small>
            </div>
        </div>
    </div>
    
    <div class="col-6 col-md-3">
        <div class="card bg-warning text-dark h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Retraits en attente</h6>
                        <h3 class="mb-0">{{ $stats['pending_withdrawals'] }}</h3>
                    </div>
                    <i class="bi bi-hourglass-split fs-1 opacity-50"></i>
                </div>
                <small>{{ number_format($stats['pending_withdrawals_amount'], 0, ',', ' ') }} XOF</small>
            </div>
        </div>
    </div>
    
    <div class="col-6 col-md-3">
        <div class="card bg-info text-white h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Commissions</h6>
                        <h3 class="mb-0">{{ number_format($stats['total_commissions'], 0, ',', ' ') }}</h3>
                    </div>
                    <i class="bi bi-wallet2 fs-1 opacity-50"></i>
                </div>
                <small>{{ number_format($stats['commissions_month'], 0, ',', ' ') }} XOF ce mois</small>
            </div>
        </div>
    </div>
    
    <div class="col-6 col-md-3">
        <div class="card bg-primary text-white h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-1">Transactions</h6>
                        <h3 class="mb-0">{{ $stats['transactions_today'] }}</h3>
                    </div>
                    <i class="bi bi-arrow-left-right fs-1 opacity-50"></i>
                </div>
                <small>Aujourd'hui</small>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-graph-up me-2"></i>Revenus (7 derniers jours)</h5>
            </div>
            <div class="card-body">
                <canvas id="revenueChart" height="100"></canvas>
            </div>
        </div>
        
        <div class="card mt-3">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-cash me-2"></i>Retraits en attente</h5>
                <a href="{{ route('accountant.withdrawals.index') }}" class="btn btn-sm btn-primary">Voir tout</a>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Utilisateur</th>
                                <th>Montant</th>
                                <th>Date</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($pendingWithdrawals as $withdrawal)
                                <tr>
                                    <td>{{ $withdrawal->user->name }}</td>
                                    <td><strong>{{ number_format($withdrawal->amount, 0, ',', ' ') }} XOF</strong></td>
                                    <td><small>{{ $withdrawal->created_at->format('d/m/Y H:i') }}</small></td>
                                    <td>
                                        <a href="{{ route('accountant.withdrawals.show', $withdrawal) }}" class="btn btn-sm btn-info">
                                            <i class="bi bi-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4" class="text-center text-muted">Aucun retrait en attente</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-percent me-2"></i>Commissions Récentes</h5>
            </div>
            <div class="card-body" style="max-height: 500px; overflow-y: auto;">
                @forelse($recentCommissions as $commission)
                    <div class="d-flex align-items-center mb-3 pb-3 border-bottom">
                        <div class="flex-grow-1">
                            <h6 class="mb-0">{{ $commission->user->name }}</h6>
                            <small class="text-muted">{{ $commission->type }}</small>
                            <br>
                            <small class="text-success">+{{ number_format($commission->amount, 0, ',', ' ') }} XOF</small>
                        </div>
                        <div class="text-end">
                            <small class="text-muted">{{ $commission->created_at->diffForHumans() }}</small>
                        </div>
                    </div>
                @empty
                    <p class="text-center text-muted">Aucune commission récente</p>
                @endforelse
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>
const ctx = document.getElementById('revenueChart');
const revenueData = @json($revenueChart);

const labels = revenueData.map(r => r.date);
const data = revenueData.map(r => r.total);

new Chart(ctx, {
    type: 'bar',
    data: {
        labels: labels,
        datasets: [{
            label: 'Revenus (XOF)',
            data: data,
            backgroundColor: '#198754',
            borderColor: '#146c43',
            borderWidth: 1
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                display: false
            }
        },
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
});
</script>
@endpush
