@extends('layouts.app')

@section('title', 'Billing & Payouts')

@section('content')
<div class="bg-gray-50 min-h-screen py-8">
    <div class="container mx-auto px-4">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900">Billing & Payouts</h1>
            <p class="text-gray-600">Track your earnings and payouts</p>
        </div>

        <!-- Stats Cards -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Total Sales</p>
                <p class="text-3xl font-bold text-gray-900">R {{ number_format($stats['total_sales'], 2) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Commission Paid</p>
                <p class="text-3xl font-bold text-red-600">R {{ number_format($stats['total_commission'], 2) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Net Earnings</p>
                <p class="text-3xl font-bold text-green-600">R {{ number_format($stats['net_earnings'], 2) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Pending Payout</p>
                <p class="text-3xl font-bold text-blue-600">R {{ number_format($stats['pending_payout'], 2) }}</p>
            </div>
        </div>

        <div class="grid lg:grid-cols-3 gap-8">
            <!-- Recent Transactions -->
            <div class="lg:col-span-2">
                <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                    <div class="p-6 border-b">
                        <h2 class="font-semibold text-lg">Recent Transactions</h2>
                    </div>
                    @if($transactions->count() > 0)
                        <div class="overflow-x-auto">
                            <table class="w-full">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th class="px-6 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Date</th>
                                        <th class="px-6 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Type</th>
                                        <th class="px-6 py-3 text-right text-xs font-semibold text-gray-500 uppercase">Amount</th>
                                        <th class="px-6 py-3 text-right text-xs font-semibold text-gray-500 uppercase">Commission</th>
                                        <th class="px-6 py-3 text-right text-xs font-semibold text-gray-500 uppercase">Net</th>
                                        <th class="px-6 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Status</th>
                                    </tr>
                                </thead>
                                <tbody class="divide-y divide-gray-200">
                                    @foreach($transactions as $transaction)
                                        <tr class="hover:bg-gray-50">
                                            <td class="px-6 py-4 text-sm text-gray-600">{{ $transaction->created_at->format('d M Y') }}</td>
                                            <td class="px-6 py-4 text-sm">
                                                <span class="capitalize">{{ $transaction->type }}</span>
                                            </td>
                                            <td class="px-6 py-4 text-sm text-right font-medium">R {{ number_format($transaction->amount, 2) }}</td>
                                            <td class="px-6 py-4 text-sm text-right text-red-600">-R {{ number_format($transaction->commission_amount, 2) }}</td>
                                            <td class="px-6 py-4 text-sm text-right font-semibold text-green-600">R {{ number_format($transaction->net_amount, 2) }}</td>
                                            <td class="px-6 py-4 text-center">
                                                @php
                                                    $statusColors = [
                                                        'pending' => 'bg-yellow-100 text-yellow-800',
                                                        'processed' => 'bg-blue-100 text-blue-800',
                                                        'paid' => 'bg-green-100 text-green-800',
                                                    ];
                                                @endphp
                                                <span class="inline-flex px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$transaction->status] ?? 'bg-gray-100 text-gray-800' }}">
                                                    {{ ucfirst($transaction->status) }}
                                                </span>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <div class="px-6 py-4 border-t">
                            {{ $transactions->links() }}
                        </div>
                    @else
                        <div class="p-8 text-center text-gray-500">
                            <p>No transactions yet</p>
                        </div>
                    @endif
                </div>
            </div>

            <!-- Payout Info -->
            <div class="space-y-6">
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h2 class="font-semibold text-lg mb-4">Commission Info</h2>
                    <div class="space-y-3">
                        <div class="flex justify-between">
                            <span class="text-gray-600">Commission Rate</span>
                            <span class="font-medium">{{ $supplier->commission_rate }}%</span>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-gray-600">Registration Fee</span>
                            <span class="font-medium {{ $supplier->registration_fee_paid ? 'text-green-600' : 'text-red-600' }}">
                                {{ $supplier->registration_fee_paid ? 'Paid' : 'Pending' }}
                            </span>
                        </div>
                    </div>
                    <div class="mt-4 p-3 bg-blue-50 rounded-lg">
                        <p class="text-sm text-blue-800">
                            Commission is automatically deducted from each sale. Payouts are processed weekly.
                        </p>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h2 class="font-semibold text-lg mb-4">Recent Payouts</h2>
                    @if($payouts->count() > 0)
                        <div class="space-y-3">
                            @foreach($payouts as $payout)
                                <div class="flex justify-between items-center py-2 border-b last:border-0">
                                    <div>
                                        <p class="font-medium">{{ $payout->payout_number }}</p>
                                        <p class="text-sm text-gray-500">{{ $payout->created_at->format('d M Y') }}</p>
                                    </div>
                                    <div class="text-right">
                                        <p class="font-semibold text-green-600">R {{ number_format($payout->net_amount, 2) }}</p>
                                        <span class="text-xs {{ $payout->status === 'completed' ? 'text-green-600' : 'text-yellow-600' }}">
                                            {{ ucfirst($payout->status) }}
                                        </span>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @else
                        <p class="text-gray-500 text-sm">No payouts yet</p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
