Files
sirpremiumv2/app/Http/Livewire/Whatsapp/ShowMenusBot.php
T
2026-04-24 21:40:10 -05:00

195 lines
6.2 KiB
PHP

<?php
namespace App\Http\Livewire\Whatsapp;
use App\Models\WhatsappMenu;
use App\Models\WhatsappMenuOption;
use Livewire\Component;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class ShowMenusBot extends Component
{
use LivewireAlert;
// Lista
public string $search = '';
// Form menú
public bool $showMenuForm = false;
public ?int $editMenuId = null;
public string $menuName = '';
public string $menuTitle = '';
public string $menuMessage = '';
public ?int $menuParentId = null;
public bool $menuIsMain = false;
public bool $menuIsActive = true;
public int $menuSortOrder = 0;
// Form opción
public bool $showOptionForm = false;
public ?int $editOptionId = null;
public ?int $optionForMenuId = null;
public int $optionNumber = 1;
public string $optionTitle = '';
public string $optionAction = 'message';
public string $optionValue = '';
public int $optionSort = 0;
public bool $optionIsActive = true;
// Vista detalle
public ?int $viewMenuId = null;
protected function rules(): array
{
return [
'menuName' => 'required|string|max:255',
'menuTitle' => 'required|string|max:255',
'menuMessage' => 'required|string',
];
}
public function openMenuForm(?int $id = null): void
{
$this->resetMenuForm();
if ($id) {
$menu = WhatsappMenu::findOrFail($id);
$this->editMenuId = $id;
$this->menuName = $menu->name;
$this->menuTitle = $menu->title;
$this->menuMessage = $menu->message;
$this->menuParentId = $menu->parent_id;
$this->menuIsMain = (bool) $menu->is_main;
$this->menuIsActive = (bool) $menu->is_active;
$this->menuSortOrder = $menu->sort_order;
}
$this->showMenuForm = true;
}
public function saveMenu(): void
{
$this->validate();
$data = [
'name' => $this->menuName,
'title' => $this->menuTitle,
'message' => $this->menuMessage,
'parent_id' => $this->menuParentId ?: null,
'is_main' => $this->menuIsMain,
'is_active' => $this->menuIsActive,
'sort_order' => $this->menuSortOrder,
];
if ($this->editMenuId) {
WhatsappMenu::findOrFail($this->editMenuId)->update($data);
$this->alert('success', 'Menú actualizado correctamente.');
} else {
WhatsappMenu::create($data);
$this->alert('success', 'Menú creado correctamente.');
}
$this->resetMenuForm();
}
public function deleteMenu(int $id): void
{
WhatsappMenu::findOrFail($id)->delete();
$this->alert('success', 'Menú eliminado.');
if ($this->viewMenuId === $id) {
$this->viewMenuId = null;
}
}
public function openOptionForm(?int $optionId = null, ?int $menuId = null): void
{
$this->resetOptionForm();
$this->optionForMenuId = $menuId;
if ($optionId) {
$opt = WhatsappMenuOption::findOrFail($optionId);
$this->editOptionId = $optionId;
$this->optionForMenuId = $opt->menu_id;
$this->optionNumber = $opt->option_number;
$this->optionTitle = $opt->title;
$this->optionAction = $opt->action_type;
$this->optionValue = $opt->action_value ?? '';
$this->optionSort = $opt->sort_order;
$this->optionIsActive = (bool) $opt->is_active;
}
$this->showOptionForm = true;
}
public function saveOption(): void
{
$this->validate([
'optionTitle' => 'required|string|max:255',
'optionForMenuId' => 'required|integer',
'optionNumber' => 'required|integer|min:1',
]);
$data = [
'menu_id' => $this->optionForMenuId,
'option_number' => $this->optionNumber,
'title' => $this->optionTitle,
'action_type' => $this->optionAction,
'action_value' => $this->optionValue,
'sort_order' => $this->optionSort,
'is_active' => $this->optionIsActive,
];
if ($this->editOptionId) {
WhatsappMenuOption::findOrFail($this->editOptionId)->update($data);
$this->alert('success', 'Opción actualizada.');
} else {
WhatsappMenuOption::create($data);
$this->alert('success', 'Opción creada.');
}
$this->resetOptionForm();
}
public function deleteOption(int $id): void
{
WhatsappMenuOption::findOrFail($id)->delete();
$this->alert('success', 'Opción eliminada.');
}
public function resetMenuForm(): void
{
$this->showMenuForm = false;
$this->editMenuId = null;
$this->menuName = '';
$this->menuTitle = '';
$this->menuMessage = '';
$this->menuParentId = null;
$this->menuIsMain = false;
$this->menuIsActive = true;
$this->menuSortOrder = 0;
}
public function resetOptionForm(): void
{
$this->showOptionForm = false;
$this->editOptionId = null;
$this->optionForMenuId = null;
$this->optionNumber = 1;
$this->optionTitle = '';
$this->optionAction = 'message';
$this->optionValue = '';
$this->optionSort = 0;
$this->optionIsActive = true;
}
public function render()
{
$menus = WhatsappMenu::when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%")
->orWhere('title', 'like', "%{$this->search}%"))
->withCount('options')
->orderBy('sort_order')
->get();
$viewMenu = $this->viewMenuId ? WhatsappMenu::with('options')->find($this->viewMenuId) : null;
$allMenus = WhatsappMenu::orderBy('title')->get();
return view('livewire.whatsapp.show-menus-bot', compact('menus', 'viewMenu', 'allMenus'));
}
}