@extends('layouts.vendor')

@section('title', 'Earnings')
@section('page-title', 'Earnings & Transactions')

@section('content')
<div class="max-w-6xl">
    {{-- Stats Cards --}}
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
        <div class="bg-white rounded-xl shadow-sm p-6">
            <p class="text-sm text-gray-500">Total Earnings</p>
            <p class="text-2xl font-bold text-gray-800">{{ format_currency($stats['total_earnings']) }}</p>
            <p class="text-xs text-gray-400 mt-1">Lifetime earnings</p>
        </div>
        <div class="bg-white rounded-xl shadow-sm p-6">
            <p class="text-sm text-gray-500">Available Balance</p>
            <p class="text-2xl font-bold text-green-600">{{ format_currency($stats['balance']) }}</p>
            <p class="text-xs text-gray-400 mt-1">Ready for withdrawal</p>
        </div>
        <div class="bg-white rounded-xl shadow-sm p-6">
            <p class="text-sm text-gray-500">This Month</p>
            <p class="text-2xl font-bold text-blue-600">{{ format_currency($stats['this_month']) }}</p>
            <p class="text-xs text-gray-400 mt-1">{{ now()->format('F Y') }}</p>
        </div>
    </div>

    {{-- Transactions Table --}}
    <div class="bg-white rounded-xl shadow-sm overflow-hidden">
        <div class="p-6 border-b border-gray-200">
            <h2 class="font-bold text-gray-800">Transaction History</h2>
        </div>
        
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead class="bg-gray-50">
                    <tr class="text-gray-500">
                        <th class="text-left py-3 px-6">Date</th>
                        <th class="text-left py-3 px-6">Type</th>
                        <th class="text-left py-3 px-6">Description</th>
                        <th class="text-left py-3 px-6">Amount</th>
                        <th class="text-left py-3 px-6">Balance After</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($transactions as $transaction)
                    <tr class="border-b last:border-0 hover:bg-gray-50">
                        <td class="py-3 px-6 text-gray-600">
                            {{ $transaction->created_at->format('M d, Y') }}
                            <br>
                            <span class="text-xs text-gray-400">{{ $transaction->created_at->format('h:i A') }}</span>
                        </td>
                        <td class="py-3 px-6">
                            <span class="px-2 py-1 rounded-full text-xs font-medium
                                {{ $transaction->type === 'credit' ? 'bg-green-100 text-green-700' : 
                                   ($transaction->type === 'debit' ? 'bg-red-100 text-red-700' : 'bg-gray-100 text-gray-700') }}">
                                {{ ucfirst($transaction->type) }}
                            </span>
                        </td>
                        <td class="py-3 px-6 text-gray-600">
                            {{ $transaction->description }}
                            @if($transaction->reference)
                                <br>
                                <span class="text-xs text-gray-400">Ref: {{ $transaction->reference }}</span>
                            @endif
                        </td>
                        <td class="py-3 px-6">
                            <span class="font-medium {{ $transaction->type === 'credit' ? 'text-green-600' : 'text-red-600' }}">
                                {{ $transaction->type === 'credit' ? '+' : '-' }}{{ format_currency($transaction->amount) }}
                            </span>
                        </td>
                        <td class="py-3 px-6 text-gray-600">
                            {{ format_currency($transaction->balance_after) }}
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="py-8 px-6 text-center text-gray-500">
                            No transactions found.
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        @if($transactions->hasPages())
        <div class="p-4 border-t border-gray-200">
            {{ $transactions->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
