<?php

namespace App\Livewire\Admin\Navigation\Menu;

use Livewire\Component;
use Illuminate\Support\Str;
use Livewire\Attributes\Validate;
use App\Models\Admin\NavigationMenu;

class Edit extends Component
{
    public $navigationMenu;

    #[Validate('required|min:3|max:255|string')]
    public $name;

    public $slug;
    public $description;

    public function mount(NavigationMenu $navigationMenu)
    {
        $this->navigationMenu = $navigationMenu;
        $this->name = $navigationMenu->name;
        $this->slug = $navigationMenu->slug;
        $this->description = $navigationMenu->description;
    }

    protected function rules()
    {
        return [
            'slug' => 'required|min:3|max:255|string|unique:navigation_menus,slug,' . $this->navigationMenu->id,
        ];
    }

    public function updatedName($value)
    {
        $this->slug = Str::slug($value);
    }

    public function save()
    {
        $this->validate();

        $this->navigationMenu->update([
            'name' => $this->name,
            'slug' => $this->slug,
            'description' => $this->description,
        ]);

        activity()->performedOn($this->navigationMenu)->causedBy(auth()->id())->log('Menü güncellendi');

        $this->dispatch('showAlert', [
            'title' => 'Başarılı!', 'text' => 'Menü güncellendi.', 'icon' => 'success'
        ]);
    }

    public function render()
    {
        return view('livewire.admin.navigation.menu.edit');
    }
}
