2026-05-07 23:31:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
|
|
|
|
use Livewire\Component;
|
2026-06-16 18:05:53 +02:00
|
|
|
use Livewire\Attributes\Layout;
|
2026-05-27 20:28:44 +02:00
|
|
|
use App\Models\Project;
|
2026-06-16 18:05:53 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
2026-05-07 23:31:33 +02:00
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
#[Layout('layouts.app')]
|
2026-05-07 23:31:33 +02:00
|
|
|
class ProjectForm extends Component
|
|
|
|
|
{
|
2026-06-16 18:05:53 +02:00
|
|
|
public ?Project $project = null;
|
2026-05-27 20:28:44 +02:00
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
// Identification
|
|
|
|
|
public string $name = '';
|
|
|
|
|
public string $reference = '';
|
|
|
|
|
public string $status = 'planning';
|
|
|
|
|
|
|
|
|
|
// Location
|
|
|
|
|
public string $address = '';
|
|
|
|
|
public string $country = '';
|
|
|
|
|
public string $lat = '';
|
|
|
|
|
public string $lng = '';
|
|
|
|
|
|
|
|
|
|
// Planning
|
|
|
|
|
public string $startDate = '';
|
|
|
|
|
public string $endDateEstimated = '';
|
2026-05-27 20:28:44 +02:00
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
public function mount(?Project $project = null): void
|
2026-05-27 20:28:44 +02:00
|
|
|
{
|
2026-06-16 18:05:53 +02:00
|
|
|
if ($project && $project->exists) {
|
|
|
|
|
Gate::authorize('edit projects', $project);
|
|
|
|
|
$this->project = $project;
|
|
|
|
|
$this->name = $project->name;
|
|
|
|
|
$this->reference = $project->reference ?? '';
|
|
|
|
|
$this->status = $project->status;
|
|
|
|
|
$this->address = $project->address;
|
|
|
|
|
$this->country = $project->country ?? '';
|
|
|
|
|
$this->lat = (string) ($project->lat ?? '');
|
|
|
|
|
$this->lng = (string) ($project->lng ?? '');
|
|
|
|
|
$this->startDate = $project->start_date->format('Y-m-d');
|
|
|
|
|
$this->endDateEstimated = $project->end_date_estimated?->format('Y-m-d') ?? '';
|
|
|
|
|
} else {
|
|
|
|
|
Gate::authorize('create projects');
|
|
|
|
|
$this->startDate = today()->format('Y-m-d');
|
2026-05-27 20:28:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
// Called from JS after map click / marker drag + reverse geocode
|
|
|
|
|
public function setLocation(string $lat, string $lng, string $address = '', string $country = ''): void
|
2026-05-27 20:28:44 +02:00
|
|
|
{
|
|
|
|
|
$this->lat = $lat;
|
|
|
|
|
$this->lng = $lng;
|
2026-06-16 18:05:53 +02:00
|
|
|
if ($address) $this->address = $address;
|
|
|
|
|
if ($country) $this->country = strtolower($country);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
|
'reference' => 'nullable|string|max:100',
|
|
|
|
|
'status' => 'required|in:planning,in_progress,paused,completed',
|
|
|
|
|
'address' => 'required|string',
|
|
|
|
|
'country' => 'nullable|string|size:2',
|
|
|
|
|
'lat' => 'nullable|numeric|between:-90,90',
|
|
|
|
|
'lng' => 'nullable|numeric|between:-180,180',
|
|
|
|
|
'startDate' => 'required|date',
|
|
|
|
|
'endDateEstimated' => 'nullable|date|after_or_equal:startDate',
|
|
|
|
|
];
|
2026-05-27 20:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
protected $validationAttributes = [
|
|
|
|
|
'name' => 'nombre',
|
|
|
|
|
'reference' => 'referencia',
|
|
|
|
|
'status' => 'estado',
|
|
|
|
|
'address' => 'dirección',
|
|
|
|
|
'country' => 'país',
|
|
|
|
|
'lat' => 'latitud',
|
|
|
|
|
'lng' => 'longitud',
|
|
|
|
|
'startDate' => 'fecha de inicio',
|
|
|
|
|
'endDateEstimated' => 'fecha de fin estimada',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function save(): void
|
2026-05-27 20:28:44 +02:00
|
|
|
{
|
|
|
|
|
$this->validate();
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
$data = [
|
|
|
|
|
'name' => $this->name,
|
|
|
|
|
'reference' => $this->reference ?: null,
|
|
|
|
|
'status' => $this->status,
|
|
|
|
|
'address' => $this->address,
|
|
|
|
|
'country' => $this->country ?: null,
|
|
|
|
|
'lat' => $this->lat ?: null,
|
|
|
|
|
'lng' => $this->lng ?: null,
|
|
|
|
|
'start_date' => $this->startDate,
|
|
|
|
|
'end_date_estimated' => $this->endDateEstimated ?: null,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($this->project && $this->project->exists) {
|
|
|
|
|
$this->project->update($data);
|
|
|
|
|
session()->flash('notify', 'Proyecto actualizado correctamente.');
|
2026-05-27 20:28:44 +02:00
|
|
|
} else {
|
2026-06-16 18:05:53 +02:00
|
|
|
$project = Project::create(array_merge($data, ['created_by' => Auth::id()]));
|
|
|
|
|
$project->users()->attach(Auth::id(), ['role_in_project' => 'supervisor']);
|
|
|
|
|
session()->flash('notify', 'Proyecto creado correctamente.');
|
2026-05-27 20:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
$this->redirect(route('projects.index'), navigate: true);
|
2026-05-27 20:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-07 23:31:33 +02:00
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.projects.project-form');
|
|
|
|
|
}
|
|
|
|
|
}
|