<x-admin-layout title="Customers">
    <div class="card">
        <div class="card-body">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="fw-bold mb-0">Customers</h5>
                @if (auth()->user()->hasPermission('customers.manage'))
                    <a href="{{ route('admin.customers.create') }}" class="btn btn-dark btn-sm">
                        <i class="bi bi-plus-lg me-1"></i>Add Customer
                    </a>
                @endif
            </div>

            @if ($customers->isEmpty())
                <div class="text-center text-muted py-5">
                    <i class="bi bi-people fs-1 d-block mb-2"></i>
                    No customers yet.
                </div>
            @else
                <div class="table-responsive">
                    <table class="table align-middle datatable">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($customers as $customer)
                                <tr>
                                    <td class="fw-semibold">
                                        {{ $customer->name }}
                                        @if ($customer->tag)
                                            @php
                                                $tagColor = ['vip' => 'is-amber', 'corporate' => 'is-blue', 'one_off' => 'is-violet'][$customer->tag] ?? 'is-blue';
                                            @endphp
                                            <span class="badge {{ $tagColor }}">{{ \App\Models\Customer::TAGS[$customer->tag] ?? $customer->tag }}</span>
                                        @endif
                                    </td>
                                    <td>{{ $customer->phone ?? '—' }}</td>
                                    <td>{{ $customer->email ?? '—' }}</td>
                                    <td>
                                        <span class="badge {{ $customer->status === 'active' ? 'is-green' : 'is-rose' }}">
                                            {{ ucfirst($customer->status) }}
                                        </span>
                                    </td>
                                    <td class="text-end">
                                        @if (auth()->user()->hasPermission('customers.manage'))
                                            <a href="{{ route('admin.customers.edit', $customer) }}" class="btn btn-sm btn-outline-secondary">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            <form method="POST" action="{{ route('admin.customers.destroy', $customer) }}" class="d-inline"
                                                  data-confirm-title="Delete customer?" data-confirm-text="Delete {{ $customer->name }}? This cannot be undone."
                                                  data-confirm-button="Delete">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button>
                                            </form>
                                        @else
                                            <span class="text-muted small">—</span>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </div>
    </div>
</x-admin-layout>
