<x-app-layout>
    <div class="portal-container service-page-wrap">
        @if (session('status')) <div class="alert alert-success">{{ session('status') }}</div> @endif
        @if (session('error')) <div class="alert alert-error">{{ session('error') }}</div> @endif
        @if ($errors->any()) <div class="alert alert-error">{{ $errors->first() }}</div> @endif

        <section class="service-hero-card">
            <div class="service-hero-main">
                <span class="service-eyebrow">Administration</span>
                <h1>Manual Payment Reviews</h1>
                <p>Review client proofs before updating WHMCS.</p>
            </div>
        </section>

        <section class="service-panel">
            <div style="overflow-x:auto;">
                <table class="client-clean-table">
                    <thead><tr><th>Invoice / Client</th><th>Method</th><th>Proof</th><th>Status</th><th>Review</th></tr></thead>
                    <tbody>
                    @forelse ($submissions as $submission)
                        <tr>
                            <td>
                                <strong>#{{ $submission->invoice?->whmcs_invoice_id ?: $submission->invoice?->invoice_number }}</strong><br>
                                <small>{{ $submission->user?->displayName() }} — {{ $submission->user?->email }}</small><br>
                                <small>{{ strtoupper((string) $submission->invoice?->currency) }} {{ number_format((float) $submission->invoice?->total, 2) }}</small>
                            </td>
                            <td>{{ config('zt.manual_payments.bank_channels.'.$submission->payment_channel.'.name', 'Bank Transfer') }}</td>
                            <td><a class="secondary-btn" href="{{ route('manual-payments.proof', $submission) }}">Open Proof</a></td>
                            <td><span class="status-pill {{ $submission->status }}">{{ ucfirst($submission->status) }}</span><br><small>{{ $submission->created_at?->format('M j, Y g:i A') }}</small></td>
                            <td style="min-width:300px;">
                                @if ($submission->status === 'pending')
                                    <form method="POST" action="{{ route('admin.payments.approve', $submission) }}" style="display:grid; gap:8px; margin-bottom:10px;">
                                        @csrf
                                        <input class="input" name="admin_note" maxlength="1000" placeholder="Optional approval note">
                                        <button class="primary-btn" type="submit" onclick="return confirm('Approve this proof and mark the WHMCS invoice paid?')">Approve & Mark Paid</button>
                                    </form>
                                    <form method="POST" action="{{ route('admin.payments.reject', $submission) }}" style="display:grid; gap:8px;">
                                        @csrf
                                        <input class="input" name="admin_note" maxlength="1000" placeholder="Required rejection reason" required>
                                        <button class="secondary-btn" type="submit">Reject</button>
                                    </form>
                                @else
                                    <small>{{ $submission->admin_note ?: 'Reviewed' }}<br>By {{ $submission->reviewer?->displayName() ?: '-' }}</small>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="5">No payment proofs have been submitted.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            <div style="margin-top:20px;">{{ $submissions->links() }}</div>
        </section>
    </div>
</x-app-layout>
