<?php

namespace App\Livewire\Studio\Catalog\Product;

use App\Livewire\Forms\Studio\Catalog\ProductForm;
use App\Models\Outlet;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithOutletSelector;
use App\Traits\Components\WithToast;
use App\Traits\Utilities\WithUpdatedData;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Title;
use Livewire\Component;

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

    public ProductForm $form;

    public array $outlets = [];

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

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

        $this->form->store();

        $this->redirectRoute('studio.catalog.product.index', [
            'notification' => 'Produk berhasil ditambahkan.',
        ], navigate: true);
    }

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