forked from eatvio/eatvio-infra
62 lines
2.3 KiB
PHP
62 lines
2.3 KiB
PHP
<?php
|
||
|
||
namespace App\Livewire\Plan;
|
||
|
||
use App\Models\Plan;
|
||
use Livewire\Component;
|
||
use Illuminate\View\View;
|
||
|
||
class Accordion extends Component
|
||
{
|
||
public Plan $plan;
|
||
|
||
public array $accordionState = ['options1' => true, 'options2' => false, 'options3' => false];
|
||
|
||
public array $options1;
|
||
|
||
public array $options2;
|
||
|
||
public array $options3;
|
||
|
||
public function mount(Plan $plan): void
|
||
{
|
||
$this->plan = $plan;
|
||
|
||
$this->options1['items'] = $this->filter_not_empty_items($plan->packet1_options['items'] ?? []);
|
||
$this->options2['items'] = $this->filter_not_empty_items($plan->packet2_options['items'] ?? []);
|
||
$this->options3['items'] = $this->filter_not_empty_items($plan->packet3_options['items'] ?? []);
|
||
|
||
$this->options1['price'] = $plan->packet1_options['price'] ?? 0;
|
||
$this->options2['price'] = $plan->packet2_options['price'] ?? 0;
|
||
$this->options3['price'] = $plan->packet3_options['price'] ?? 0;
|
||
|
||
$this->options1['short'] = $plan->packet1_options['short'] ?? '';
|
||
$this->options2['short'] = $plan->packet2_options['short'] ?? '';
|
||
$this->options3['short'] = $plan->packet3_options['short'] ?? '';
|
||
|
||
$this->options1['description'] = $plan->packet1_options['description'] ?? '';
|
||
$this->options2['description'] = $plan->packet2_options['description'] ?? '';
|
||
$this->options3['description'] = $plan->packet3_options['description'] ?? '';
|
||
}
|
||
|
||
public function render(): View
|
||
{
|
||
return view('livewire.plan.accordion');
|
||
}
|
||
|
||
public function toggleAccordionState(string $optionName): void
|
||
{
|
||
$this->accordionState[$optionName] = ! $this->accordionState[$optionName];
|
||
|
||
// 1. Если у нас 1 true все остальные делать false(чтобы закрывать остальные вкладки у которых не true)
|
||
// a. у нас есть ключ у которого будет true
|
||
// b. проходим по всем ключам accordionState и все кроме true делаем false
|
||
array_map(fn ($key) => $key === $optionName ? $optionName : $this->accordionState[$key] = false, array_keys($this->accordionState));
|
||
}
|
||
|
||
private function filter_not_empty_items(array $items = []): array
|
||
{
|
||
return collect($items)->filter(fn ($item) => ! empty($item))->toArray();
|
||
}
|
||
}
|