@extends('admin.layouts.app')
@section('title', 'Customer: '.$customer->name)
@section('content')
<div class="page-header d-print-none">
    <div class="container-xl">
        <div class="row g-2 align-items-center">
            <div class="col-auto">
                <a href="{{ route('admin.customers.index') }}" class="btn btn-outline-secondary btn-sm">← Back</a>
            </div>
            <div class="col"><h2 class="page-title">{{ $customer->name }}</h2></div>
            <div class="col-auto d-flex gap-2">
                @if($customer->status === 'active')
                    <form action="{{ route('admin.customers.block', $customer) }}" method="POST">
                        @csrf
                        <button class="btn btn-danger btn-sm" onclick="return confirm('Block this customer?')">Block Customer</button>
                    </form>
                @else
                    <form action="{{ route('admin.customers.unblock', $customer) }}" method="POST">
                        @csrf
                        <button class="btn btn-success btn-sm">Unblock Customer</button>
                    </form>
                @endif
            </div>
        </div>
    </div>
</div>
<div class="page-body">
    <div class="row g-4">
        <!-- Profile -->
        <div class="col-md-4">
            <div class="card">
                <div class="card-body text-center">
                    <span class="avatar avatar-xl mb-3" style="font-size:2rem">{{ strtoupper(substr($customer->name,0,2)) }}</span>
                    <h3 class="mb-1">{{ $customer->name }}</h3>
                    <p class="text-muted mb-2">{{ $customer->email }}</p>
                    <p class="text-muted mb-3">{{ $customer->phone ?? 'No phone' }}</p>
                    @php $sc=['active'=>'success','blocked'=>'danger','pending'=>'warning']; @endphp
                    <span class="badge bg-{{ $sc[$customer->status]??'secondary' }}-lt fs-6 text-capitalize">{{ $customer->status }}</span>
                </div>
                <div class="card-footer">
                    <div class="row text-center">
                        <div class="col">
                            <div class="fw-bold">{{ $customer->orders_count }}</div>
                            <div class="text-muted small">Orders</div>
                        </div>
                        <div class="col">
                            <div class="fw-bold">₹{{ number_format($customer->wallet?->balance ?? 0, 2) }}</div>
                            <div class="text-muted small">Balance</div>
                        </div>
                        <div class="col">
                            <div class="fw-bold">{{ $customer->created_at->format('d M Y') }}</div>
                            <div class="text-muted small">Joined</div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Wallet Card -->
            <div class="card mt-3">
                <div class="card-header"><h4 class="card-title">Wallet</h4></div>
                <div class="card-body">
                    <div class="d-flex justify-content-between mb-2">
                        <span class="text-muted">Balance</span>
                        <strong>₹{{ number_format($customer->wallet?->balance ?? 0, 2) }}</strong>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span class="text-muted">Points</span>
                        <strong>{{ number_format($customer->wallet?->points_balance ?? 0) }}</strong>
                    </div>
                    <div class="d-flex justify-content-between">
                        <span class="text-muted">Pending Withdrawal</span>
                        <strong>₹{{ number_format($customer->wallet?->pending_withdrawal ?? 0, 2) }}</strong>
                    </div>
                    @if($customer->wallet?->is_frozen)
                        <div class="alert alert-danger mt-3 mb-0 py-2">Wallet is frozen</div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Orders -->
        <div class="col-md-8">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Recent Orders</h4></div>
                <div class="table-responsive">
                    <table class="table table-vcenter card-table">
                        <thead>
                            <tr>
                                <th>Order #</th>
                                <th>Vendor</th>
                                <th>Type</th>
                                <th>Total</th>
                                <th>Status</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($orders as $order)
                            <tr>
                                <td><a href="{{ route('admin.orders.show', $order) }}">{{ $order->order_number }}</a></td>
                                <td>{{ $order->vendor?->business_name ?? '—' }}</td>
                                <td><span class="badge bg-blue-lt text-capitalize">{{ $order->type }}</span></td>
                                <td>₹{{ number_format($order->total, 2) }}</td>
                                <td>
                                    @php $colors=['pending'=>'warning','completed'=>'success','cancelled'=>'danger','disputed'=>'red','in_progress'=>'primary','accepted'=>'info']; @endphp
                                    <span class="badge bg-{{ $colors[$order->status]??'secondary' }}-lt text-capitalize">{{ str_replace('_',' ',$order->status) }}</span>
                                </td>
                                <td>{{ $order->created_at->format('d M Y') }}</td>
                            </tr>
                            @empty
                            <tr><td colspan="6" class="text-center text-muted">No orders yet</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                @if($orders->hasPages())
                <div class="card-footer">{{ $orders->links() }}</div>
                @endif
            </div>

            <!-- Recent Transactions -->
            @if($customer->wallet)
            <div class="card mt-3">
                <div class="card-header"><h4 class="card-title">Recent Wallet Transactions</h4></div>
                <div class="table-responsive">
                    <table class="table table-vcenter card-table">
                        <thead>
                            <tr><th>Reference</th><th>Type</th><th>Amount</th><th>Status</th><th>Date</th></tr>
                        </thead>
                        <tbody>
                            @forelse($customer->wallet->transactions as $txn)
                            <tr>
                                <td class="text-muted small">{{ $txn->reference }}</td>
                                <td><span class="badge bg-{{ in_array($txn->type,['credit','bonus']) ? 'success' : 'danger' }}-lt text-capitalize">{{ $txn->type }}</span></td>
                                <td>₹{{ number_format($txn->amount, 2) }}</td>
                                <td><span class="badge bg-{{ $txn->status==='completed'?'success':'warning' }}-lt text-capitalize">{{ $txn->status }}</span></td>
                                <td>{{ $txn->created_at->format('d M Y') }}</td>
                            </tr>
                            @empty
                            <tr><td colspan="5" class="text-center text-muted">No transactions</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
