'showIndex', ]; // Khi search thay đổi, reset pagination public function updatingSearch() { $this->resetPage(); } // Chuyển sang form (create/edit) public function showForm(int $id = null) { $this->mode = 'form'; $this->editingId = $id; $this->name = $id ? Permission::findOrFail($id)->name : ''; } // Quay về index, refresh list public function showIndex() { $this->mode = 'index'; $this->resetPage(); $this->name = ''; $this->editingId = null; } // Xóa public function delete(int $id) { Permission::findOrFail($id)->delete(); session()->flash('message', 'Permission deleted.'); $this->resetPage(); } // Lưu form public function save() { $rules = ['name' => 'required|string|unique:permissions,name' .($this->editingId ? ",{$this->editingId}" : '')]; $this->validate($rules); Permission::updateOrCreate( ['id' => $this->editingId], ['name' => $this->name] ); session()->flash('message', 'Permission saved.'); $this->emit('permissionSaved'); } public function render() { $permissions = Permission::where('name', 'like', "%{$this->search}%") ->orderByDesc('id') ->paginate(10); return view('components.permission.manager', compact('permissions')); } }