<x-app-layout>
    <x-slot name="header">{{ __('Vendor Ledger') }}: {{ $vendor->name }}</x-slot>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="row g-4">
        {{-- Add Entry --}}
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header">
                    <h6 class="mb-0"><i class="bi bi-plus-circle me-1"></i>{{ __('Add Ledger Entry') }}</h6>
                </div>
                <div class="card-body">
                    <form action="{{ route('vendors.ledger.store', $vendor) }}" method="POST">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">{{ __('Date') }}</label>
                            <input type="date" name="transaction_date" class="form-control"
                                   value="{{ old('transaction_date', now()->format('Y-m-d')) }}" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">{{ __('Type') }}</label>
                            <select name="type" class="form-select" required>
                                <option value="debit" {{ old('type') == 'debit' ? 'selected' : '' }}>{{ __('Debit (Purchase / Due)') }}</option>
                                <option value="payment" {{ old('type') == 'payment' ? 'selected' : '' }}>{{ __('Payment (We Paid)') }}</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">{{ __('Amount') }}</label>
                            <input type="number" name="amount" step="0.01" min="0.01" class="form-control"
                                   value="{{ old('amount') }}" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">{{ __('Description') }}</label>
                            <input type="text" name="description" class="form-control" value="{{ old('description') }}">
                        </div>
                        <button type="submit" class="btn btn-primary w-100">{{ __('Save Entry') }}</button>
                    </form>
                </div>
            </div>
            {{-- Balance Card --}}
            <div class="card mt-3 {{ $vendor->current_due > 0 ? 'border-danger' : 'border-success' }}">
                <div class="card-body text-center">
                    <h6 class="text-muted">{{ __('Current Due Balance') }}</h6>
                    <h3 class="{{ $vendor->current_due > 0 ? 'text-danger' : 'text-success' }}">
                        {{ number_format($vendor->current_due, 2) }}
                    </h3>
                    <a href="{{ route('vendors.index') }}" class="btn btn-outline-secondary btn-sm mt-2">
                        <i class="bi bi-arrow-left me-1"></i>{{ __('Back to Vendors') }}
                    </a>
                </div>
            </div>
        </div>

        {{-- Ledger Table --}}
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header">
                    <h6 class="mb-0"><i class="bi bi-journal-text me-1"></i>{{ __('Transaction History') }}</h6>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>{{ __('Date') }}</th>
                                    <th>{{ __('Type') }}</th>
                                    <th class="text-end">{{ __('Amount') }}</th>
                                    <th class="text-end">{{ __('Balance') }}</th>
                                    <th>{{ __('Description') }}</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($ledgers as $ledger)
                                    <tr>
                                        <td>{{ $ledger->transaction_date->format('d M Y') }}</td>
                                        <td>
                                            @if($ledger->type === 'debit')
                                                <span class="badge bg-danger">{{ __('Debit') }}</span>
                                            @else
                                                <span class="badge bg-success">{{ __('Payment') }}</span>
                                            @endif
                                        </td>
                                        <td class="text-end">{{ number_format($ledger->amount, 2) }}</td>
                                        <td class="text-end {{ $ledger->balance > 0 ? 'text-danger' : 'text-success' }}">
                                            {{ number_format($ledger->balance, 2) }}
                                        </td>
                                        <td>{{ $ledger->description ?? '-' }}</td>
                                        <td>
                                            <form method="POST" action="{{ route('vendors.ledger.destroy', [$vendor, $ledger]) }}"
                                                  onsubmit="return confirm('{{ __('Delete this entry?') }}')">
                                                @csrf @method('DELETE')
                                                <button class="btn btn-outline-danger btn-sm"><i class="bi bi-trash"></i></button>
                                            </form>
                                        </td>
                                    </tr>
                                @empty
                                    <tr><td colspan="6" class="text-center text-muted py-4">{{ __('No transactions yet.') }}</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
                @if($ledgers->hasPages())
                    <div class="card-footer">{{ $ledgers->links() }}</div>
                @endif
            </div>
        </div>
    </div>
</x-app-layout>
