{{-- Addresses Card - Reusable card showing addresses in 2-column grid --}}
{{-- Props: $entityType, $entityId, $addresses, $canEdit = true --}}
@php
    $canEdit = $canEdit ?? true;
    $addressTypes = [
        'billing' => __('Billing'),
        'shipping' => __('Shipping'),
        'office' => __('Office'),
        'warehouse' => __('Warehouse'),
        'home' => __('Home'),
        'other' => __('Other'),
    ];
@endphp

<div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">
            <i class="bx bx-map me-1"></i>{{ __('Addresses') }}
            <span class="badge bg-label-primary ms-2">{{ $addresses->count() }}</span>
        </h5>
        @if($canEdit)
        <button type="button" class="btn btn-sm btn-primary" onclick="openAddressOffcanvas()">
            <i class="bx bx-plus me-1"></i>{{ __('Add') }}
        </button>
        @endif
    </div>
    <div class="card-body">
        @if($addresses->count() > 0)
            <div class="row">
                @foreach($addresses as $address)
                <div class="col-md-6 mb-3">
                    <div class="border rounded p-3 h-100 position-relative">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <div>
                                @if($address->is_primary)
                                <span class="badge bg-success me-1">{{ __('Primary') }}</span>
                                @endif
                                @if($address->is_billing)
                                <span class="badge bg-label-info me-1">{{ __('Billing') }}</span>
                                @endif
                                @if($address->is_shipping)
                                <span class="badge bg-label-warning me-1">{{ __('Shipping') }}</span>
                                @endif
                            </div>
                            @if($canEdit)
                            <div class="dropdown">
                                <button class="btn btn-sm btn-icon btn-text-secondary rounded-pill dropdown-toggle hide-arrow" data-bs-toggle="dropdown">
                                    <i class="bx bx-dots-vertical-rounded"></i>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-end">
                                    <li>
                                        <a class="dropdown-item" href="javascript:void(0);" onclick="editAddress({{ $address->id }})">
                                            <i class="bx bx-edit me-1"></i>{{ __('Edit') }}
                                        </a>
                                    </li>
                                    <li>
                                        <a class="dropdown-item text-danger" href="javascript:void(0);" onclick="deleteAddress({{ $address->id }})">
                                            <i class="bx bx-trash me-1"></i>{{ __('Delete') }}
                                        </a>
                                    </li>
                                </ul>
                            </div>
                            @endif
                        </div>
                        <h6 class="fw-semibold text-capitalize">{{ $addressTypes[$address->address_type] ?? ucfirst($address->address_type) }}</h6>
                        <p class="mb-1">{{ $address->address_line_1 }}</p>
                        @if($address->address_line_2)
                        <p class="mb-1">{{ $address->address_line_2 }}</p>
                        @endif
                        <p class="mb-0">{{ $address->city }}@if($address->state), {{ $address->state }}@endif @if($address->postal_code){{ $address->postal_code }}@endif</p>
                        @if($address->country)
                        <p class="mb-0 text-muted">{{ $address->country }}</p>
                        @endif
                    </div>
                </div>
                @endforeach
            </div>
        @else
            <div class="text-center py-5">
                <div class="mb-3">
                    <i class="bx bx-map-alt bx-lg text-muted"></i>
                </div>
                <p class="text-muted mb-3">{{ __('No addresses found') }}</p>
                @if($canEdit)
                <button type="button" class="btn btn-primary" onclick="openAddressOffcanvas()">
                    <i class="bx bx-plus me-1"></i>{{ __('Add First Address') }}
                </button>
                @endif
            </div>
        @endif
    </div>
</div>

{{-- Include the offcanvas form --}}
@include('systemcore::partials.entity._address_offcanvas', [
    'entityType' => $entityType,
    'entityId' => $entityId
])
