<?php

namespace App\Livewire\Studio\Master\User;

use App\Livewire\Forms\Studio\Master\UserForm;
use App\Models\Outlet;
use App\Models\User;
use App\Traits\WithAuthorization;
use App\Traits\WithOutletSelector;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Ubah Pegawai')]
class Edit extends Component
{
    use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData;

    public UserForm $form;

    public array $outlets = [];

    public function mount(User $user)
    {
        $this->form->setUser($user);
        $this->outlets = Outlet::pluck('name', 'id')->toArray();
    }

    public function save()
    {
        $this->canOrAbort('update user');

        $this->form->update();

        $this->toast('Pegawai berhasil diperbarui.');

        $this->redirectRoute('studio.master.user.index', navigate: true);
    }

    public function render()
    {
        return view('livewire.studio.master.user.form', [
            'pageTitle' => 'Ubah Pegawai',
        ]);
    }
}
