@extends('layouts.app')

@section('title', 'Transaction Ledger')

@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">Transaction Ledger</h1>
            <p class="text-gray-600">Complete financial transaction history</p>
        </div>

        <!-- Summary -->
        <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 Credits</p>
                <p class="text-3xl font-bold text-green-600">R {{ number_format($totals['credits'], 2) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Total Debits</p>
                <p class="text-3xl font-bold text-red-600">R {{ number_format($totals['debits'], 2) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Net Balance</p>
                <p class="text-3xl font-bold {{ ($totals['credits'] - $totals['debits']) >= 0 ? 'text-green-600' : 'text-red-600' }}">
                    R {{ number_format($totals['credits'] - $totals['debits'], 2) }}
                </p>
            </div>
        </div>

        <!-- Filters -->
        <div class="bg-white rounded-xl shadow-sm p-6 mb-6">
            <form action="{{ route('admin.finance.ledger') }}" method="GET" class="flex flex-wrap gap-4">
                <div>
                    <select name="type" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">All Types</option>
                        <option value="income" {{ request('type') == 'income' ? 'selected' : '' }}>Income</option>
                        <option value="expense" {{ request('type') == 'expense' ? 'selected' : '' }}>Expense</option>
                        <option value="commission" {{ request('type') == 'commission' ? 'selected' : '' }}>Commission</option>
                        <option value="payout" {{ request('type') == 'payout' ? 'selected' : '' }}>Payout</option>
                        <option value="refund" {{ request('type') == 'refund' ? 'selected' : '' }}>Refund</option>
                    </select>
                </div>
                <div>
                    <select name="category" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">All Categories</option>
                        <option value="sales" {{ request('category') == 'sales' ? 'selected' : '' }}>Sales</option>
                        <option value="commission" {{ request('category') == 'commission' ? 'selected' : '' }}>Commission</option>
                        <option value="delivery" {{ request('category') == 'delivery' ? 'selected' : '' }}>Delivery</option>
                        <option value="refund" {{ request('category') == 'refund' ? 'selected' : '' }}>Refund</option>
                        <option value="supplier_payout" {{ request('category') == 'supplier_payout' ? 'selected' : '' }}>Supplier Payout</option>
                    </select>
                </div>
                <div>
                    <input type="date" name="date_from" value="{{ request('date_from') }}" 
                        class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
                        placeholder="From Date">
                </div>
                <div>
                    <input type="date" name="date_to" value="{{ request('date_to') }}" 
                        class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
                        placeholder="To Date">
                </div>
                <button type="submit" class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                    Filter
                </button>
                @if(request()->hasAny(['type', 'category', 'date_from', 'date_to']))
                    <a href="{{ route('admin.finance.ledger') }}" class="px-6 py-2 border border-gray-300 rounded-lg hover:bg-gray-50 transition">
                        Clear
                    </a>
                @endif
            </form>
        </div>

        <!-- Ledger Table -->
        <div class="bg-white rounded-xl shadow-sm overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Reference</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Date</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Type</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Category</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Description</th>
                            <th class="px-6 py-4 text-right text-sm font-semibold text-gray-900">Debit</th>
                            <th class="px-6 py-4 text-right text-sm font-semibold text-gray-900">Credit</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($entries as $entry)
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 font-medium">{{ $entry->reference_number }}</td>
                                <td class="px-6 py-4 text-gray-600">{{ $entry->created_at->format('d M Y H:i') }}</td>
                                <td class="px-6 py-4">
                                    <span class="capitalize">{{ $entry->type }}</span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="capitalize">{{ str_replace('_', ' ', $entry->category) }}</span>
                                </td>
                                <td class="px-6 py-4 text-gray-600 max-w-xs truncate">{{ $entry->description }}</td>
                                <td class="px-6 py-4 text-right font-medium text-red-600">
                                    {{ $entry->direction === 'debit' ? 'R ' . number_format($entry->amount, 2) : '-' }}
                                </td>
                                <td class="px-6 py-4 text-right font-medium text-green-600">
                                    {{ $entry->direction === 'credit' ? 'R ' . number_format($entry->amount, 2) : '-' }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="px-6 py-12 text-center text-gray-500">No transactions found</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="px-6 py-4 border-t">
                {{ $entries->links() }}
            </div>
        </div>
    </div>
</div>
@endsection
