<?php

namespace App\Livewire\Studio\Master\User;

use App\Livewire\Forms\Studio\Master\UserForm;
use App\Models\Outlet;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithOutletSelector;
use App\Traits\Components\WithRoleSelector;
use App\Traits\Components\WithToast;
use App\Traits\Utilities\WithUpdatedData;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Title;
use Livewire\Component;
use Spatie\Permission\Models\Role;

#[Title('Tambah Pegawai')]
class Create extends Component
{
    use WithAuthorization, WithOutletSelector, WithRoleSelector, WithToast, WithUpdatedData;

    public UserForm $form;

    public array $outlets = [];

    public array $roles = [];

    public function mount(): void
    {
        $this->outlets = Outlet::pluck('name', 'id')->toArray();

        $this->roles = Role::where('name', '!=', 'Developer')->orderBy('id')->pluck('name', 'id')->toArray();
    }

    public function save(): void
    {
        $this->canOrAbort('create user');

        $this->form->store();

        $this->redirectRoute('studio.master.user.index', [
            'notification' => 'Pegawai berhasil ditambahkan.',
        ], navigate: true);
    }

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