@extends('coach.layouts.app')

@section('title', 'پرداخت‌ها')

@section('content')
    <h1 class="text-xl font-bold mb-6">پرداخت‌ها</h1>

    <div class="space-y-3">
        @forelse($payments as $payment)
            <div class="bg-white rounded-xl shadow-sm p-4">
                <div class="flex items-center justify-between">
                    <div>
                        <div class="font-medium">{{ $payment->coachRequest->athlete->user->fullName() }}</div>
                        <div class="text-xs text-gray-400">
                            مبلغ: {{ number_format($payment->amount) }} تومان ·
                            ارسال: {{ verta($payment->submitted_at)->format('Y/m/d H:i') }}
                        </div>
                    </div>
                    <span class="text-xs px-2 py-1 rounded-full
                        {{ $payment->status->value === 'approved' ? 'bg-green-50 text-green-700' :
                           ($payment->status->value === 'rejected' ? 'bg-red-50 text-red-600' : 'bg-amber-50 text-amber-600') }}">
                        {{ $payment->status->label() }}
                    </span>
                </div>

                <a href="{{ route('coach.payments.receipt', $payment) }}" target="_blank" class="text-blue-600 text-sm hover:underline mt-2 inline-block">
                    مشاهده رسید
                </a>

                @if($payment->status->value === 'pending_review')
                    <div class="flex items-center gap-2 mt-3">
                        <form method="POST" action="{{ route('coach.payments.approve', $payment) }}">
                            @csrf
                            <button class="bg-green-600 text-white text-sm px-4 py-2 rounded-lg">تأیید پرداخت</button>
                        </form>
                        <button type="button" onclick="document.getElementById('reject-{{ $payment->id }}').classList.toggle('hidden')"
                                class="bg-red-50 text-red-600 text-sm px-4 py-2 rounded-lg">رد رسید</button>
                    </div>
                    <form id="reject-{{ $payment->id }}" method="POST" action="{{ route('coach.payments.reject', $payment) }}" class="hidden mt-2">
                        @csrf
                        <textarea name="reason" rows="2" placeholder="دلیل رد رسید..." class="w-full border rounded-lg px-3 py-2 text-sm" required></textarea>
                        <button class="bg-red-600 text-white text-sm px-4 py-2 rounded-lg mt-2">ثبت رد</button>
                    </form>
                @elseif($payment->status->value === 'rejected')
                    <p class="text-xs text-red-500 mt-2">دلیل رد: {{ $payment->rejection_reason }}</p>
                @endif
            </div>
        @empty
            <p class="text-gray-400 text-center py-10">پرداختی ثبت نشده است.</p>
        @endforelse
    </div>
@endsection
