@extends('backend.master')
@section('title', 'Withdraw Requests')

@push('styles')
    {{-- Bootstrap Icons --}}
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
@endpush

@section('content')

<style>
    :root{
        --wd-primary:#dc2626;
        --wd-primary-soft:#fee2e2;
        --wd-bg:#f5f5f5;
    }

    .wd-bg{
        background:var(--wd-bg);
    }

    .wd-card{
        border-radius:1rem;
        box-shadow:0 10px 28px rgba(15,23,42,.08);
        border:1px solid #e5e7eb;
        background:#fff;
    }

    .wd-section-title{
        font-size:.8rem;
        font-weight:600;
        text-transform:uppercase;
        letter-spacing:.08em;
        color:#6b7280;
        margin-bottom:.25rem;
    }

    .pill-title{
        display:inline-flex;
        align-items:center;
        gap:.4rem;
        padding:.25rem .8rem;
        border-radius:999px;
        background:var(--wd-primary-soft);
        color:var(--wd-primary);
        font-size:.78rem;
        font-weight:600;
    }

    .badge-status{
        border-radius:999px;
        padding:.25rem .6rem;
        font-size:.7rem;
        font-weight:600;
        display:inline-flex;
        align-items:center;
        gap:.25rem;
    }
    .badge-status .dot{
        width:.45rem;
        height:.45rem;
        border-radius:999px;
    }

    .badge-pending{
        background:#fffbeb;
        color:#b45309;
    }
    .badge-pending .dot{ background:#f59e0b; }

    .badge-approved{
        background:#ecfdf3;
        color:#15803d;
    }
    .badge-approved .dot{ background:#22c55e; }

    .badge-rejected{
        background:#fef2f2;
        color:#b91c1c;
    }
    .badge-rejected .dot{ background:#ef4444; }

    .badge-chip{
        border-radius:999px;
        padding:.15rem .6rem;
        font-size:.7rem;
        font-weight:600;
    }

    .badge-chip-bank{
        background:#e0f2fe;
        color:#0369a1;
    }
    .badge-chip-mobile{
        background:#ecfdf3;
        color:#15803d;
    }

    .table-wd th{
        font-size:.76rem;
        text-transform:uppercase;
        letter-spacing:.05em;
        color:#9ca3af;
        border-bottom:1px solid #e5e7eb!important;
        white-space:nowrap;
    }
    .table-wd td{
        font-size:.82rem;
        border-bottom:1px solid #f3f4f6!important;
        vertical-align:middle;
    }

    .btn-soft-primary{
        background:#eff6ff;
        color:#1d4ed8;
        border:1px solid #bfdbfe;
        border-radius:999px;
        font-size:.8rem;
        padding:.3rem .65rem;
    }
    .btn-soft-primary:hover{
        background:#1d4ed8;
        color:#fff;
        border-color:#1d4ed8;
    }

    .filter-chip{
        border-radius:999px;
        border:1px solid #e5e7eb;
        padding:.2rem .75rem;
        font-size:.75rem;
        display:inline-flex;
        align-items:center;
        gap:.3rem;
        background:#f9fafb;
    }

    @media (max-width: 768px){
        .wd-header-stack{
            flex-direction:column;
            align-items:flex-start!important;
            gap:.35rem;
        }
    }
</style>

<div class="wd-bg py-3 py-md-4">
    <div class="container-fluid">

        <div class="d-flex wd-header-stack justify-content-between align-items-center mb-3">
            <div>
                <h5 class="mb-1 fw-semibold d-flex align-items-center gap-2">
                    <i class="bi bi-cash-coin text-danger fs-5"></i>
                    <span>Withdraw Requests</span>
                </h5>
                <small class="text-muted">
                    Manage all user withdraw requests with status & TRX id.
                </small>
            </div>

            <span class="pill-title">
                <i class="bi bi-funnel"></i>
                <span>Current filter:</span>
                <span class="text-capitalize">
                    {{ $status ? $status : 'All' }}
                </span>
            </span>
        </div>

        {{-- Flash message --}}
        @if(session('success'))
            <div class="alert alert-success border-0 shadow-sm py-2 px-3 mb-3">
                <i class="bi bi-check-circle-fill me-1"></i>
                {{ session('success') }}
            </div>
        @endif

        <div class="card wd-card mb-3">
            <div class="card-body pb-2">

                {{-- Status filter --}}
                <form method="GET" class="row g-2 align-items-end mb-2">
                    <div class="col-12 col-md-auto">
                        <label class="form-label mb-1 small text-uppercase text-muted">Status</label>
                        <select name="status" class="form-select form-select-sm">
                            <option value="">All</option>
                            <option value="pending"  {{ $status==='pending'  ? 'selected' : '' }}>Pending</option>
                            <option value="approved" {{ $status==='approved' ? 'selected' : '' }}>Approved</option>
                            <option value="rejected" {{ $status==='rejected' ? 'selected' : '' }}>Rejected</option>
                        </select>
                    </div>
                    <div class="col-12 col-md-auto d-flex gap-2">
                        <button class="btn btn-sm btn-danger d-inline-flex align-items-center gap-1 mt-3 mt-md-4">
                            <i class="bi bi-funnel-fill"></i>
                            <span>Apply</span>
                        </button>
                        @if($status)
                            <a href="{{ route('admin.withdraw.index') }}"
                               class="btn btn-sm btn-outline-secondary mt-3 mt-md-4">
                                Clear
                            </a>
                        @endif
                    </div>
                </form>

                @if($status)
                    <div class="filter-chip mt-1">
                        <i class="bi bi-info-circle"></i>
                        <span>Showing only <strong class="text-capitalize">{{ $status }}</strong> requests</span>
                    </div>
                @endif
            </div>
        </div>

        <div class="card wd-card">
            <div class="card-body pb-2">
                <div class="wd-section-title mb-2">All requests</div>

                <div class="table-responsive">
                    <table class="table table-sm table-hover align-middle table-wd mb-0">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>User</th>
                            <th>Method</th>
                            <th>Account</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th>TRX ID</th>
                            <th>Requested at</th>
                            <th class="text-end">Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($withdraws as $w)
                            @php
                                $isBank = str_starts_with($w->method,'bank:');
                                if ($w->status === 'pending') {
                                    $statusClass = 'badge-pending';
                                    $statusLabel = 'Pending';
                                } elseif ($w->status === 'approved') {
                                    $statusClass = 'badge-approved';
                                    $statusLabel = 'Approved';
                                } else {
                                    $statusClass = 'badge-rejected';
                                    $statusLabel = 'Rejected';
                                }
                            @endphp
                            <tr>
                                <td class="fw-semibold">{{ $w->id }}</td>
                                <td>
                                    <div class="fw-semibold">{{ $w->user?->name ?? 'N/A' }}</div>
                                    <div class="text-muted small">
                                        <i class="bi bi-envelope me-1"></i>
                                        {{ $w->user?->email ?? '—' }}
                                    </div>
                                </td>
                                <td>
                                    <div class="mb-1">
                                        @if($isBank)
                                            <span class="badge-chip badge-chip-bank">
                                                <i class="bi bi-bank2 me-1"></i> Bank
                                            </span>
                                        @else
                                            <span class="badge-chip badge-chip-mobile">
                                                <i class="bi bi-phone-fill me-1"></i> Mobile
                                            </span>
                                        @endif
                                    </div>
                                    <small class="text-muted">{{ $w->method }}</small>
                                </td>
                                <td>
                                    <div class="fw-semibold small">{{ $w->account_number }}</div>
                                    @if($w->account_holder)
                                        <div class="text-muted small">
                                            <i class="bi bi-person-circle me-1"></i>
                                            Holder: {{ $w->account_holder }}
                                        </div>
                                    @endif
                                    @if($w->branch_name)
                                        <div class="text-muted small">
                                            <i class="bi bi-geo-alt me-1"></i>
                                            Branch: {{ $w->branch_name }}
                                        </div>
                                    @endif
                                </td>
                                <td class="fw-semibold">
                                    {{ number_format($w->amount) }} BDT
                                </td>
                                <td>
                                    <span class="badge-status {{ $statusClass }}">
                                        <span class="dot"></span>
                                        {{ $statusLabel }}
                                    </span>
                                </td>
                                <td class="small">
                                    {{ $w->trx_id ?: '—' }}
                                </td>
                                <td class="small text-muted">
                                    {{ $w->created_at->format('d M, Y H:i') }}
                                </td>
                                <td class="text-end">
                                    <a href="{{ route('admin.withdraw.edit', $w) }}"
                                       class="btn btn-soft-primary btn-sm d-inline-flex align-items-center gap-1">
                                        <i class="bi bi-pencil-square"></i>
                                        <span class="d-none d-xl-inline">Edit</span>
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="9" class="text-center text-muted py-3">
                                    <i class="bi bi-inbox me-1"></i>
                                    No withdraw requests found.
                                </td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>

                <div class="mt-3">
                    {{ $withdraws->withQueryString()->links() }}
                </div>
            </div>
        </div>

    </div>
</div>
@endsection
