@extends('layouts.app')

@section('title', 'Payments & Receipts')

@section('content')
<main class="nxl-container">
    <div class="nxl-content">
        <div class="page-header"><h4>Payments & Receipts</h4></div>
        <div class="main-content">
            @if ($errors->any())
                <div class="alert alert-danger"><ul class="mb-0">@foreach ($errors->all() as $error)<li>{{ $error }}</li>@endforeach</ul></div>
            @endif

            <div class="card mb-3">
                <div class="card-body">
                    <form method="POST" enctype="multipart/form-data" action="{{ route('payments.store') }}" class="row g-2">
                        @csrf
                        <div class="col-md-3"><input class="form-control" name="payer" placeholder="Payer" value="{{ old('payer') }}" required></div>
                        <div class="col-md-2"><input class="form-control" type="number" step=".01" min="0.01" name="amount" placeholder="Amount" value="{{ old('amount') }}" required></div>
                        <div class="col-md-2"><input class="form-control" name="method" placeholder="Method" value="{{ old('method') }}" required></div>
                        <div class="col-md-2"><input class="form-control" name="reference" placeholder="Reference" value="{{ old('reference') }}" required></div>
                        <div class="col-md-3"><input class="form-control" type="date" name="received_at" value="{{ old('received_at') }}" required></div>
                        <div class="col-12"><input class="form-control" type="file" name="attachment"></div>
                        <div class="col-12">
                            <label class="form-label">Invoice allocations <small class="text-muted">Only open or partially paid invoices in this business can be selected.</small></label>
                            @forelse ($invoices as $invoice)
                                @php($balance = $invoice->details['balance_due'] ?? $invoice->details['total_amount'] ?? 0)
                                <div class="row g-2 mb-1 align-items-center">
                                    <div class="col-md-6">{{ $invoice->reference_number }} · Outstanding balance {{ number_format((float) $balance, 2) }}</div>
                                    <div class="col-md-3">
                                        <input type="hidden" name="allocations[{{ $loop->index }}][invoice_id]" value="{{ $invoice->id }}">
                                        <input class="form-control" type="number" step=".01" min="0.01" max="{{ $balance }}" name="allocations[{{ $loop->index }}][amount]" placeholder="Allocate" value="{{ old('allocations.'.$loop->index.'.amount') }}">
                                    </div>
                                </div>
                            @empty
                                <p class="text-muted mb-0">There are no eligible invoices available for allocation.</p>
                            @endforelse
                        </div>
                        <div class="col-12"><button class="btn btn-primary">Record payment</button></div>
                    </form>
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                    <table class="table">
                        <tr><th>Reference</th><th>Payer</th><th>Amount</th><th>Allocated</th><th>Unallocated</th><th>Received</th><th>Status</th><th>Receipt</th></tr>
                        @foreach ($payments as $payment)
                            @php($unallocated = max(0, (float) $payment->amount - (float) $payment->allocated_amount))
                            <tr id="payment-{{ $payment->id }}">
                                <td>{{ $payment->reference }}</td><td>{{ $payment->payer }}</td><td>{{ number_format((float) $payment->amount, 2) }}</td>
                                <td>{{ number_format((float) $payment->allocated_amount, 2) }}</td><td>{{ number_format($unallocated, 2) }}</td>
                                <td>{{ $payment->received_at }}</td><td>{{ $payment->status }}</td>
                                <td>
                                    @if ($payment->status !== 'verified')
                                        <form method="POST" action="{{ route('payments.verify', $payment->id) }}">
                                            @csrf
                                            <button class="btn btn-sm btn-success">Verify &amp; receipt</button>
                                        </form>
                                    @else
                                        Verified
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </table>
                </div>
            </div>
        </div>
    </div>
</main>
@endsection
