@extends('layouts.admin')

@section('title', 'Transactions')

@section('content')
<div class="admin-top">
    <div>
        <h1>Transactions</h1>
        <p class="sub">Search, filter, export CSV, open details for refunds.</p>
    </div>
    <div style="display:flex; gap:0.5rem; flex-wrap:wrap;">
        <a href="{{ route('admin.transactions.analytics') }}" class="btn btn-ghost">Analytics</a>
        <a href="{{ route('admin.transactions.export', request()->query()) }}" class="btn btn-primary">Export CSV</a>
    </div>
</div>

<form method="GET" class="card" style="margin-bottom:1rem;">
    <div style="display:grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 1rem; align-items: end;">
        <div class="form-row" style="margin:0;">
            <label>Gateway</label>
            <select name="gateway">
                <option value="">All</option>
                @foreach(['stripe','paypal','square'] as $gw)
                    <option value="{{ $gw }}" @selected(request('gateway') === $gw)>{{ $gw }}</option>
                @endforeach
            </select>
        </div>
        <div class="form-row" style="margin:0;">
            <label>Status</label>
            <select name="status">
                <option value="">All</option>
                @foreach(['pending','processing','succeeded','failed','refunded'] as $st)
                    <option value="{{ $st }}" @selected(request('status') === $st)>{{ $st }}</option>
                @endforeach
            </select>
        </div>
        <div class="form-row" style="margin:0;">
            <label>Search</label>
            <input type="text" name="search" value="{{ request('search') }}" placeholder="Email, ID…">
        </div>
        <div>
            <button type="submit" class="btn btn-primary">Filter</button>
        </div>
    </div>
</form>

<div class="card">
    <table class="admin-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>User</th>
                <th>Gateway</th>
                <th>Amount</th>
                <th>Status</th>
                <th>Date</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @forelse($transactions as $t)
                <tr>
                    <td>#{{ $t->id }}</td>
                    <td>{{ $t->user?->email ?? '—' }}</td>
                    <td>{{ $t->gateway }}</td>
                    <td>{{ $t->currency }} {{ number_format($t->amount, 2) }}</td>
                    <td>
                        @if($t->status === 'succeeded')
                            <span class="pill-yes">{{ $t->status }}</span>
                        @elseif($t->status === 'failed')
                            <span style="color:var(--admin-danger); font-weight:600;">{{ $t->status }}</span>
                        @else
                            <span class="pill-no">{{ $t->status }}</span>
                        @endif
                    </td>
                    <td>{{ $t->created_at?->format('Y-m-d H:i') }}</td>
                    <td><a href="{{ route('admin.transactions.show', $t) }}" class="btn btn-ghost btn-sm">View</a></td>
                </tr>
            @empty
                <tr><td colspan="7">No transactions yet.</td></tr>
            @endforelse
        </tbody>
    </table>
    <div class="pagination">{{ $transactions->links() }}</div>
</div>
@endsection
