@extends('layouts.admin')
@section('title', __('Review payment'))

@section('content')
<a href="{{ route('admin.payments.index') }}" class="btn btn-sm mb-btn-ghost mb-3">← {{ __('Back to queue') }}</a>
<h1 class="h4 fw-bold mb-3">{{ __('Payment from :clinic', ['clinic' => $payment->tenant->name]) }}</h1>

<div class="row g-3">
    <div class="col-lg-6">
        <div class="mb-card p-4 mb-3">
            <table class="table mb-table mb-0">
                <tr><td class="mb-muted">{{ __('Clinic') }}</td><td>{{ $payment->tenant->name }} <span class="mb-muted small" dir="ltr">({{ $payment->tenant->slug }})</span></td></tr>
                <tr><td class="mb-muted">{{ __('Gateway') }}</td><td>{{ $payment->gateway->label() }}</td></tr>
                <tr><td class="mb-muted">{{ __('Amount') }}</td><td dir="ltr"><strong>{{ $payment->amountFormatted() }}</strong></td></tr>
                <tr><td class="mb-muted">{{ __('Period') }}</td><td>{{ $payment->period_months }} {{ __('month(s)') }}</td></tr>
                <tr><td class="mb-muted">{{ __('Reference') }}</td><td dir="ltr">{{ $payment->proof_reference }}</td></tr>
                <tr><td class="mb-muted">{{ __('Plan') }}</td><td>{{ $payment->plan->name }}</td></tr>
                <tr><td class="mb-muted">{{ __('Status') }}</td><td><span class="status-chip" style="background: {{ $payment->status->color() }}">{{ $payment->status->label() }}</span></td></tr>
            </table>
        </div>

        @if($payment->proof_path)
        <div class="mb-card p-3 mb-3">
            <h2 class="h6 fw-semibold mb-2">{{ __('Payment proof') }}</h2>
            @if(\Illuminate\Support\Str::endsWith($payment->proof_path, '.pdf'))
                <a class="btn mb-btn-ghost w-100" target="_blank" href="{{ route('admin.payments.proof', $payment) }}">📄 {{ __('Open PDF proof') }}</a>
            @else
                <a target="_blank" href="{{ route('admin.payments.proof', $payment) }}">
                    <img src="{{ route('admin.payments.proof', $payment) }}" alt="proof" class="img-fluid rounded">
                </a>
            @endif
        </div>
        @endif
    </div>

    <div class="col-lg-6">
        @if($payment->status === \App\Enums\PaymentProofStatus::Pending || $payment->status === \App\Enums\PaymentProofStatus::InfoRequested)
        <div class="mb-card p-4 mb-3" style="border-color: var(--mb-primary)">
            <h2 class="h6 fw-semibold mb-2">{{ __('Decision') }}</h2>
            <p class="mb-muted small">{{ __('Approving extends the clinic’s paid period and reactivates access immediately.') }}</p>
            <form method="POST" action="{{ route('admin.payments.review', $payment) }}">
                @csrf
                <textarea name="note" rows="2" class="form-control mb-2" placeholder="{{ __('Note (required to reject or request info)') }}">{{ old('note') }}</textarea>
                @error('note')<div class="text-danger small mb-2">{{ $message }}</div>@enderror
                <div class="d-flex gap-2 flex-wrap">
                    <button name="action" value="approve" class="btn mb-btn-primary">✓ {{ __('Approve') }}</button>
                    <button name="action" value="info" class="btn mb-btn-ghost">↩ {{ __('Request info') }}</button>
                    <button name="action" value="reject" class="btn btn-outline-danger">✕ {{ __('Reject') }}</button>
                </div>
            </form>
        </div>
        @endif

        {{-- Audit trail --}}
        <div class="mb-card p-4">
            <h2 class="h6 fw-semibold mb-3">🕘 {{ __('Audit trail') }}</h2>
            <div class="tl">
                @forelse($payment->reviews as $r)
                    <div class="tl-item">
                        <div class="d-flex justify-content-between">
                            <strong>{{ __(ucfirst(str_replace('_', ' ', $r->action))) }}</strong>
                            <span class="mb-muted small">{{ $r->created_at->format('d M Y · H:i') }}</span>
                        </div>
                        <div class="mb-muted small">
                            {{ $r->reviewer?->name ?? __('Clinic') }}
                            @if($r->from_status) · {{ $r->from_status }} → {{ $r->to_status }} @endif
                            @if($r->ip) · <span dir="ltr">{{ $r->ip }}</span> @endif
                        </div>
                        @if($r->note)<div class="small">{{ $r->note }}</div>@endif
                    </div>
                @empty
                    <p class="mb-muted small mb-0">{{ __('No audit entries.') }}</p>
                @endforelse
            </div>
        </div>
    </div>
</div>
@endsection
