<?php

use App\Models\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('Créer une adresse')]
class extends Component {
    use Toast, ManageAddress;

    /**
     * Initialise le composant.
     */
    public function mount(): void
    {
        $this->countries = Country::all();
        if ($this->countries->isNotEmpty()) {
            $this->country_id = $this->countries->first()->id;
            $this->country = $this->countries->first(); // Charge le pays par défaut
        }
    }

    /**
     * Enregistre la nouvelle adresse.
     * @return void
     */
    public function save(): void
    {
        $data = $this->validate($this->rules());

        Auth::user()->addresses()->create($data);

        $this->success(__('Adresse créée avec succès.'), redirectTo: route('addresses'));
    }

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

