@extends('admin.layouts.app')
@section('title', 'Customers')
@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">Customers</h2></div>
            <div class="col-auto ms-auto d-print-none">
                <a href="{{ route('admin.customers.create') }}" class="btn btn-primary">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5l0 14" /><path d="M5 12l14 0" /></svg>
                    Add Customer
                </a>
            </div>
        </div>
    </div>
</div>
<div class="page-body">
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-5">
                    <input type="text" name="search" class="form-control" placeholder="Search name, email, phone..." value="{{ request('search') }}">
                </div>
                <div class="col-md-3">
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        @foreach(['active','blocked','pending'] as $s)
                            <option value="{{ $s }}" {{ request('status')==$s?'selected':'' }}>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-4 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="{{ route('admin.customers.index') }}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h3 class="card-title">All Customers ({{ $customers->total() }})</h3>
        </div>
        <div class="table-responsive">
            <table class="table table-vcenter card-table">
                <thead>
                    <tr>
                        <th>Customer</th>
                        <th>Phone</th>
                        <th>Status</th>
                        <th>Orders</th>
                        <th>Wallet</th>
                        <th>Joined</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($customers as $customer)
                    <tr>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <span class="avatar avatar-sm">{{ strtoupper(substr($customer->name,0,2)) }}</span>
                                <div>
                                    <div class="fw-medium">{{ $customer->name }}</div>
                                    <div class="text-muted small">{{ $customer->email }}</div>
                                </div>
                            </div>
                        </td>
                        <td>{{ $customer->phone ?? '—' }}</td>
                        <td>
                            @php $sc=['active'=>'success','blocked'=>'danger','pending'=>'warning']; @endphp
                            <span class="badge bg-{{ $sc[$customer->status]??'secondary' }}-lt text-capitalize">{{ $customer->status }}</span>
                        </td>
                        <td>{{ $customer->orders_count }}</td>
                        <td>₹{{ number_format($customer->wallet?->balance ?? 0, 2) }}</td>
                        <td>{{ $customer->created_at->format('d M Y') }}</td>
                        <td>
                            <div class="d-flex gap-1">
                                <a href="{{ route('admin.customers.show', $customer) }}" class="btn btn-sm btn-outline-secondary">View</a>
                                @if($customer->status === 'active')
                                    <form action="{{ route('admin.customers.block', $customer) }}" method="POST" class="d-inline">
                                        @csrf
                                        <button class="btn btn-sm btn-danger" onclick="return confirm('Block this customer?')">Block</button>
                                    </form>
                                @else
                                    <form action="{{ route('admin.customers.unblock', $customer) }}" method="POST" class="d-inline">
                                        @csrf
                                        <button class="btn btn-sm btn-success">Unblock</button>
                                    </form>
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No customers found</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="card-footer d-flex align-items-center">
            {{ $customers->links() }}
        </div>
    </div>
</div>
@endsection
