<?php

namespace App\Livewire\Studio\Manage\Article;

use App\Livewire\Forms\Studio\Manage\ArticleForm;
use App\Models\Article;
use App\Traits\WithAuthorization;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Ubah Artikel')]
class Edit extends Component
{
    use WithAuthorization, WithToast, WithUpdatedData;

    public ArticleForm $form;

    public function mount(Article $article)
    {
        $this->form->setArticle($article);
    }

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

        $this->form->update();

        $this->dispatch('refreshDatatable');

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

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

    public function render()
    {
        return view('livewire.studio.manage.article.form', [
            'pageTitle' => 'Ubah Artikel',
        ]);
    }
}
