@extends('layouts.admin')

@section('title', 'Parrain du Mois')

@section('content')
<div class="container-fluid">
    {{-- En-tête --}}
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">🏆 Parrain du Mois</h1>
            <p class="text-muted">Classement des meilleurs parrains</p>
        </div>
        <a href="{{ route('admin.top-sponsors.history') }}" class="btn btn-outline-primary">
            <i class="fas fa-history me-1"></i> Historique des récompenses
        </a>
    </div>

    {{-- Sélecteur de période --}}
    <div class="card mb-4">
        <div class="card-body">
            <form action="{{ route('admin.top-sponsors.index') }}" method="GET" class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Mois</label>
                    <select name="month" class="form-select">
                        @for($m = 1; $m <= 12; $m++)
                            <option value="{{ $m }}" {{ $month == $m ? 'selected' : '' }}>
                                {{ \Carbon\Carbon::create()->month($m)->translatedFormat('F') }}
                            </option>
                        @endfor
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Année</label>
                    <select name="year" class="form-select">
                        @for($y = now()->year; $y >= 2020; $y--)
                            <option value="{{ $y }}" {{ $year == $y ? 'selected' : '' }}>{{ $y }}</option>
                        @endfor
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">Voir</button>
                </div>
            </form>
        </div>
    </div>

    {{-- Statistiques --}}
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card bg-primary text-white">
                <div class="card-body text-center">
                    <h2 class="mb-0">{{ number_format($stats['total_referrals_this_month']) }}</h2>
                    <p class="mb-0">Filleuls ce mois</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-success text-white">
                <div class="card-body text-center">
                    <h2 class="mb-0">{{ number_format($stats['active_sponsors']) }}</h2>
                    <p class="mb-0">Parrains actifs</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-info text-white">
                <div class="card-body text-center">
                    <h2 class="mb-0">{{ number_format($stats['total_commissions']) }} FCFA</h2>
                    <p class="mb-0">Commissions générées</p>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        {{-- Top 1 - Parrain du Mois --}}
        <div class="col-lg-4 mb-4">
            @if($sponsorOfMonth)
            <div class="card border-warning">
                <div class="card-header bg-warning text-dark text-center">
                    <h5 class="mb-0">🏆 Parrain du Mois</h5>
                    <small>{{ \Carbon\Carbon::create()->month($month)->translatedFormat('F') }} {{ $year }}</small>
                </div>
                <div class="card-body text-center">
                    <div class="mb-3">
                        @if($sponsorOfMonth['user']->avatar_url)
                            <img src="{{ $sponsorOfMonth['user']->avatar_url }}" class="rounded-circle" width="100" height="100">
                        @else
                            <div class="bg-warning text-dark rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 100px; height: 100px; font-size: 2.5rem;">
                                {{ strtoupper(substr($sponsorOfMonth['user']->name, 0, 2)) }}
                            </div>
                        @endif
                    </div>
                    <h4 class="mb-1">{{ $sponsorOfMonth['user']->name }}</h4>
                    <p class="text-muted mb-3">{{ $sponsorOfMonth['user']->email }}</p>
                    
                    <div class="row text-center mb-4">
                        <div class="col-4">
                            <h5 class="mb-0">{{ $sponsorOfMonth['referrals_count'] }}</h5>
                            <small class="text-muted">Filleuls</small>
                        </div>
                        <div class="col-4">
                            <h5 class="mb-0">{{ number_format($sponsorOfMonth['total_purchases']) }}</h5>
                            <small class="text-muted">Achats (FCFA)</small>
                        </div>
                        <div class="col-4">
                            <h5 class="mb-0 text-success">{{ number_format($sponsorOfMonth['score'], 1) }}</h5>
                            <small class="text-muted">Score</small>
                        </div>
                    </div>

                    <button type="button" class="btn btn-warning w-100" data-bs-toggle="modal" data-bs-target="#awardModal" 
                            data-user-id="{{ $sponsorOfMonth['user']->id }}" 
                            data-user-name="{{ $sponsorOfMonth['user']->name }}">
                        🎁 Attribuer le bonus
                    </button>
                </div>
            </div>
            @else
            <div class="card">
                <div class="card-body text-center py-5">
                    <i class="fas fa-trophy fa-3x text-muted mb-3"></i>
                    <p class="text-muted">Aucun parrain actif ce mois</p>
                </div>
            </div>
            @endif
        </div>

        {{-- Classement complet --}}
        <div class="col-lg-8 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Classement des Parrains</h5>
                </div>
                <div class="card-body p-0">
                    @if(count($leaderboard) > 0)
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th width="60">Rang</th>
                                    <th>Parrain</th>
                                    <th class="text-center">Filleuls</th>
                                    <th class="text-center">Achats</th>
                                    <th class="text-center">Commissions</th>
                                    <th class="text-center">Score</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($leaderboard as $index => $sponsor)
                                <tr class="{{ $index < 3 ? 'table-warning' : '' }}">
                                    <td>
                                        @if($index === 0)
                                            <span class="fs-4">🥇</span>
                                        @elseif($index === 1)
                                            <span class="fs-4">🥈</span>
                                        @elseif($index === 2)
                                            <span class="fs-4">🥉</span>
                                        @else
                                            <span class="badge bg-secondary">{{ $index + 1 }}</span>
                                        @endif
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            @if($sponsor['user']->avatar_url)
                                                <img src="{{ $sponsor['user']->avatar_url }}" class="rounded-circle me-2" width="40" height="40">
                                            @else
                                                <div class="bg-light rounded-circle me-2 d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                                                    {{ strtoupper(substr($sponsor['user']->name, 0, 1)) }}
                                                </div>
                                            @endif
                                            <div>
                                                <strong>{{ $sponsor['user']->name }}</strong>
                                                <br><small class="text-muted">{{ $sponsor['user']->email }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="text-center">
                                        <span class="badge bg-primary">{{ $sponsor['referrals_count'] }}</span>
                                    </td>
                                    <td class="text-center">
                                        {{ number_format($sponsor['total_purchases'], 0, ',', ' ') }}
                                    </td>
                                    <td class="text-center">
                                        {{ number_format($sponsor['total_commissions'], 0, ',', ' ') }}
                                    </td>
                                    <td class="text-center">
                                        <strong class="text-success">{{ number_format($sponsor['score'], 1) }}</strong>
                                    </td>
                                    <td>
                                        <button type="button" class="btn btn-sm btn-outline-warning" data-bs-toggle="modal" data-bs-target="#awardModal"
                                                data-user-id="{{ $sponsor['user']->id }}" 
                                                data-user-name="{{ $sponsor['user']->name }}">
                                            🎁
                                        </button>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    @else
                    <div class="text-center py-5">
                        <i class="fas fa-users fa-3x text-muted mb-3"></i>
                        <p class="text-muted">Aucun parrain actif pour cette période</p>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

{{-- Modal Attribution Bonus --}}
<div class="modal fade" id="awardModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('admin.top-sponsors.award') }}" method="POST">
                @csrf
                <input type="hidden" name="user_id" id="award-user-id">
                <input type="hidden" name="month" value="{{ $month }}">
                <input type="hidden" name="year" value="{{ $year }}">
                
                <div class="modal-header bg-warning">
                    <h5 class="modal-title">🎁 Attribuer le Bonus</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <p>Attribuer le bonus à <strong id="award-user-name"></strong></p>
                    
                    <div class="mb-3">
                        <label class="form-label">Montant du bonus (FCFA)</label>
                        <input type="number" name="bonus_amount" class="form-control" value="50000" min="0" required>
                    </div>
                    
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-1"></i>
                        Le bonus sera crédité directement dans le portefeuille du membre.
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
                    <button type="submit" class="btn btn-warning">🎁 Attribuer le Bonus</button>
                </div>
            </form>
        </div>
    </div>
</div>

@push('scripts')
<script>
document.getElementById('awardModal').addEventListener('show.bs.modal', function (event) {
    const button = event.relatedTarget;
    const userId = button.getAttribute('data-user-id');
    const userName = button.getAttribute('data-user-name');
    
    document.getElementById('award-user-id').value = userId;
    document.getElementById('award-user-name').textContent = userName;
});
</script>
@endpush
@endsection
