@extends('layouts.admin')
@section('title', 'Transactions')
@section('content')
<div class="page-header" style="display:flex;justify-content:space-between;align-items:flex-start;flex-wrap:wrap;gap:1rem">
    <div>
        <h1>Transactions</h1>
        <p>All transactions in the system ({{ number_format($totalCount) }} total)</p>
    </div>
    <div class="action-group">
        <a href="{{ route('admin.transactions.export.excel', request()->only(['search', 'type'])) }}"
           class="md-btn md-btn-tonal">Download Excel</a>
        <a href="{{ route('admin.transactions.export.pdf', request()->only(['search', 'type'])) }}"
           class="md-btn md-btn-filled">Download PDF</a>
    </div>
</div>

<form class="toolbar" method="GET">
    <input type="search" name="search" class="md-input" placeholder="Search user..." value="{{ request('search') }}">
    <select name="type" class="md-select">
        <option value="all">All Types</option>
        <option value="deposit" @selected(request('type') === 'deposit')>Deposit</option>
        <option value="withdrawal" @selected(request('type') === 'withdrawal')>Withdrawal</option>
        <option value="transfer" @selected(request('type') === 'transfer')>Transfer</option>
        <option value="balance_add" @selected(request('type') === 'balance_add')>Balance Add</option>
        <option value="balance_subtract" @selected(request('type') === 'balance_subtract')>Balance Subtract</option>
    </select>
    <button type="submit" class="md-btn md-btn-filled">Filter</button>
    @if(request()->hasAny(['search', 'type']) && request('type') !== 'all')
        <a href="{{ route('admin.transactions.index') }}" class="md-btn md-btn-tonal">Clear</a>
    @endif
</form>

<div class="md-card">
    <div class="table-wrap">
        <table class="md-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>User</th>
                    <th>Type</th>
                    <th>Details</th>
                    <th>Amount</th>
                    <th>Status</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                @forelse($transactions as $tx)
                <tr>
                    <td>{{ $tx->id }}</td>
                    <td>
                        {{ $tx->user?->name ?? '—' }}
                        @if($tx->user?->email)
                            <br><small style="color:var(--md-on-surface-variant)">{{ $tx->user->email }}</small>
                        @endif
                    </td>
                    <td>{{ str_replace('_', ' ', ucfirst($tx->type)) }}</td>
                    <td style="max-width:280px">{{ $tx->details ?? '—' }}</td>
                    <td>${{ number_format($tx->amount, 2) }}</td>
                    <td><span class="badge {{ $tx->status->badgeClass() }}">{{ $tx->status->label() }}</span></td>
                    <td>{{ $tx->created_at->format('Y-m-d H:i:s') }}</td>
                </tr>
                @empty
                <tr><td colspan="7" style="text-align:center;padding:2rem">No transactions found</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($transactions->hasPages())
    <div style="padding:1rem">{{ $transactions->links() }}</div>
    @endif
</div>
@endsection
