<?php

namespace App\Livewire\Studio\Catalog\Product;

use App\Livewire\Forms\Studio\Catalog\ProductForm;
use App\Models\Outlet;
use App\Models\Product;
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('Ubah Produk')]
class Edit extends Component
{
    use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData;

    public ProductForm $form;

    public array $outlets = [];

    public function mount(Product $product): void
    {
        $this->form->setProduct($product);

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

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

        $this->form->update();

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

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