<?php

namespace App\Http\Livewire;

use App\Models\PostalCode;
use Spatie\LivewireWizard\Components\StepComponent;

class PostalCodeStep extends StepComponent
{
    public $postalCodes = [];
    public $postal_code_id = null;

    protected $rules = [
        'postal_code_id' => 'required|integer',
    ];

    public function mount()
    {
        $this->postalCodes = PostalCode::selectRaw("id, CONCAT(code, ' ', name) as name")->get()
            ->pluck('name', 'id')
            ->toArray();
    }

     public function submitForm()
     {
         $this->validate();

         session()->put('client_input', ['postal_code_id' => $this->postal_code_id]);

         $this->showStep('consommation-step');
     }

    public function render()
    {
        return view('livewire.postal-code-step');
    }
}
