forked from eatvio/eatvio-infra
161 lines
4.5 KiB
PHP
161 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Plan;
|
|
|
|
use App\Models\Plan;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
/* use Tonysm\RichTextLaravel\Livewire\WithRichTexts; */
|
|
use Livewire\WithFileUploads;
|
|
|
|
class Create extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
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 render()
|
|
{
|
|
return view('livewire.plan.create');
|
|
}
|
|
|
|
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();
|
|
|
|
$plan = new Plan();
|
|
|
|
$plan->name = $this->name;
|
|
$plan->image = $this->image->store('plans');
|
|
$plan->duration = $this->duration;
|
|
$plan->state = $this->state;
|
|
$plan->details = $this->details;
|
|
$plan->packets_count = $this->packets_count;
|
|
$plan->type = $this->type;
|
|
$plan->packet1_options = $this->packet1_options;
|
|
$plan->packet2_options = $this->packet2_options;
|
|
$plan->packet3_options = $this->packet3_options;
|
|
|
|
$plan->saveOrFail();
|
|
|
|
$user = Auth::user();
|
|
$user->author_of_plans()->attach($plan->id);
|
|
|
|
$this->redirectRoute('plan.show', $plan);
|
|
}
|
|
|
|
public function addToDrafts()
|
|
{
|
|
$this->state = 'draft';
|
|
$this->publish();
|
|
}
|
|
|
|
public function showPreview()
|
|
{
|
|
$this->state = 'unactive';
|
|
$this->publish();
|
|
}
|
|
|
|
protected function rules()
|
|
{
|
|
$rules = [
|
|
'name' => 'required|min:3',
|
|
'image' => 'required|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;
|
|
}
|
|
}
|