Files
eatvio-infra/app/Livewire/Plan/Edit.php
2023-12-28 17:07:09 +03:00

208 lines
6.9 KiB
PHP

<?php
namespace App\Livewire\Plan;
use App\Models\Plan;
use Livewire\Component;
use Livewire\WithFileUploads;
class Edit extends Component
{
use WithFileUploads;
public Plan $plan;
public $name = '';
public $image;
public $duration = '7';
public $state = 'draft';
public $type = ['fit' => false, 'slim' => false, 'behavior' => false, 'wellness' => false];
public $details = '';
public $packets_count = 1;
public $packet1_options = [];
public $packet2_options = [];
public $packet3_options = [];
public $packet1_options_input = [];
public $packet2_options_input = [];
public $packet3_options_input = [];
public $input_items_1 = '';
public $input_items_2 = '';
public $input_items_3 = '';
public $isVisibleDuration = false;
public $isVisibleState = false;
public function mount(Plan $plan)
{
$this->plan = $plan;
$this->name = $plan->name;
$this->image = $plan->image;
$this->duration = $plan->duration;
$this->state = $plan->state;
$this->type = $plan->type;
$this->details = $plan->details;
$this->packets_count = $plan->packets_count;
$this->packet1_options_input['short'] = $plan->packet1_options['short'];
$this->packet1_options_input['price'] = $plan->packet1_options['price'];
$this->packet1_options_input['description'] = $plan->packet1_options['description'];
// $this->input_items_1 = $plan->packet1_options['items'];
$this->packet1_options['short'] = $plan->packet1_options['short'];
$this->packet1_options['price'] = $plan->packet1_options['price'];
$this->packet1_options['description'] = $plan->packet1_options['description'];
$this->packet1_options['items'] = $plan->packet1_options['items'];
if ($plan->packets_count === 3) {
$this->packet2_options_input['short'] = $plan->packet2_options['short'];
$this->packet2_options_input['price'] = $plan->packet2_options['price'];
$this->packet2_options_input['description'] = $plan->packet2_options['description'];
// $this->input_items_2 = $plan->packet1_options['items'];
$this->packet2_options['short'] = $plan->packet2_options['short'];
$this->packet2_options['price'] = $plan->packet2_options['price'];
$this->packet2_options['description'] = $plan->packet2_options['description'];
$this->packet2_options['items'] = $plan->packet2_options['items'];
$this->packet3_options_input['short'] = $plan->packet2_options['short'];
$this->packet3_options_input['price'] = $plan->packet2_options['price'];
$this->packet3_options_input['description'] = $plan->packet2_options['description'];
// $this->input_items_3 = $plan->packet1_options['items'];
$this->packet3_options['short'] = $plan->packet2_options['short'];
$this->packet3_options['price'] = $plan->packet2_options['price'];
$this->packet3_options['description'] = $plan->packet2_options['description'];
$this->packet3_options['items'] = $plan->packet3_options['items'];
}
// likes_count
// commentsCount
}
public function showListDuration()
{
$this->isVisibleDuration = ! $this->isVisibleDuration;
}
public function showListState()
{
$this->isVisibleState = ! $this->isVisibleState;
}
public function toggleDuration($duration)
{
$this->duration = $duration;
$this->isVisibleDuration = false;
}
public function toggleState($state)
{
$this->state = $state;
$this->isVisibleState = false;
}
public function addPacketOption($options_type, $options_type_input)
{
$this->$options_type['short'] = $this->$options_type_input['short'];
$this->$options_type['price'] = $this->$options_type_input['price'];
$this->$options_type['description'] = $this->$options_type_input['description'];
$this->$options_type['items'][] = $this->input_items_1;
$this->$options_type['items'][] = $this->input_items_2;
$this->$options_type['items'][] = $this->input_items_3;
// delete empty value in items
$this->$options_type['items'] = array_filter($this->$options_type['items'], fn ($item) => ! empty($item));
$this->reset('input_items_1');
$this->reset('input_items_2');
$this->reset('input_items_3');
}
public function publish()
{
$this->validate();
$this->plan->name = $this->name;
$this->plan->image = ! is_string($this->image) ? $this->image->store('plans') : $this->image;
$this->plan->duration = $this->duration;
$this->plan->state = $this->state;
$this->plan->details = $this->details;
$this->plan->packets_count = $this->packets_count;
$this->plan->type = $this->type;
$this->plan->packet1_options = $this->packet1_options;
$this->plan->packet2_options = $this->packet2_options;
$this->plan->packet3_options = $this->packet3_options;
$this->plan->update();
// $user = Auth::user();
// $user->author_of_plans()->attach($plan->id);
$this->redirectRoute('plan.show', $this->plan);
}
public function addToDrafts()
{
$this->state = 'draft';
$this->publish();
}
public function showPreview()
{
$this->state = 'unactive';
$this->publish();
}
public function render()
{
return view('livewire.plan.edit');
}
protected function rules()
{
$rules = [
'name' => 'required|min:3',
// 'image' => 'image|max:2048',
'duration' => 'required|numeric|min:7',
'state' => 'required|in:draft,active,unactive',
'type' => ['required', 'array:slim,fit,wellness,behavior', 'min:1'],
'details' => 'required|min:3',
'packets_count' => 'required|in:1,3',
'packet1_options.short' => 'required|string|min:3',
'packet1_options.price' => 'required|numeric|min:1',
'packet1_options.description' => 'required|string|min:3',
'packet1_options.items' => 'required|array|min:1',
];
if ($this->packets_count === 3) {
$rules['packet2_options.short'] = 'required|string|min:3';
$rules['packet2_options.price'] = 'required|numeric|min:1';
$rules['packet2_options.description'] = 'required|string|min:3';
$rules['packet2_options.items'] = 'required|array|min:1';
$rules['packet3_options.short'] = 'required|string|min:3';
$rules['packet3_options.price'] = 'required|numeric|min:1';
$rules['packet3_options.description'] = 'required|string|min:3';
$rules['packet3_options.items'] = 'required|array|min:1';
}
return $rules;
}
}