Add phase selection to template manager and create new template button

This commit is contained in:
2026-05-11 15:28:16 +02:00
parent 43e8a70f9c
commit 436e3ba5cf
2 changed files with 33 additions and 2 deletions
+13 -1
View File
@@ -5,16 +5,19 @@ namespace App\Livewire;
use Livewire\Component;
use App\Models\InspectionTemplate;
use App\Models\Project;
use App\Models\Phase;
class TemplateManager extends Component
{
public $project;
public $templates;
public $phases;
public $editingTemplate = null;
public $showForm = false; // Controla si mostrar el formulario
public $form = [
'name' => '',
'description' => '',
'phase_id' => null,
'fields' => [],
];
public $fieldTypes = [
@@ -31,9 +34,15 @@ class TemplateManager extends Component
public function mount(Project $project)
{
$this->project = $project;
$this->loadPhases();
$this->loadTemplates();
}
public function loadPhases()
{
$this->phases = $this->project->phases()->orderBy('name')->get();
}
public function loadTemplates()
{
$this->templates = InspectionTemplate::where('project_id', $this->project->id)->get();
@@ -49,7 +58,7 @@ class TemplateManager extends Component
public function editTemplate($id)
{
$template = InspectionTemplate::find($id);
$this->form = $template->only(['name', 'description', 'fields']);
$this->form = $template->only(['name', 'description', 'phase_id', 'fields']);
$this->editingTemplate = $id;
$this->showForm = true;
}
@@ -65,6 +74,7 @@ class TemplateManager extends Component
$this->form = [
'name' => '',
'description' => '',
'phase_id' => null,
'fields' => [],
];
$this->editingTemplate = null;
@@ -94,6 +104,7 @@ class TemplateManager extends Component
{
$this->validate([
'form.name' => 'required|string|max:255',
'form.phase_id' => 'nullable|exists:phases,id',
'form.fields' => 'array',
]);
@@ -106,6 +117,7 @@ class TemplateManager extends Component
'name' => $this->form['name'],
'description' => $this->form['description'],
'project_id' => $this->project->id,
'phase_id' => $this->form['phase_id'],
'fields' => $this->form['fields'],
]);
session()->flash('message', 'Template creado');
@@ -34,6 +34,23 @@
<textarea wire:model="form.description" class="textarea textarea-bordered w-full" rows="2"></textarea>
</td>
</tr>
{{-- Fase asociada (opcional) --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">
{{__('Fase asociada (opcional)')}}
</td>
<td class="py-3">
<select wire:model="form.phase_id" class="select select-bordered w-full">
<option value="">Ninguna (global para el proyecto)</option>
@foreach($phases as $phase)
<option value="{{ $phase->id }}" {{ old('form.phase_id') == $phase->id ? 'selected' : '' }}>
{{ $phase->name }}
</option>
@endforeach
</select>
</td>
</tr>
</tbody>
</table>
@@ -107,6 +124,7 @@
<tr>
<th>Nombre</th>
<th>Descripción</th>
<th>Fase</th>
<th>Campos</th>
<th>Acciones</th>
</tr>
@@ -116,6 +134,7 @@
<tr>
<td>{{ $template->name }}</td>
<td>{{ $template->description ?? '-' }}</td>
<td>{{ $template->phase ? $template->phase->name : 'Global' }}</td>
<td>{{ count($template->fields) }}</td>
<td>
<button wire:click="editTemplate({{ $template->id }})" class="btn btn-xs btn-warning">
@@ -126,7 +145,7 @@
</tr>
@empty
<tr>
<td colspan="4" class="text-center">No hay templates creados. Presiona "Nuevo template" para comenzar.</td>
<td colspan="5" class="text-center">No hay templates creados. Presiona "Nuevo template" para comenzar.</td>
</tr>
@endforelse
</tbody>