Files
construprogress/app/Livewire/TemplateManager.php
T

161 lines
4.4 KiB
PHP
Raw Normal View History

2026-05-07 23:31:33 +02:00
<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\InspectionTemplate;
use App\Models\Project;
use App\Models\Phase;
use Illuminate\Support\Facades\Auth;
2026-05-07 23:31:33 +02:00
class TemplateManager extends Component
{
public $project;
public $templates;
public $phases;
2026-05-07 23:31:33 +02:00
public $editingTemplate = null;
public $showForm = false; // Controla si mostrar el formulario
2026-05-07 23:31:33 +02:00
public $form = [
'name' => '',
2026-05-07 23:31:33 +02:00
'description' => '',
'phase_id' => null,
'fields' => [],
2026-05-07 23:31:33 +02:00
];
public $fieldTypes = [
'text' => 'Texto corto',
'textarea' => 'Texto largo',
'integer' => 'Número entero',
'decimal' => 'Número decimal',
2026-05-07 23:31:33 +02:00
'percentage' => 'Porcentaje (0-100)',
'boolean' => 'Sí/No (checkbox)',
'date' => 'Fecha',
'select' => 'Lista desplegable',
2026-05-07 23:31:33 +02:00
];
protected $listeners = ['showTemplateForm' => 'newTemplate'];
2026-05-07 23:31:33 +02:00
public function mount(Project $project)
{
$user = Auth::user();
if (!$user->hasRole('Admin') && !$project->users()->where('user_id', $user->id)->exists()) {
abort(403);
}
2026-05-07 23:31:33 +02:00
$this->project = $project;
$this->loadPhases();
2026-05-07 23:31:33 +02:00
$this->loadTemplates();
}
public function loadPhases()
{
$this->phases = $this->project->phases()->orderBy('name')->get();
}
2026-05-07 23:31:33 +02:00
public function loadTemplates()
{
$this->templates = InspectionTemplate::where('project_id', $this->project->id)->get();
2026-05-07 23:31:33 +02:00
}
public function newTemplate()
{
$this->resetForm();
$this->editingTemplate = null;
2026-05-07 23:31:33 +02:00
$this->showForm = true;
}
public function editTemplate($id)
{
$template = InspectionTemplate::where('id', $id)
->where('project_id', $this->project->id)
->firstOrFail();
$this->form = $template->only(['name', 'description', 'phase_id', 'fields']);
2026-05-07 23:31:33 +02:00
$this->editingTemplate = $id;
$this->showForm = true;
}
public function cancelForm()
{
$this->showForm = false;
$this->resetForm();
}
public function resetForm()
{
$this->form = [
'name' => '',
2026-05-07 23:31:33 +02:00
'description' => '',
'phase_id' => null,
'fields' => [],
2026-05-07 23:31:33 +02:00
];
$this->editingTemplate = null;
}
public function addField()
{
$this->form['fields'][] = [
'name' => '',
'label' => '',
'type' => 'text',
'options' => [],
2026-05-07 23:31:33 +02:00
'required' => false,
'min' => null,
'max' => null,
'step' => null,
2026-05-07 23:31:33 +02:00
];
}
public function removeField($index)
{
unset($this->form['fields'][$index]);
$this->form['fields'] = array_values($this->form['fields']);
}
public function saveTemplate()
{
$this->validate([
'form.name' => 'required|string|max:255',
'form.phase_id' => 'nullable|exists:phases,id',
'form.fields' => 'array',
2026-05-07 23:31:33 +02:00
]);
if ($this->editingTemplate) {
$template = InspectionTemplate::where('id', $this->editingTemplate)
->where('project_id', $this->project->id)
->firstOrFail();
$template->update([
'name' => $this->form['name'],
'description' => $this->form['description'],
'phase_id' => $this->form['phase_id'],
'fields' => $this->form['fields'],
]);
session()->flash('message', 'Template actualizado');
2026-05-07 23:31:33 +02:00
} else {
InspectionTemplate::create([
'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');
2026-05-07 23:31:33 +02:00
}
$this->cancelForm();
$this->loadTemplates();
}
public function deleteTemplate($id)
{
InspectionTemplate::where('id', $id)
->where('project_id', $this->project->id)
->firstOrFail()
->delete();
2026-05-07 23:31:33 +02:00
$this->loadTemplates();
session()->flash('message', 'Template eliminado');
2026-05-07 23:31:33 +02:00
}
public function render()
{
return view('livewire.template-manager');
}
}