@extends('layouts.admin')

@section('title', 'المعاملات')
@section('heading', 'معاملات المحفظة')
@section('subheading', 'عرض وفلترة وبحث في السجل الداخلي')

@section('content')
    <form method="get" action="{{ route('admin.transactions.index') }}" class="mb-6 grid gap-4 rounded-2xl border border-zinc-200 bg-white p-4 shadow-sm sm:grid-cols-2 lg:grid-cols-5">
        <div class="lg:col-span-2">
            <label for="q" class="mb-1 block text-sm font-medium text-zinc-700">بحث</label>
            <input id="q" name="q" type="search" value="{{ $q }}"
                   placeholder="اسم المستخدم، الاسم، أو المرجع…"
                   class="w-full rounded-xl border border-zinc-300 bg-zinc-50 px-4 py-2.5 text-sm focus:border-teal-500 focus:outline-none focus:ring-2 focus:ring-teal-500/20">
        </div>
        <div>
            <label for="type" class="mb-1 block text-sm font-medium text-zinc-700">النوع</label>
            <select id="type" name="type" class="w-full rounded-xl border border-zinc-300 bg-white px-4 py-2.5 text-sm">
                <option value="">الكل</option>
                @foreach (['deposit' => 'إيداع', 'withdraw' => 'سحب', 'reward' => 'مكافأة', 'payment' => 'دفع', 'refund' => 'استرجاع'] as $val => $label)
                    <option value="{{ $val }}" @selected($type === $val)>{{ $label }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label for="status" class="mb-1 block text-sm font-medium text-zinc-700">الحالة</label>
            <select id="status" name="status" class="w-full rounded-xl border border-zinc-300 bg-white px-4 py-2.5 text-sm">
                <option value="">الكل</option>
                @foreach (['pending' => 'معلق', 'completed' => 'مكتمل', 'failed' => 'فشل', 'cancelled' => 'ملغى'] as $val => $label)
                    <option value="{{ $val }}" @selected($status === $val)>{{ $label }}</option>
                @endforeach
            </select>
        </div>
        <div class="flex items-end gap-2">
            <button type="submit" class="w-full rounded-xl bg-madar-950 px-4 py-2.5 text-sm font-bold text-white hover:bg-madar-900">تطبيق</button>
            <a href="{{ route('admin.transactions.index') }}" class="rounded-xl border border-zinc-300 px-4 py-2.5 text-sm font-medium text-zinc-700 hover:bg-zinc-50">مسح</a>
        </div>
    </form>

    <div class="overflow-hidden rounded-2xl border border-zinc-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-zinc-200 text-sm">
                <thead class="bg-zinc-50 text-right text-xs font-semibold uppercase tracking-wide text-zinc-500">
                    <tr>
                        <th class="px-4 py-3">#</th>
                        <th class="px-4 py-3">المستخدم</th>
                        <th class="px-4 py-3">النوع</th>
                        <th class="px-4 py-3">المبلغ</th>
                        <th class="px-4 py-3">الحالة</th>
                        <th class="px-4 py-3">الوصف</th>
                        <th class="px-4 py-3">المرجع</th>
                        <th class="px-4 py-3">التاريخ</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-zinc-100">
                    @forelse ($transactions as $tx)
                        <tr class="hover:bg-zinc-50/80">
                            <td class="whitespace-nowrap px-4 py-3 text-zinc-500">{{ $tx->id }}</td>
                            <td class="px-4 py-3">
                                @if ($tx->user)
                                    <a href="{{ route('admin.users.show', $tx->user) }}" class="font-medium text-teal-700 hover:underline">{{ $tx->user->username }}</a>
                                @else
                                    —
                                @endif
                            </td>
                            <td class="px-4 py-3 text-zinc-700">{{ $tx->type }}</td>
                            <td class="whitespace-nowrap px-4 py-3 font-mono {{ (float) $tx->amount < 0 ? 'text-red-600' : 'text-emerald-700' }}">{{ number_format((float) $tx->amount, 2) }}</td>
                            <td class="px-4 py-3">
                                <span class="inline-flex rounded-full bg-zinc-100 px-2 py-0.5 text-xs font-medium text-zinc-700">{{ $tx->status }}</span>
                            </td>
                            <td class="max-w-[12rem] truncate px-4 py-3 text-zinc-600" title="{{ $tx->description }}">{{ $tx->description ?? '—' }}</td>
                            <td class="max-w-[8rem] truncate px-4 py-3 font-mono text-xs text-zinc-500">{{ $tx->reference ?? '—' }}</td>
                            <td class="whitespace-nowrap px-4 py-3 text-zinc-500">{{ $tx->created_at?->format('Y-m-d H:i') }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="px-4 py-12 text-center text-zinc-500">لا توجد معاملات.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="border-t border-zinc-100 px-4 py-3">
            {{ $transactions->links() }}
        </div>
    </div>
@endsection
