<?php

namespace App\Livewire\Studio\Catalog\Perfume;

use App\Livewire\Forms\Studio\Catalog\PerfumeForm;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Outlet;
use App\Traits\WithAuthorization;
use App\Traits\WithCategorySelector;
use App\Traits\WithOutletSelector;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
use Livewire\Attributes\Title;
use Livewire\Component;

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

    public PerfumeForm $form;

    public array $outlets = [];

    public array $brands = [];

    public array $categories = [];

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

        $this->brands = Brand::pluck('name', 'id')->toArray();

        $this->categories = Category::pluck('name', 'id')->toArray();
    }

    public function save()
    {
        $this->canOrAbort('create perfume');

        $this->form->store();

        $this->dispatch('refreshDatatable');

        $this->toast('Parfum berhasil ditambahkan.');

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

    public function render()
    {
        return view('livewire.studio.catalog.perfume.form', [
            'pageTitle' => 'Tambah Parfum',
        ]);
    }
}
