169 lines
5.6 KiB
PHP
169 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\Chat;
|
|
|
|
use App\Models\ChatMenu;
|
|
use App\Models\ChatMenuOption;
|
|
use Livewire\Component;
|
|
|
|
class ShowMenusChat extends Component
|
|
{
|
|
// ── Formulario Menú ──────────────────────────────────
|
|
public bool $showMenuForm = false;
|
|
public ?int $editMenuId = null;
|
|
public string $menuNombre = '';
|
|
public string $menuMensaje = '';
|
|
public bool $menuEsRaiz = false;
|
|
public bool $menuActivo = true;
|
|
|
|
// ── Formulario Opción ─────────────────────────────────
|
|
public bool $showOpcionForm = false;
|
|
public ?int $editOpcionId = null;
|
|
public ?int $opcionMenuId = null;
|
|
public string $opcionClave = '';
|
|
public string $opcionEtiqueta = '';
|
|
public string $opcionAccion = 'respuesta'; // ir_menu | respuesta | agente
|
|
public ?int $opcionMenuDestId = null;
|
|
public string $opcionRespuesta = '';
|
|
public int $opcionOrden = 0;
|
|
|
|
// ── CRUD Menú ─────────────────────────────────────────
|
|
|
|
public function saveMenu(): void
|
|
{
|
|
$this->validate([
|
|
'menuNombre' => 'required|string|max:80',
|
|
'menuMensaje' => 'required|string|max:500',
|
|
]);
|
|
|
|
if ($this->menuEsRaiz) {
|
|
ChatMenu::where('es_raiz', true)->update(['es_raiz' => false]);
|
|
}
|
|
|
|
$data = [
|
|
'nombre' => $this->menuNombre,
|
|
'mensaje' => $this->menuMensaje,
|
|
'es_raiz' => $this->menuEsRaiz,
|
|
'activo' => $this->menuActivo,
|
|
];
|
|
|
|
if ($this->editMenuId) {
|
|
ChatMenu::where('id', $this->editMenuId)->update($data);
|
|
} else {
|
|
ChatMenu::create($data);
|
|
}
|
|
|
|
$this->closeMenuForm();
|
|
}
|
|
|
|
public function editMenu(int $id): void
|
|
{
|
|
$menu = ChatMenu::findOrFail($id);
|
|
$this->editMenuId = $id;
|
|
$this->menuNombre = $menu->nombre;
|
|
$this->menuMensaje = $menu->mensaje;
|
|
$this->menuEsRaiz = (bool) $menu->es_raiz;
|
|
$this->menuActivo = (bool) $menu->activo;
|
|
$this->showMenuForm = true;
|
|
}
|
|
|
|
public function deleteMenu(int $id): void
|
|
{
|
|
ChatMenu::destroy($id);
|
|
}
|
|
|
|
public function closeMenuForm(): void
|
|
{
|
|
$this->showMenuForm = false;
|
|
$this->editMenuId = null;
|
|
$this->menuNombre = '';
|
|
$this->menuMensaje = '';
|
|
$this->menuEsRaiz = false;
|
|
$this->menuActivo = true;
|
|
}
|
|
|
|
// ── CRUD Opción ───────────────────────────────────────
|
|
|
|
public function saveOpcion(): void
|
|
{
|
|
$this->validate([
|
|
'opcionClave' => 'required|string|max:20',
|
|
'opcionEtiqueta' => 'required|string|max:150',
|
|
'opcionAccion' => 'required|in:ir_menu,respuesta,agente',
|
|
'opcionMenuDestId' => 'nullable|exists:chat_menus,id',
|
|
'opcionRespuesta' => 'nullable|string|max:1000',
|
|
]);
|
|
|
|
$data = [
|
|
'menu_id' => $this->opcionMenuId,
|
|
'clave' => strtolower(trim($this->opcionClave)),
|
|
'etiqueta' => $this->opcionEtiqueta,
|
|
'accion' => $this->opcionAccion,
|
|
'menu_destino_id' => $this->opcionAccion === 'ir_menu' ? $this->opcionMenuDestId : null,
|
|
'respuesta_texto' => $this->opcionAccion === 'respuesta' ? $this->opcionRespuesta : null,
|
|
'orden' => $this->opcionOrden,
|
|
];
|
|
|
|
if ($this->editOpcionId) {
|
|
ChatMenuOption::where('id', $this->editOpcionId)->update($data);
|
|
} else {
|
|
ChatMenuOption::create($data);
|
|
}
|
|
|
|
$this->closeOpcionForm();
|
|
}
|
|
|
|
public function openOpcionForm(int $menuId): void
|
|
{
|
|
$this->opcionMenuId = $menuId;
|
|
$this->editOpcionId = null;
|
|
$this->opcionClave = '';
|
|
$this->opcionEtiqueta = '';
|
|
$this->opcionAccion = 'respuesta';
|
|
$this->opcionMenuDestId = null;
|
|
$this->opcionRespuesta = '';
|
|
$this->opcionOrden = 0;
|
|
$this->showOpcionForm = true;
|
|
}
|
|
|
|
public function editOpcion(int $id): void
|
|
{
|
|
$op = ChatMenuOption::findOrFail($id);
|
|
$this->editOpcionId = $id;
|
|
$this->opcionMenuId = $op->menu_id;
|
|
$this->opcionClave = $op->clave;
|
|
$this->opcionEtiqueta = $op->etiqueta;
|
|
$this->opcionAccion = $op->accion;
|
|
$this->opcionMenuDestId = $op->menu_destino_id;
|
|
$this->opcionRespuesta = $op->respuesta_texto ?? '';
|
|
$this->opcionOrden = $op->orden;
|
|
$this->showOpcionForm = true;
|
|
}
|
|
|
|
public function deleteOpcion(int $id): void
|
|
{
|
|
ChatMenuOption::destroy($id);
|
|
}
|
|
|
|
public function closeOpcionForm(): void
|
|
{
|
|
$this->showOpcionForm = false;
|
|
$this->editOpcionId = null;
|
|
$this->opcionMenuId = null;
|
|
$this->opcionClave = '';
|
|
$this->opcionEtiqueta = '';
|
|
$this->opcionAccion = 'respuesta';
|
|
$this->opcionMenuDestId = null;
|
|
$this->opcionRespuesta = '';
|
|
$this->opcionOrden = 0;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$menus = ChatMenu::with('options.menuDestino')->orderByDesc('es_raiz')->orderBy('nombre')->get();
|
|
$todosMenus = ChatMenu::orderBy('nombre')->get();
|
|
|
|
return view('livewire.chat.show-menus-chat', compact('menus', 'todosMenus'));
|
|
}
|
|
}
|