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

114 lines
3.5 KiB
PHP

<?php
namespace App\Http\Livewire\Whatsapp;
use App\Models\WhatsappMessageTemplate;
use Livewire\Component;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class ShowPlantillas extends Component
{
use LivewireAlert;
public string $search = '';
public string $filterStatus = '';
// Formulario
public bool $showForm = false;
public ?int $editId = null;
public string $name = '';
public string $templateName = '';
public string $languageCode = 'es';
public string $status = 'pending';
public string $bodyText = '';
public string $headerType = 'none';
public string $headerText = '';
public string $footerText = '';
protected function rules(): array
{
return [
'name' => 'required|string|max:255',
'templateName' => 'required|string|max:255|regex:/^[a-z0-9_]+$/',
'languageCode' => 'required|string|max:10',
'bodyText' => 'required|string',
];
}
public function openForm(?int $id = null): void
{
$this->resetForm();
if ($id) {
$tpl = WhatsappMessageTemplate::findOrFail($id);
$this->editId = $id;
$this->name = $tpl->name;
$this->templateName = $tpl->template_name;
$this->languageCode = $tpl->language_code;
$this->status = $tpl->status;
$this->bodyText = $tpl->body_text ?? '';
$this->headerType = $tpl->header_type;
$this->headerText = $tpl->header_text ?? '';
$this->footerText = $tpl->footer_text ?? '';
}
$this->showForm = true;
}
public function save(): void
{
$this->validate();
$data = [
'name' => $this->name,
'template_name' => $this->templateName,
'language_code' => $this->languageCode,
'status' => $this->status,
'body_text' => $this->bodyText,
'header_type' => $this->headerType,
'header_text' => $this->headerText,
'footer_text' => $this->footerText,
];
if ($this->editId) {
WhatsappMessageTemplate::findOrFail($this->editId)->update($data);
$this->alert('success', 'Plantilla actualizada.');
} else {
WhatsappMessageTemplate::create($data);
$this->alert('success', 'Plantilla creada.');
}
$this->resetForm();
}
public function delete(int $id): void
{
WhatsappMessageTemplate::findOrFail($id)->delete();
$this->alert('success', 'Plantilla eliminada.');
}
public function resetForm(): void
{
$this->showForm = false;
$this->editId = null;
$this->name = '';
$this->templateName = '';
$this->languageCode = 'es';
$this->status = 'pending';
$this->bodyText = '';
$this->headerType = 'none';
$this->headerText = '';
$this->footerText = '';
}
public function render()
{
$templates = WhatsappMessageTemplate::when($this->search, fn($q) =>
$q->where('name', 'like', "%{$this->search}%")
->orWhere('template_name', 'like', "%{$this->search}%"))
->when($this->filterStatus, fn($q) => $q->where('status', $this->filterStatus))
->orderByDesc('updated_at')
->paginate(15);
return view('livewire.whatsapp.show-plantillas', compact('templates'));
}
}