@extends('admin.layouts.app')
@section('title', 'Wallet & Transactions')
@section('content')
<div class="page-header d-print-none">
    <div class="container-xl">
        <div class="row g-2 align-items-center">
            <div class="col"><h2 class="page-title">Wallet Management</h2></div>
            <div class="col-auto d-flex gap-2">
                <button class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#creditModal">+ Credit Wallet</button>
                <button class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#debitModal">- Debit Wallet</button>
            </div>
        </div>
    </div>
</div>
<div class="page-body">
    <!-- Stats -->
    <div class="row row-deck row-cards mb-4">
        <div class="col-sm-6 col-lg-4">
            <div class="card">
                <div class="card-body">
                    <div class="subheader">Total Wallet Volume</div>
                    <div class="h2 mb-0">₹{{ number_format($totalVolume, 2) }}</div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-4">
            <div class="card border-warning">
                <div class="card-body">
                    <div class="subheader text-warning">Pending Withdrawals</div>
                    <div class="h2 mb-0">{{ $pendingWithdrawals }}</div>
                    <a href="{{ route('admin.wallet.withdrawals') }}" class="btn btn-sm btn-warning mt-2">Process</a>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-3">
                    <select name="type" class="form-select">
                        <option value="">All Types</option>
                        @foreach(['credit','debit','withdrawal','refund','bonus','commission'] as $t)
                            <option value="{{ $t }}" {{ request('type')==$t?'selected':'' }}>{{ ucfirst($t) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        @foreach(['pending','completed','failed','cancelled'] as $s)
                            <option value="{{ $s }}" {{ request('status')==$s?'selected':'' }}>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="{{ route('admin.wallet.index') }}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h3 class="card-title">Transactions ({{ $transactions->total() }})</h3></div>
        <div class="table-responsive">
            <table class="table table-vcenter card-table">
                <thead>
                    <tr><th>Reference</th><th>Owner</th><th>Type</th><th>Amount</th><th>Balance After</th><th>Status</th><th>Date</th></tr>
                </thead>
                <tbody>
                    @forelse($transactions as $txn)
                    <tr>
                        <td class="text-muted small">{{ $txn->reference }}</td>
                        <td>
                            @if($txn->wallet?->owner)
                                <div class="fw-medium">{{ $txn->wallet->owner?->name ?? $txn->wallet->owner?->business_name ?? '—' }}</div>
                                <div class="text-muted small text-capitalize">{{ class_basename($txn->wallet->owner_type) }}</div>
                            @else —
                            @endif
                        </td>
                        <td>
                            @php $tc=['credit'=>'success','bonus'=>'success','debit'=>'danger','withdrawal'=>'warning','refund'=>'info','commission'=>'secondary']; @endphp
                            <span class="badge bg-{{ $tc[$txn->type]??'secondary' }}-lt text-capitalize">{{ $txn->type }}</span>
                        </td>
                        <td class="{{ in_array($txn->type,['credit','bonus','refund']) ? 'text-success' : 'text-danger' }}">
                            {{ in_array($txn->type,['credit','bonus','refund']) ? '+' : '-' }}₹{{ number_format($txn->amount, 2) }}
                        </td>
                        <td>₹{{ number_format($txn->balance_after, 2) }}</td>
                        <td>
                            @php $sc=['completed'=>'success','pending'=>'warning','failed'=>'danger','cancelled'=>'secondary']; @endphp
                            <span class="badge bg-{{ $sc[$txn->status]??'secondary' }}-lt text-capitalize">{{ $txn->status }}</span>
                        </td>
                        <td>{{ $txn->created_at->format('d M Y H:i') }}</td>
                    </tr>
                    @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No transactions found</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="card-footer d-flex align-items-center">{{ $transactions->links() }}</div>
    </div>
</div>

<!-- Credit Modal -->
<div class="modal modal-blur fade" id="creditModal" tabindex="-1"><div class="modal-dialog modal-sm modal-dialog-centered"><div class="modal-content">
    <form action="{{ route('admin.wallet.credit') }}" method="POST">@csrf
        <div class="modal-header"><h5 class="modal-title">Credit Wallet</h5></div>
        <div class="modal-body">
            <div class="mb-3">
                <label class="form-label">Owner Type</label>
                <select name="owner_type" class="form-select" required>
                    <option value="user">Customer</option>
                    <option value="vendor">Vendor</option>
                </select>
            </div>
            <div class="mb-3">
                <label class="form-label">Owner ID</label>
                <input type="number" name="owner_id" class="form-control" placeholder="User or Vendor ID" required>
            </div>
            <div class="mb-3">
                <label class="form-label">Amount (₹)</label>
                <input type="number" name="amount" class="form-control" placeholder="0.00" step="0.01" min="1" required>
            </div>
            <div class="mb-3">
                <label class="form-label">Description</label>
                <input type="text" name="description" class="form-control" placeholder="Reason for credit...">
            </div>
        </div>
        <div class="modal-footer"><button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-success">Credit</button></div>
    </form>
</div></div></div>

<!-- Debit Modal -->
<div class="modal modal-blur fade" id="debitModal" tabindex="-1"><div class="modal-dialog modal-sm modal-dialog-centered"><div class="modal-content">
    <form action="{{ route('admin.wallet.debit') }}" method="POST">@csrf
        <div class="modal-header"><h5 class="modal-title">Debit Wallet</h5></div>
        <div class="modal-body">
            <div class="mb-3">
                <label class="form-label">Owner Type</label>
                <select name="owner_type" class="form-select" required>
                    <option value="user">Customer</option>
                    <option value="vendor">Vendor</option>
                </select>
            </div>
            <div class="mb-3">
                <label class="form-label">Owner ID</label>
                <input type="number" name="owner_id" class="form-control" placeholder="User or Vendor ID" required>
            </div>
            <div class="mb-3">
                <label class="form-label">Amount (₹)</label>
                <input type="number" name="amount" class="form-control" placeholder="0.00" step="0.01" min="1" required>
            </div>
            <div class="mb-3">
                <label class="form-label">Description</label>
                <input type="text" name="description" class="form-control" placeholder="Reason for debit...">
            </div>
        </div>
        <div class="modal-footer"><button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-danger">Debit</button></div>
    </form>
</div></div></div>
@endsection
