ad->toArray(); $this->form->fill($initialData); } protected function getFormSchema(): array { return [ Card::make() ->schema([ SpatieMediaLibraryFileUpload::make('images') ->helperText('След добавяне/пренареждане/изтриване на снимките винаги запазвайте промените си с бутона "Запази".') ->collection('ads') ->disk('adFiles') ->image() ->enableReordering() ->multiple() ->maxFiles(3) ->maxSize(1024 * 10) ->label('Снимки на обявата (максимум 3 бр.)'), TextInput::make('name') ->label('Заглавие на обявата') ->maxLength(255) ->required(), RichEditor::make('description') ->toolbarButtons([ 'bold', 'bulletList', 'italic', 'link', 'orderedList', 'redo', 'strike', 'undo', ]) ->label('Описание на обявата'), Grid::make(2) ->schema([ Select::make('type') ->options([ 'search' => 'Търся транспорт', 'offer' => 'Предлагам транспорт' ]) ->reactive() ->label('Вид обява') ->required() ->placeholder('Избор'), Select::make('transport_type') ->options([ 'shared' => 'Споделен транспорт', 'personal' => 'Персонален транспорт' ]) ->label('Тип транспорт') ->required() ->placeholder('Избор'), ]), Grid::make(4) ->visible(fn ($get) => $get('type') === 'search') ->schema([ TextInput::make('packet_info.width') ->label('Широчина на пратката') ->numeric() ->suffix('см.') ->required(), TextInput::make('packet_info.height') ->label('Дължина на пратката') ->suffix('см.') ->numeric() ->required(), TextInput::make('packet_info.depth') ->suffix('см.') ->label('Дълбочина на пратката') ->numeric() ->required(), TextInput::make('packet_info.weight') ->suffix('кг.') ->label('Тегло на пратката (в кг.)') ->numeric() ->required(), ]), TextInput::make('price') ->label('Ориентировъчна цена за превоза') ->numeric() ->suffix('лв.') ->nullable() ->mask( fn (TextInput\Mask $mask) => $mask ->numeric() ->positive() ->minValue(0.01) ->thousandsSeparator(',') ->decimalSeparator('.'), ) ->visible(fn ($get) => $get('type') === 'offer'), DateTimePicker::make('departure_at') ->minDate(now()) ->required() ->label('Дата на превоза') ->displayFormat('d.m.Y H:i') ->helperText('Въведете дата в завимост дали е дата на потегляне или дата на взимане на пратката.'), Grid::make(2) ->schema([ Select::make('from') ->label('Начална дестинация') ->placeholder('Избор на населено място') ->searchable() ->required() ->searchingMessage('Търсене на място...') ->searchPrompt('Търси населено място. Българска клавиатура само!') ->getSearchResultsUsing(function (string $search) { return Location::query() ->where('name', 'like', "%{$search}%") ->limit(20) ->get() ->mapWithKeys(fn ($record) => [ $record->ekatte => "{$record->t_v_m} {$record->name} ( обл. {$record->region->name} )" ]) ->toArray(); }) ->getOptionLabelUsing(function ($value) { $location = Location::query()->where('ekatte', $value)->get()->first(); return "{$location->t_v_m} {$location?->name} ( обл. {$location->region->name} )"; }), Select::make('to') ->label('Крайна дестинация') ->placeholder('Избор на населено място') ->searchable() ->required() ->searchingMessage('Търсене на място...') ->searchPrompt('Търси населено място. Българска клавиатура само!') ->getSearchResultsUsing(function (string $search) { return Location::query() ->where('name', 'like', "%{$search}%") ->limit(20) ->get() ->mapWithKeys(fn ($record) => [ $record->ekatte => "{$record->t_v_m} {$record->name} ( обл. {$record->region->name} )" ]) ->toArray(); }) ->getOptionLabelUsing(function ($value) { $location = Location::query()->where('ekatte', $value)->get()->first(); return "{$location->t_v_m} {$location?->name} ( обл. {$location->region->name} )"; }), ]), Select::make('vehicle_id') ->visible(fn ($get) => $get('type') === 'offer') ->required(fn ($get) => $get('type') === 'offer') ->options(Vehicle::where('user_id', auth()->user()->id)->pluck('name', 'id')) ->label('Изберете превозно средство, с което ще извършите превоза') ->placeholder('Избор на превозно средство') ->helperText(fn () => new HtmlString('Ако нямате добавено превозно средство, създайте го тук')), Group::make() ->visible(fn ($get) => $get('type') === 'offer') ->schema([ Placeholder::make('route_info') ->label('Информация на маршрута') ->content('Добави населени места, за да опишеш маршрута си. Първото е мястото на тръгване, а последното мястото на пристигане.'), Repeater::make('locations') ->relationship() ->label('Описване на маршрут') ->helperText('') ->minItems(1) ->defaultItems(1) ->createItemButtonLabel('+ добави спирка') ->schema([ Select::make('location_ekatte') ->label('Населено място') ->placeholder('Избор на населено място') ->searchable() ->required() ->searchingMessage('Търсене на място...') ->searchPrompt('Търси населено място. Българска клавиатура само!') ->getSearchResultsUsing(function (string $search) { return Location::query() ->where('name', 'like', "%{$search}%") ->limit(20) ->get() ->mapWithKeys(fn ($record) => [ $record->ekatte => "{$record->t_v_m} {$record->name} ( обл. {$record->region->name} )" ]) ->toArray(); }) ->getOptionLabelUsing(function ($value) { $location = Location::query()->where('ekatte', $value)->get()->first(); return "{$location->t_v_m} {$location?->name} ( обл. {$location->region->name} )"; }) ]) ]) ]) ]; } protected function getFormStatePath(): string { return 'formData'; } protected function getFormModel(): Ad { return $this->ad; } public function submit() { $this->authorize('update', $this->ad); $payload = [ 'name' => $this->formData['name'], 'description' => $this->formData['description'], 'type' => $this->formData['type'], 'user_id' => auth()->user()->id, 'departure_at' => $this->formData['departure_at'], 'transport_type' => $this->formData['transport_type'], 'from' => $this->formData['from'], 'to' => $this->formData['to'], ]; if ($this->formData['type'] === 'offer') { $payload['vehicle_id'] = $this->formData['vehicle_id']; $payload['price'] = $this->formData['price']; } if ($this->formData['type'] === 'search') { $payload['packet_info'] = [ 'width' => $this->formData['packet_info']['width'], 'height' => $this->formData['packet_info']['height'], 'depth' => $this->formData['packet_info']['depth'], 'weight' => $this->formData['packet_info']['weight'], ]; } $this->ad->update($payload); $this->form->model($this->ad)->saveRelationships(); Notification::make() ->title('Промените са запазени успешно!') ->success() ->send(); } public function render() { $this->authorize('view', $this->ad); return view('livewire.pages.ads.edit') ->extends('layouts.public') ->section('content');; } }