<x-app-layout>
    <x-slot name="header">{{ __('app.customer_dues') }}</x-slot>

    <div class="mb-3 d-flex justify-content-between align-items-center">
        <span class="text-muted small">{{ $ledgers->total() }} {{ __('entries') }}</span>
        <a href="{{ route('customer-ledgers.create') }}" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-circle me-1"></i>{{ __('Add Ledger Entry') }}
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-dark">
                        <tr>
                            <th>{{ __('#') }}</th>
                            <th>{{ __('Customer') }}</th>
                            <th>{{ __('Date') }}</th>
                            <th>{{ __('Type') }}</th>
                            <th class="text-end">{{ __('Amount') }}</th>
                            <th class="text-end">{{ __('Balance') }}</th>
                            <th>{{ __('Description') }}</th>
                            <th class="text-end">{{ __('Actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($ledgers as $ledger)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td><strong>{{ $ledger->customer->name ?? '-' }}</strong></td>
                                <td>{{ $ledger->transaction_date ?? $ledger->entry_date ?? '-' }}</td>
                                <td>
                                    @php $typeColor = match($ledger->type ?? $ledger->entry_type ?? '') { 'credit' => 'danger', 'payment' => 'success', default => 'secondary' }; @endphp
                                    <span class="badge bg-{{ $typeColor }} text-capitalize">{{ $ledger->type ?? $ledger->entry_type ?? '-' }}</span>
                                </td>
                                <td class="text-end">{{ number_format($ledger->amount, 2) }}</td>
                                <td class="text-end fw-bold">{{ number_format($ledger->balance, 2) }}</td>
                                <td class="text-muted small">{{ $ledger->description ?? $ledger->notes ?? '-' }}</td>
                                <td class="text-end">
                                    <form method="POST" action="{{ route('customer-ledgers.destroy', $ledger) }}" class="d-inline" onsubmit="return confirm('{{ __('Delete this entry?') }}')">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="8" class="text-center text-muted py-4">{{ __('No ledger entries found.') }}</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="mt-3">{{ $ledgers->links() }}</div>
</x-app-layout>

