project = $project; $this->phases = $project->phases()->with('currentLayer')->get(); $this->activeLayers = $this->phases->pluck('id')->toArray(); $this->loadTemplates(); } public function toggleLayer($phaseId) { if (in_array($phaseId, $this->activeLayers)) { $this->activeLayers = array_diff($this->activeLayers, [$phaseId]); } else { $this->activeLayers[] = $phaseId; } $this->dispatch('layersUpdated', $this->activeLayers); } public function updateProgress($featureId, $newProgress, $comment = null) { $feature = Feature::findOrFail($featureId); $user = Auth::user(); if (!$user->can('update progress') && !$user->hasRole('Admin')) { $this->dispatch('notify', 'Sin permisos'); return; } $feature->progress = min(100, max(0, $newProgress)); $feature->save(); // Actualizar progreso de la fase (sumar promedio) $phase = Phase::find($feature->layer->phase_id); $phase->progress_percent = $phase->features()->avg('progress'); $phase->save(); $phase->progressUpdates()->create([ 'user_id' => $user->id, 'progress_percent' => $phase->progress_percent, 'comment' => $comment, ]); $this->dispatch('progressUpdated', $featureId, $feature->progress); $this->dispatch('notify', 'Progreso actualizado'); $this->editProgress = $feature->progress; } public function loadTemplates() { $this->templates = InspectionTemplate::where('project_id', $this->project->id)->get(); } public function selectFeature($featureId, $featureProps) { $feature = Feature::with('template')->find($featureId); if (!$feature) return; $this->selectedFeature = $feature; $this->selectedPhaseId = $feature->layer->phase_id; $this->editProgress = $feature->progress; $this->editResponsible = $feature->responsible; $this->selectedTemplateId = $feature->template_id; $this->loadInspectionHistory(); $this->resetInspectionForm(); $this->dispatch('featureSelected', $featureId); } public function saveFeatureProgress() { if (!$this->selectedFeature || !$this->selectedPhaseId) { return; } $this->updateProgress($this->selectedPhaseId, $this->editProgress, $this->editComment); $this->editComment = ''; } public function resetInspectionForm() { $this->inspectionFormData = []; if ($this->selectedTemplateId) { $template = InspectionTemplate::find($this->selectedTemplateId); if ($template) { foreach ($template->fields as $field) { $this->inspectionFormData[$field['name']] = ''; } } } } public function loadInspectionHistory() { if (!$this->selectedFeature || !$this->selectedPhaseId) { $this->inspectionHistory = []; return; } $layer = Phase::find($this->selectedPhaseId)->currentLayer; if ($layer) { $this->inspectionHistory = Inspection::where('layer_id', $layer->id) ->where('feature_id', $this->selectedFeature['id']) ->with('user', 'template') ->orderBy('created_at', 'desc') ->get(); } } public function saveInspection() { if (!$this->selectedFeature || !$this->selectedPhaseId) { return; } $this->validate([ 'selectedTemplateId' => 'required|exists:inspection_templates,id', ]); $layer = Phase::find($this->selectedPhaseId)->currentLayer; if (!$layer) return; $inspection = Inspection::create([ 'project_id' => $this->project->id, 'layer_id' => $this->selectedFeature->layer_id, 'feature_id' => $this->selectedFeature->id, 'template_id' => $this->selectedTemplateId, 'user_id' => auth()->id(), 'data' => $this->inspectionFormData, ]); // Opcional: actualizar el progreso del elemento en el GeoJSON if (isset($this->inspectionFormData['progress'])) { $this->updateProgress($this->selectedPhaseId, $this->inspectionFormData['progress'], 'Inspección registrada'); } $this->loadInspectionHistory(); $this->resetInspectionForm(); $this->dispatch('notify', 'Inspección guardada'); } public function updateFeatureTemplate($templateId) { // Actualizar el template asociado al elemento (podrías guardarlo en la capa GeoJSON) if ($this->selectedFeature && $this->selectedPhaseId) { $layer = Phase::find($this->selectedPhaseId)->currentLayer; if ($layer) { $geojson = $layer->geojson_data; foreach ($geojson['features'] as &$feature) { if ($feature['properties']['id'] == $this->selectedFeature['id']) { $feature['properties']['template_id'] = $templateId; break; } } $layer->geojson_data = $geojson; $layer->save(); } } $this->selectedTemplateId = $templateId; $this->resetInspectionForm(); } public function toggleFullscreen() { $this->formFullscreen = !$this->formFullscreen; if (!$this->formFullscreen) { $this->dispatch('mapResize'); } } // Añadir al final de la clase ProjectMap public function refreshTemplates() { $this->templates = InspectionTemplate::where('project_id', $this->project->id)->get(); } // Asignar template ahora actualiza el campo template_id public function assignTemplateToFeature($templateId) { if (!$this->selectedFeature) return; $this->selectedFeature->template_id = $templateId; $this->selectedFeature->save(); $this->selectedTemplateId = $templateId; $this->resetInspectionForm(); $this->dispatch('notify', 'Template asignado'); } public function render() { return view('livewire.projects.project-map', [ 'project' => $this->project, 'phases' => $this->phases, ]); } }