@extends('layouts.seller')

@section('title', 'Mon Portefeuille')

@push('styles')
<style>
    /* ===== ULTRA MODERN WALLET PAGE ===== */
    .wallet-hero {
        background: linear-gradient(135deg, rgba(139, 92, 246, 0.2) 0%, rgba(99, 102, 241, 0.15) 50%, rgba(59, 130, 246, 0.1) 100%);
        border-radius: 28px;
        padding: 40px;
        margin-bottom: 32px;
        position: relative;
        overflow: hidden;
        border: 1px solid rgba(139, 92, 246, 0.2);
    }
    
    .wallet-hero::before {
        content: '';
        position: absolute;
        top: -100px;
        right: -100px;
        width: 350px;
        height: 350px;
        background: radial-gradient(circle, rgba(139, 92, 246, 0.15) 0%, transparent 70%);
        animation: float 6s ease-in-out infinite;
    }
    
    .wallet-hero::after {
        content: '';
        position: absolute;
        bottom: -80px;
        left: -50px;
        width: 250px;
        height: 250px;
        background: radial-gradient(circle, rgba(59, 130, 246, 0.1) 0%, transparent 70%);
        animation: float 8s ease-in-out infinite reverse;
    }
    
    @keyframes float {
        0%, 100% { transform: translateY(0) rotate(0deg); }
        50% { transform: translateY(-20px) rotate(5deg); }
    }
    
    .hero-content { position: relative; z-index: 1; }
    
    .wallet-label {
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 2px;
        color: #8b5cf6;
        font-weight: 600;
        margin-bottom: 8px;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    
    .hero-title {
        font-size: 2.2rem;
        font-weight: 700;
        background: linear-gradient(135deg, #8b5cf6, #6366f1, #3b82f6);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
        margin-bottom: 8px;
    }
    
    .hero-subtitle { color: var(--muted); font-size: 1rem; }
    
    /* Balance Cards */
    .balance-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 24px;
        margin-bottom: 40px;
    }
    
    .balance-card {
        background: linear-gradient(145deg, #ffffff 0%, #fafbff 100%);
        border-radius: 24px;
        padding: 28px;
        border: 1px solid rgba(139, 92, 246, 0.1);
        position: relative;
        overflow: hidden;
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    .balance-card:hover {
        transform: translateY(-8px);
        box-shadow: 0 25px 50px rgba(139, 92, 246, 0.15);
    }
    
    .balance-card.main {
        background: linear-gradient(135deg, #8b5cf6 0%, #6366f1 50%, #4f46e5 100%);
        color: white;
        grid-column: span 2;
    }
    
    @media (max-width: 768px) { .balance-card.main { grid-column: span 1; } }
    
    .balance-card.main .balance-label { color: rgba(255,255,255,0.8); }
    .balance-card.main .balance-value { color: white; font-size: 3rem; }
    
    .balance-card::before {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        width: 120px;
        height: 120px;
        background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
        border-radius: 50%;
        transform: translate(30%, -30%);
    }
    
    .balance-icon {
        width: 56px;
        height: 56px;
        border-radius: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.5rem;
        margin-bottom: 20px;
        background: linear-gradient(135deg, rgba(139, 92, 246, 0.15), rgba(99, 102, 241, 0.1));
        color: #8b5cf6;
    }
    
    .balance-card.main .balance-icon {
        background: rgba(255,255,255,0.2);
        color: white;
    }
    
    .balance-card.pending .balance-icon { background: linear-gradient(135deg, rgba(245, 158, 11, 0.15), rgba(217, 119, 6, 0.1)); color: #f59e0b; }
    .balance-card.earnings .balance-icon { background: linear-gradient(135deg, rgba(34, 197, 94, 0.15), rgba(22, 163, 74, 0.1)); color: #22c55e; }
    
    .balance-label {
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 1px;
        color: var(--muted);
        font-weight: 600;
        margin-bottom: 8px;
    }
    
    .balance-value {
        font-size: 2rem;
        font-weight: 700;
        color: var(--text);
    }
    
    .balance-value span { font-size: 1rem; font-weight: 500; opacity: 0.7; }
    
    .balance-trend {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        margin-top: 12px;
        padding: 6px 12px;
        border-radius: 20px;
        font-size: 0.8rem;
        font-weight: 600;
    }
    
    .balance-trend.up { background: rgba(34, 197, 94, 0.1); color: #22c55e; }
    .balance-trend.down { background: rgba(239, 68, 68, 0.1); color: #ef4444; }
    .balance-card.main .balance-trend { background: rgba(255,255,255,0.2); color: white; }
    
    /* Quick Actions */
    .quick-actions {
        display: flex;
        gap: 16px;
        margin-bottom: 40px;
        flex-wrap: wrap;
    }
    
    .quick-action-btn {
        flex: 1;
        min-width: 200px;
        background: #ffffff;
        border: 2px solid rgba(139, 92, 246, 0.1);
        border-radius: 16px;
        padding: 20px 24px;
        display: flex;
        align-items: center;
        gap: 16px;
        text-decoration: none;
        color: var(--text);
        transition: all 0.3s ease;
    }
    
    .quick-action-btn:hover {
        border-color: #8b5cf6;
        background: linear-gradient(135deg, rgba(139, 92, 246, 0.05), rgba(99, 102, 241, 0.02));
        transform: translateY(-4px);
        box-shadow: 0 10px 30px rgba(139, 92, 246, 0.1);
        color: var(--text);
    }
    
    .quick-action-btn.primary {
        background: linear-gradient(135deg, #8b5cf6, #6366f1);
        border-color: transparent;
        color: white;
    }
    
    .quick-action-btn.primary:hover {
        transform: translateY(-4px);
        box-shadow: 0 15px 35px rgba(139, 92, 246, 0.35);
        color: white;
    }
    
    .quick-action-icon {
        width: 50px;
        height: 50px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.3rem;
        background: linear-gradient(135deg, rgba(139, 92, 246, 0.1), rgba(99, 102, 241, 0.05));
        color: #8b5cf6;
    }
    
    .quick-action-btn.primary .quick-action-icon {
        background: rgba(255,255,255,0.2);
        color: white;
    }
    
    .quick-action-text h4 { font-size: 1rem; font-weight: 600; margin: 0 0 4px 0; }
    .quick-action-text p { font-size: 0.8rem; color: var(--muted); margin: 0; }
    .quick-action-btn.primary .quick-action-text p { color: rgba(255,255,255,0.8); }
    
    /* Transactions Section */
    .transactions-card {
        background: #ffffff;
        border-radius: 24px;
        overflow: hidden;
        border: 1px solid rgba(139, 92, 246, 0.1);
        box-shadow: 0 4px 30px rgba(0, 0, 0, 0.03);
    }
    
    .transactions-header {
        background: linear-gradient(135deg, rgba(139, 92, 246, 0.08), rgba(99, 102, 241, 0.04));
        padding: 24px 28px;
        border-bottom: 1px solid rgba(139, 92, 246, 0.1);
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 16px;
    }
    
    .transactions-title h3 {
        font-size: 1.25rem;
        font-weight: 700;
        color: var(--text);
        margin: 0;
    }
    
    .transactions-title p {
        font-size: 0.85rem;
        color: var(--muted);
        margin: 4px 0 0 0;
    }
    
    .filter-tabs {
        display: flex;
        background: #f1f5f9;
        border-radius: 12px;
        padding: 4px;
    }
    
    .filter-tab {
        padding: 10px 20px;
        border-radius: 10px;
        border: none;
        background: transparent;
        font-size: 0.85rem;
        font-weight: 600;
        color: var(--muted);
        cursor: pointer;
        transition: all 0.2s ease;
    }
    
    .filter-tab.active, .filter-tab:hover {
        background: white;
        color: #8b5cf6;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .transaction-list { padding: 0; }
    
    .transaction-item {
        display: flex;
        align-items: center;
        gap: 16px;
        padding: 20px 28px;
        border-bottom: 1px solid #f1f5f9;
        transition: all 0.2s ease;
    }
    
    .transaction-item:last-child { border-bottom: none; }
    .transaction-item:hover { background: rgba(139, 92, 246, 0.02); }
    
    .transaction-icon {
        width: 48px;
        height: 48px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.2rem;
        flex-shrink: 0;
    }
    
    .transaction-icon.credit { background: linear-gradient(135deg, rgba(34, 197, 94, 0.15), rgba(22, 163, 74, 0.1)); color: #22c55e; }
    .transaction-icon.debit { background: linear-gradient(135deg, rgba(239, 68, 68, 0.15), rgba(220, 38, 38, 0.1)); color: #ef4444; }
    .transaction-icon.pending { background: linear-gradient(135deg, rgba(245, 158, 11, 0.15), rgba(217, 119, 6, 0.1)); color: #f59e0b; }
    
    .transaction-details { flex: 1; min-width: 0; }
    
    .transaction-title {
        font-weight: 600;
        color: var(--text);
        margin-bottom: 4px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    .transaction-meta {
        font-size: 0.8rem;
        color: var(--muted);
        display: flex;
        align-items: center;
        gap: 12px;
    }
    
    .transaction-amount {
        font-size: 1.1rem;
        font-weight: 700;
        text-align: right;
    }
    
    .transaction-amount.credit { color: #22c55e; }
    .transaction-amount.debit { color: #ef4444; }
    
    .transaction-amount span {
        display: block;
        font-size: 0.75rem;
        font-weight: 500;
        color: var(--muted);
    }
    
    .status-pill {
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 0.7rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    .status-pill.completed { background: rgba(34, 197, 94, 0.1); color: #22c55e; }
    .status-pill.pending { background: rgba(245, 158, 11, 0.1); color: #f59e0b; }
    .status-pill.failed { background: rgba(239, 68, 68, 0.1); color: #ef4444; }
    
    /* Empty State */
    .empty-state { text-align: center; padding: 60px 20px; }
    
    .empty-icon {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        background: linear-gradient(135deg, rgba(139, 92, 246, 0.1), rgba(99, 102, 241, 0.05));
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 24px;
        font-size: 2.5rem;
        color: #8b5cf6;
    }
    
    .empty-title { font-size: 1.25rem; font-weight: 700; color: var(--text); margin-bottom: 8px; }
    .empty-text { color: var(--muted); font-size: 0.95rem; }
    
    .pagination-wrapper { padding: 24px 28px; border-top: 1px solid #f1f5f9; }
    
    /* Animations */
    .fade-in { animation: fadeIn 0.5s ease forwards; }
    @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
    .stagger-1 { animation-delay: 0.1s; }
    .stagger-2 { animation-delay: 0.2s; }
    .stagger-3 { animation-delay: 0.3s; }
</style>
@endpush

@section('content')
<div class="page-wrapper">
    <div class="wallet-hero fade-in">
        <div class="hero-content">
            <div class="wallet-label">
                <i class="bi bi-wallet2"></i> Mon portefeuille vendeur
            </div>
            <h1 class="hero-title">Gestion Financière</h1>
            <p class="hero-subtitle">Suivez vos revenus, retraits et transactions en temps réel</p>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show mb-4" role="alert" style="border-radius: 16px; border: none; background: linear-gradient(135deg, rgba(34, 197, 94, 0.1), rgba(22, 163, 74, 0.05)); color: #16a34a;">
            <i class="bi bi-check-circle-fill me-2"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert" style="border-radius: 16px; border: none; background: linear-gradient(135deg, rgba(239, 68, 68, 0.1), rgba(220, 38, 38, 0.05)); color: #dc2626;">
            <i class="bi bi-exclamation-circle-fill me-2"></i>{{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="balance-grid">
        <div class="balance-card main fade-in stagger-1">
            <div class="balance-icon"><i class="bi bi-wallet2"></i></div>
            <div class="balance-label">Solde disponible</div>
            <div class="balance-value">{{ number_format($balance ?? $wallet->balance ?? 0, 0, ',', ' ') }} <span>XOF</span></div>
            @if(($balance ?? $wallet->balance ?? 0) > 0)
            <div class="balance-trend up"><i class="bi bi-arrow-up"></i> Disponible au retrait</div>
            @else
            <div class="balance-trend" style="background: rgba(255,255,255,0.15);"><i class="bi bi-info-circle"></i> Crédité après approbation livraison</div>
            @endif
        </div>
        <div class="balance-card pending fade-in stagger-2">
            <div class="balance-icon"><i class="bi bi-hourglass-split"></i></div>
            <div class="balance-label">En attente de validation</div>
            <div class="balance-value">{{ number_format($pendingBalance ?? 0, 0, ',', ' ') }} <span>XOF</span></div>
            @if(($pendingBalance ?? 0) > 0)
            <small class="text-muted d-block mt-2"><i class="bi bi-info-circle me-1"></i>En attente d'approbation preuve livraison</small>
            @endif
        </div>
        <div class="balance-card earnings fade-in stagger-3">
            <div class="balance-icon"><i class="bi bi-graph-up-arrow"></i></div>
            <div class="balance-label">Revenus ce mois</div>
            <div class="balance-value">{{ number_format($monthlyEarnings ?? 0, 0, ',', ' ') }} <span>XOF</span></div>
            @php
                $trend = ($lastMonthEarnings ?? 0) > 0 ? round((($monthlyEarnings ?? 0) - ($lastMonthEarnings ?? 0)) / ($lastMonthEarnings ?? 1) * 100) : 0;
            @endphp
            @if($trend != 0)
            <div class="balance-trend {{ $trend > 0 ? 'up' : 'down' }}">
                <i class="bi bi-arrow-{{ $trend > 0 ? 'up' : 'down' }}"></i> {{ abs($trend) }}% vs mois dernier
            </div>
            @endif
        </div>
    </div>

    <!-- Info Box pour expliquer le système -->
    @if(($pendingBalance ?? 0) > 0 && ($balance ?? $wallet->balance ?? 0) == 0)
    <div class="alert mb-4" style="background: linear-gradient(135deg, rgba(139, 92, 246, 0.1), rgba(99, 102, 241, 0.05)); border: 1px solid rgba(139, 92, 246, 0.2); border-radius: 16px; padding: 20px;">
        <div class="d-flex align-items-start gap-3">
            <div style="width: 48px; height: 48px; border-radius: 12px; background: rgba(139, 92, 246, 0.15); display: flex; align-items: center; justify-content: center;">
                <i class="bi bi-lightbulb text-primary fs-5"></i>
            </div>
            <div>
                <h6 class="fw-bold mb-1" style="color: #7c3aed;">Comment ça fonctionne ?</h6>
                <p class="mb-0 text-muted" style="font-size: 0.9rem;">
                    Vos revenus sont en attente jusqu'à ce que vous soumettiez une <strong>preuve de livraison</strong> et que l'administrateur l'approuve. 
                    Une fois approuvée, le montant sera automatiquement crédité sur votre solde disponible.
                </p>
            </div>
        </div>
    </div>
    @endif

    <div class="quick-actions fade-in">
        <a href="{{ route('seller.wallet.withdrawals') }}" class="quick-action-btn primary">
            <div class="quick-action-icon"><i class="bi bi-cash-stack"></i></div>
            <div class="quick-action-text">
                <h4>Demander un retrait</h4>
                <p>Transférer vers votre compte</p>
            </div>
        </a>
        <a href="{{ route('seller.wallet.withdrawals') }}" class="quick-action-btn">
            <div class="quick-action-icon"><i class="bi bi-clock-history"></i></div>
            <div class="quick-action-text">
                <h4>Historique complet</h4>
                <p>Voir toutes les transactions</p>
            </div>
        </a>
        <a href="{{ route('seller.profile.index') }}#payment" class="quick-action-btn">
            <div class="quick-action-icon"><i class="bi bi-credit-card"></i></div>
            <div class="quick-action-text">
                <h4>Méthodes de paiement</h4>
                <p>Gérer vos comptes</p>
            </div>
        </a>
    </div>

    <div class="transactions-card fade-in">
        <div class="transactions-header">
            <div class="transactions-title">
                <h3><i class="bi bi-arrow-left-right me-2"></i>Transactions récentes</h3>
                <p>Dernières opérations sur votre portefeuille</p>
            </div>
            <div class="filter-tabs">
                <button class="filter-tab active" data-filter="all">Toutes</button>
                <button class="filter-tab" data-filter="credit">Entrées</button>
                <button class="filter-tab" data-filter="debit">Sorties</button>
            </div>
        </div>

        @if(isset($transactions) && $transactions->count() > 0)
        <div class="transaction-list">
            @foreach($transactions as $transaction)
            @php
                // Types de crédit (entrées d'argent)
                $creditTypes = ['sale', 'seller_sale', 'commission', 'refund', 'credit', 'bonus'];
                $isCredit = in_array($transaction->type, $creditTypes) || $transaction->amount > 0;
                // Si le montant est négatif, c'est une sortie peu importe le type
                if ($transaction->amount < 0) $isCredit = false;
                $iconClass = $isCredit ? 'credit' : 'debit';
                $icon = $isCredit ? 'bi-arrow-down-left' : 'bi-arrow-up-right';
                if($transaction->status === 'pending') { $iconClass = 'pending'; $icon = 'bi-hourglass-split'; }
            @endphp
            <div class="transaction-item" data-type="{{ $isCredit ? 'credit' : 'debit' }}">
                <div class="transaction-icon {{ $iconClass }}"><i class="bi {{ $icon }}"></i></div>
                <div class="transaction-details">
                    <div class="transaction-title">
                        @switch($transaction->type)
                            @case('sale') Vente reçue @break
                            @case('seller_sale') Vente reçue @break
                            @case('commission') Commission @break
                            @case('withdrawal') Retrait @break
                            @case('withdrawal_request') Demande de retrait @break
                            @case('refund') Remboursement @break
                            @case('fee') Frais @break
                            @case('bonus') Bonus @break
                            @case('credit') Crédit @break
                            @default {{ ucfirst(str_replace('_', ' ', $transaction->type)) }}
                        @endswitch
                    </div>
                    <div class="transaction-meta">
                        <span><i class="bi bi-calendar3 me-1"></i>{{ $transaction->created_at->format('d/m/Y H:i') }}</span>
                        @if($transaction->reference)
                        <span><i class="bi bi-hash me-1"></i>{{ $transaction->reference }}</span>
                        @endif
                    </div>
                </div>
                <div class="transaction-amount {{ $iconClass }}">
                    {{ $isCredit ? '+' : '' }}{{ number_format(abs($transaction->amount), 0, ',', ' ') }}
                    <span>XOF</span>
                </div>
                <span class="status-pill {{ $transaction->status }}">
                    @switch($transaction->status)
                        @case('completed') Complété @break
                        @case('pending') En attente @break
                        @case('failed') Échoué @break
                        @default {{ ucfirst($transaction->status) }}
                    @endswitch
                </span>
            </div>
            @endforeach
        </div>
        @if($transactions->hasPages())
        <div class="pagination-wrapper">{{ $transactions->links() }}</div>
        @endif
        @else
        <div class="empty-state">
            <div class="empty-icon"><i class="bi bi-receipt"></i></div>
            <h4 class="empty-title">Aucune transaction</h4>
            <p class="empty-text">Vos transactions apparaîtront ici</p>
        </div>
        @endif
    </div>
</div>
@endsection

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    const filterTabs = document.querySelectorAll('.filter-tab');
    const transactionItems = document.querySelectorAll('.transaction-item');
    
    filterTabs.forEach(tab => {
        tab.addEventListener('click', function() {
            filterTabs.forEach(t => t.classList.remove('active'));
            this.classList.add('active');
            
            const filter = this.dataset.filter;
            transactionItems.forEach(item => {
                if (filter === 'all' || item.dataset.type === filter) {
                    item.style.display = 'flex';
                } else {
                    item.style.display = 'none';
                }
            });
        });
    });
});
</script>
@endpush
