project = $project; abort_unless($this->canAccessProject() && Auth::user()->can('edit issues'), 403); $this->loadTemplates(); } private function canAccessProject(): bool { $user = Auth::user(); return $user->can('manage all') || $this->project->users()->where('user_id', $user->id)->exists(); } public function loadTemplates(): void { $this->templates = IssueChecklistTemplate::where('project_id', $this->project->id) ->orderBy('name')->get(); } public function newTemplate(): void { $this->reset(['editingId', 'name']); $this->items = ['']; $this->resetErrorBag(); $this->showForm = true; } public function edit($id): void { $t = IssueChecklistTemplate::where('project_id', $this->project->id)->findOrFail($id); $this->editingId = $t->id; $this->name = $t->name; $this->items = array_values($t->items ?: ['']) ?: ['']; $this->resetErrorBag(); $this->showForm = true; } public function addItemLine(): void { $this->items[] = ''; } public function removeItemLine(int $i): void { unset($this->items[$i]); $this->items = array_values($this->items); if (empty($this->items)) { $this->items = ['']; } } public function save(): void { $this->validate([ 'name' => 'required|string|max:255', 'items' => 'required|array', 'items.*' => 'nullable|string|max:255', ]); $items = array_values(array_filter(array_map('trim', $this->items), fn ($v) => $v !== '')); if (empty($items)) { $this->addError('items', 'AƱade al menos una tarea.'); return; } IssueChecklistTemplate::updateOrCreate( ['id' => $this->editingId, 'project_id' => $this->project->id], ['name' => $this->name, 'items' => $items, 'project_id' => $this->project->id], ); $this->showForm = false; $this->loadTemplates(); $this->dispatch('notify', 'Plantilla guardada'); } public function delete($id): void { IssueChecklistTemplate::where('project_id', $this->project->id)->findOrFail($id)->delete(); $this->loadTemplates(); $this->dispatch('notify', 'Plantilla eliminada'); } public function render() { return view('livewire.issues.issue-checklist-manager'); } }