user()->isAdmin() || auth()->user()->isManager(), 403); if ($request && $request->exists) { $this->serviceRequest = $request; $this->serviceRequestId = $request->id; $this->title = $request->title; $this->description = $request->description; $this->typeId = (string) $request->type_id; $this->priorityId = (string) $request->priority_id; $this->statusId = (string) $request->status_id; $this->clientId = (string) $request->client_id; $this->assignedTo = (string) $request->assigned_to; $this->equipmentId = (string) $request->equipment_id; $this->location = (string) $request->location; $this->plannedAt = (string) $request->planned_at?->format('Y-m-d\TH:i'); $this->deadlineAt = (string) $request->deadline_at?->format('Y-m-d\TH:i'); $this->resolutionNotes = (string) $request->resolution_notes; $this->clientRating = $request->client_rating; $this->clientComment = (string) $request->client_comment; } else { $this->statusId = (string) Status::query()->where('slug', StatusSlug::NEW->value)->value('id'); } } public function updatedTypeId(): void { if (!$this->typeId) { return; } $type = RequestType::query()->find($this->typeId); if ($type) { $base = $this->plannedAt !== '' ? Carbon::parse($this->plannedAt) : now(); $this->deadlineAt = $base->addHours($type->sla_hours)->format('Y-m-d\TH:i'); } } public function save(RequestService $requestService) { $this->validate([ 'title' => ['required', 'string', 'max:255'], 'description' => ['required', 'string'], 'typeId' => ['required', 'exists:request_types,id'], 'priorityId' => ['required', 'exists:priorities,id'], 'statusId' => ['nullable', 'exists:statuses,id'], 'clientId' => ['required', 'exists:users,id'], 'assignedTo' => ['nullable', 'exists:users,id'], 'equipmentId' => ['nullable', 'exists:equipment,id'], 'location' => ['nullable', 'string', 'max:255'], 'plannedAt' => ['nullable', 'date'], 'deadlineAt' => ['nullable', 'date'], 'resolutionNotes' => ['nullable', 'string'], 'clientRating' => ['nullable', 'integer', 'between:1,5'], 'clientComment' => ['nullable', 'string', 'max:2000'], 'attachments' => ['nullable', 'array', 'max:5'], 'attachments.*' => ['file', 'max:10240'], ]); if (!$this->serviceRequestId && empty($this->statusId)) { throw ValidationException::withMessages([ 'statusId' => 'Не удалось определить статус заявки.', ]); } $payload = [ 'title' => $this->title, 'description' => $this->description, 'type_id' => $this->typeId, 'priority_id' => $this->priorityId, 'status_id' => $this->statusId, 'client_id' => $this->clientId, 'assigned_to' => $this->assignedTo ?: null, 'equipment_id' => $this->equipmentId ?: null, 'location' => $this->location, 'planned_at' => $this->plannedAt ?: null, 'deadline_at' => $this->deadlineAt ?: null, 'resolution_notes' => $this->resolutionNotes ?: null, 'client_rating' => $this->clientRating, 'client_comment' => $this->clientComment ?: null, 'attachments' => $this->attachments, ]; if ($this->serviceRequestId && $this->serviceRequest) { $request = $requestService->update($this->serviceRequest, $payload, auth()->user()); session()->flash('success', 'Заявка успешно обновлена.'); return redirect()->route('requests.show', $request); } $request = $requestService->create($payload, auth()->user()); session()->flash('success', 'Заявка успешно создана.'); return redirect()->route('requests.show', $request); } public function render() { $clientId = $this->clientId !== '' ? (int) $this->clientId : null; return view('livewire.requests.request-form-page', [ 'types' => RequestType::query()->orderBy('name')->get(), 'priorities' => Priority::query()->orderBy('level')->get(), 'statuses' => Status::query()->orderBy('sort_order')->get(), 'clients' => User::query()->where('role', 'client')->orderBy('name')->get(), 'technicians' => User::query()->where('role', 'technician')->orderBy('name')->get(), 'equipmentItems' => Equipment::query() ->when($clientId, fn ($q) => $q->where('client_id', $clientId)) ->orderBy('name') ->get(), ])->title($this->serviceRequestId ? 'Редактирование заявки' : 'Создание заявки'); } }