<?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\WithAuthorization;
use App\Traits\WithOutletSelector;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
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)
    {
        $this->form->setProduct($product);

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

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

        $this->form->update();

        $this->dispatch('refreshDatatable');

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

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

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