@extends('account.layout')

@section('account-content')
<div>
    <div class="flex items-center justify-between mb-4">
        <h1 class="text-2xl font-bold">My Addresses</h1>
        <button type="button" 
            class="inline-flex items-center gap-1.5 px-4 py-2 rounded-lg font-medium transition-colors bg-green-600 hover:bg-green-700 text-white"
            data-modal-target="addAddressModal">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
            Add New Address
        </button>
    </div>

    @if($addresses->count() > 0)
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            @foreach($addresses as $address)
                <div class="bg-white rounded-xl border border-gray-100 h-full">
                    <div class="p-6">
                        <div class="flex items-start justify-between gap-4">
                            <div class="min-w-0">
                                <div class="flex items-center gap-2 mb-1 flex-wrap">
                                    <h5 class="font-semibold truncate">{{ $address->first_name }} {{ $address->last_name }}</h5>
                                    @if($address->is_default)
                                        <span class="bg-green-100 text-green-800 text-xs font-medium px-2 py-0.5 rounded-full shrink-0">Default</span>
                                    @endif
                                    <span class="bg-gray-100 text-gray-700 text-xs font-medium px-2 py-0.5 rounded-full shrink-0 capitalize">{{ $address->type }}</span>
                                </div>
                                <p class="text-sm text-gray-600">
                                    {{ $address->address_line1 }}<br>
                                    {{ $address->city }}, {{ $address->state }} {{ $address->postal_code }}<br>
                                    {{ $countries[$address->country] ?? $address->country }}
                                </p>
                            </div>
                            <div class="flex items-center gap-1 shrink-0">
                                <button type="button"
                                    class="inline-flex items-center justify-center p-1.5 text-sm rounded-lg font-medium transition-colors border border-gray-300 text-gray-600 hover:bg-gray-100"
                                    data-modal-target="editAddressModal{{ $address->id }}">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
                                </button>
                                <form action="{{ route('account.addresses.delete', $address) }}" method="POST" class="inline" 
                                    onsubmit="return confirm('Are you sure you want to delete this address?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="inline-flex items-center justify-center p-1.5 text-sm rounded-lg font-medium transition-colors border border-red-500 text-red-500 hover:bg-red-500 hover:text-white">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                @include('account.partials.address-form-modal', [
                    'id' => 'editAddressModal'.$address->id,
                    'title' => 'Edit Address',
                    'action' => route('account.addresses.update', $address),
                    'method' => 'PUT',
                    'address' => $address,
                ])
            @endforeach
        </div>
    @else
        <div class="text-center py-12">
            <svg class="w-16 h-16 text-gray-300 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
            <h3 class="text-lg font-semibold text-gray-700">No addresses saved</h3>
            <p class="text-gray-500 mb-4">Add a new address to make checkout easier!</p>
        </div>
    @endif
</div>

<div class="fixed inset-0 z-50 hidden" id="addAddressModal" tabindex="-1">
    <div class="fixed inset-0 bg-black/50 z-40"></div>
    <div class="fixed inset-0 z-50 flex items-center justify-center p-4">
        <div class="bg-white rounded-xl shadow-xl w-full max-w-lg max-h-full overflow-y-auto">
            <div class="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
                <h5 class="text-lg font-semibold">Add New Address</h5>
                <button type="button" class="text-gray-400 hover:text-gray-600 transition-colors p-1" data-modal-close>
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                </button>
            </div>
            <form action="{{ route('account.addresses.store') }}" method="POST">
                @csrf
                <div class="px-6 py-4 space-y-3">
                    <div>
                        <label for="first_name" class="block text-sm font-medium text-gray-700 mb-1">First Name</label>
                        <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="first_name" name="first_name" required>
                    </div>
                    <div>
                        <label for="last_name" class="block text-sm font-medium text-gray-700 mb-1">Last Name</label>
                        <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="last_name" name="last_name" required>
                    </div>
                    <div>
                        <label for="type" class="block text-sm font-medium text-gray-700 mb-1">Address Type</label>
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg bg-white focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="type" name="type" required>
                            <option value="shipping">Shipping</option>
                            <option value="billing">Billing</option>
                        </select>
                    </div>
                    <div>
                        <label for="address_line1" class="block text-sm font-medium text-gray-700 mb-1">Address Line 1</label>
                        <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="address_line1" name="address_line1" required>
                    </div>
                    <div>
                        <label for="address_line2" class="block text-sm font-medium text-gray-700 mb-1">Address Line 2 <span class="text-gray-400">(Optional)</span></label>
                        <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="address_line2" name="address_line2">
                    </div>
                    <div class="grid grid-cols-2 gap-3">
                        <div>
                            <label for="city" class="block text-sm font-medium text-gray-700 mb-1">City</label>
                            <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="city" name="city" required>
                        </div>
                        <div>
                            <label for="state" class="block text-sm font-medium text-gray-700 mb-1">State</label>
                            <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="state" name="state" required>
                        </div>
                    </div>
                    <div class="grid grid-cols-2 gap-3">
                        <div>
                            <label for="postal_code" class="block text-sm font-medium text-gray-700 mb-1">Postal Code</label>
                            <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="postal_code" name="postal_code" required>
                        </div>
                        <div>
                            <label for="country" class="block text-sm font-medium text-gray-700 mb-1">Country</label>
                            <select class="w-full px-3 py-2 border border-gray-300 rounded-lg bg-white focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="country" name="country" required>
                                <option value="">Select Country</option>
                                @foreach($countries as $code => $name)
                                    <option value="{{ $code }}">{{ $name }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div>
                        <label for="phone" class="block text-sm font-medium text-gray-700 mb-1">Phone</label>
                        <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none" id="phone" name="phone" required>
                    </div>
                    <div class="flex items-center gap-2">
                        <input type="checkbox" class="h-4 w-4 text-green-600 border-gray-300 rounded focus:ring-green-500" id="is_default" name="is_default">
                        <label class="text-sm text-gray-700" for="is_default">Set as default address</label>
                    </div>
                </div>
                <div class="px-6 py-4 border-t border-gray-200 flex justify-end gap-2">
                    <button type="button" class="inline-flex items-center px-4 py-2 rounded-lg font-medium transition-colors bg-gray-100 hover:bg-gray-200 text-gray-700" data-modal-close>Close</button>
                    <button type="submit" class="inline-flex items-center px-4 py-2 rounded-lg font-medium transition-colors bg-green-600 hover:bg-green-700 text-white">Save Address</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
