Template::orderBy('name')->get(), 'types' => Template::TYPES, ]); } public function create(): void { $this->resetForm(); $this->showForm = true; } public function edit(int $id): void { $template = Template::findOrFail($id); $this->editingId = $template->id; $this->name = $template->name; $this->body = $template->body; $this->type = $template->type; $this->title = $template->title; $this->target = $template->target; $this->placeholders = $template->placeholders ? implode(', ', $template->placeholders) : ''; $this->showForm = true; } public function save(): void { $validated = $this->validate([ 'name' => ['required', 'string', 'max:255'], 'body' => ['required', 'string'], 'type' => ['nullable', 'string', 'in:' . implode(',', Template::TYPES)], 'title' => ['nullable', 'string', 'max:255'], 'target' => ['nullable', 'string', 'max:255'], 'placeholders' => ['nullable', 'string'], ]); // Parse placeholders from comma-separated string $placeholderArray = null; if ($validated['placeholders']) { $placeholderArray = array_map('trim', explode(',', $validated['placeholders'])); $placeholderArray = array_filter($placeholderArray); } Template::updateOrCreate(['id' => $this->editingId], [ 'name' => $validated['name'], 'body' => $validated['body'], 'type' => $validated['type'], 'title' => $validated['title'], 'target' => $validated['target'], 'placeholders' => $placeholderArray, ]); $this->resetForm(); $this->showForm = false; } public function delete(int $id): void { Template::findOrFail($id)->delete(); } public function resetForm(): void { $this->editingId = null; $this->name = ''; $this->body = ''; $this->type = null; $this->title = null; $this->target = null; $this->placeholders = ''; $this->resetValidation(); } }