<?php

namespace App\Livewire\Studio\Catalog\Product;

use App\Livewire\Forms\Studio\Catalog\ProductForm;
use App\Models\Outlet;
use App\Traits\WithAuthorization;
use App\Traits\WithOutletSelector;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
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()
    {
        $this->outlets = Outlet::pluck('name', 'id')->toArray();
    }

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

        $this->form->store();

        $this->dispatch('refreshDatatable');

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

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

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