<?php

use App\Models\{Address, Country};
use Livewire\Component;
use Livewire\Attributes\Title;
use Mary\Traits\Toast;
use Illuminate\Support\Collection;
use App\Traits\ManageAddress;
use Illuminate\Support\Facades\Auth;

return new #[Title('Modifier l\'adresse')]
class extends Component {
    use Toast, ManageAddress;

    /*
     * L'adresse à modifier.
     */
    public Address $myAddress;

    /*
     * Initialise le composant avec l'adresse à modifier.
     */
    public function mount(Address $address): void
    {
        if ($address->user_id !== Auth::id()) {
            abort(403);
        }

        $this->myAddress = $address;
        $this->fill($this->myAddress);
        $this->countries = Country::all();
        $this->country = $this->countries->find($this->country_id); // ✅ Charge le pays correspondant à l'adresse
    }

    /**
     * Enregistre les modifications de l'adresse.
     * @return void
     */
    public function save(): void
    {
        $data = $this->validate($this->rules());

        $this->myAddress->update($data);

        $this->success(__('Adresse mise à jour avec succès.'), redirectTo: route('addresses'));
    }

    protected function view($data = [])
    {
        return app('view')->file('E:\laragon\www\shop2\storage\framework\views/livewire/views/ca2260bd.blade.php', $data);
    }
}; 