diff --git a/app/Livewire/Projects/ProjectMap.php b/app/Livewire/Projects/ProjectMap.php index dd6bfb3..ef843a5 100644 --- a/app/Livewire/Projects/ProjectMap.php +++ b/app/Livewire/Projects/ProjectMap.php @@ -4,6 +4,7 @@ namespace App\Livewire\Projects; use Livewire\Component; use Livewire\Attributes\On; +use Livewire\WithFileUploads; use Illuminate\Support\Facades\Auth; use App\Models\Project; use App\Models\Phase; @@ -15,6 +16,8 @@ use App\Models\Issue; class ProjectMap extends Component { + use WithFileUploads; + public Project $project; public $phases; public $activeLayers = []; // Now stores Layer IDs (not Phase IDs) @@ -54,6 +57,7 @@ class ProjectMap extends Component // Inspection workflow public $inspectionResult = ''; public $inspectionNotes = ''; + public $inspectionPhotos = []; // Issues public $openIssuesCount = 0; @@ -247,6 +251,7 @@ class ProjectMap extends Component $this->inspectionFormData = []; $this->inspectionResult = ''; $this->inspectionNotes = ''; + $this->inspectionPhotos = []; if ($this->selectedTemplateId) { $template = InspectionTemplate::find($this->selectedTemplateId); if ($template) { @@ -266,7 +271,10 @@ class ProjectMap extends Component $feature = Feature::with('layer.phase')->find($this->selectedFeature->id); if (!$feature || $feature->layer->phase->project_id !== $this->project->id) abort(403); - $this->validate(['selectedTemplateId' => 'required|exists:inspection_templates,id']); + $this->validate([ + 'selectedTemplateId' => 'required|exists:inspection_templates,id', + 'inspectionPhotos.*' => 'nullable|image|max:20480', + ]); $template = InspectionTemplate::find($this->selectedTemplateId); foreach ($template->fields as $field) { @@ -290,6 +298,22 @@ class ProjectMap extends Component 'data' => $this->inspectionFormData, ]); + // Fotos adjuntas a la inspección + foreach ($this->inspectionPhotos as $photo) { + $mime = $photo->getMimeType(); + $path = $photo->store("uploads/inspections/{$inspection->id}", 'public'); + $inspection->media()->create([ + 'name' => $photo->getClientOriginalName(), + 'file_path' => $path, + 'file_type' => $mime, + 'file_extension' => $photo->getClientOriginalExtension(), + 'file_size' => $photo->getSize(), + 'category' => str_starts_with($mime, 'image/') ? 'image' : 'document', + 'uploaded_by' => auth()->id(), + 'uuid' => (string) \Illuminate\Support\Str::uuid(), + ]); + } + if ($this->inspectionResult === 'fail') { Issue::create([ 'project_id' => $this->project->id, @@ -363,7 +387,7 @@ class ProjectMap extends Component public function viewInspection($id) { $ins = Inspection::where('project_id', $this->project->id) - ->with(['feature.layer.phase', 'template', 'user']) + ->with(['feature.layer.phase', 'template', 'user', 'media']) ->find($id); if (!$ins) return; $this->viewingInspection = [ @@ -379,6 +403,7 @@ class ProjectMap extends Component 'notes' => $ins->notes, 'data' => $ins->data ?? [], 'fields' => $ins->template?->fields ?? [], + 'photos' => $ins->media->map(fn ($m) => ['url' => $m->url, 'name' => $m->name])->values()->all(), ]; } diff --git a/app/Models/Inspection.php b/app/Models/Inspection.php index 76b735c..d04d3d7 100644 --- a/app/Models/Inspection.php +++ b/app/Models/Inspection.php @@ -49,6 +49,11 @@ class Inspection extends Model return $this->belongsTo(User::class, 'inspector_user_id'); } + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } + public function feature() { return $this->belongsTo(Feature::class, 'feature_id'); diff --git a/resources/views/livewire/projects/project-map.blade.php b/resources/views/livewire/projects/project-map.blade.php index 15fcc21..19b4700 100644 --- a/resources/views/livewire/projects/project-map.blade.php +++ b/resources/views/livewire/projects/project-map.blade.php @@ -214,37 +214,68 @@ @if($selectedTemplateId && !empty($inspectionFormData)) @php $template = $templates->firstWhere('id', $selectedTemplateId); @endphp @if($template) - @foreach($template->fields as $field) -
{{ $viewingInspection['notes'] }}
@endif + @if(!empty($viewingInspection['photos'])) +