up
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Whatsapp;
|
||||
|
||||
use App\Models\WhatsappScheduledMessage;
|
||||
use App\Models\WhatsappMessageTemplate;
|
||||
use App\Models\WhatsappUser;
|
||||
use Livewire\Component;
|
||||
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ShowProgramados extends Component
|
||||
{
|
||||
use LivewireAlert;
|
||||
|
||||
public string $search = '';
|
||||
public string $filterStatus = '';
|
||||
|
||||
// Formulario
|
||||
public bool $showForm = false;
|
||||
public string $phoneNumber = '';
|
||||
public string $messageType = 'text';
|
||||
public string $messageContent = '';
|
||||
public ?int $templateId = null;
|
||||
public string $scheduledDate = '';
|
||||
public string $scheduledTime = '';
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'phoneNumber' => 'required|string|max:20',
|
||||
'scheduledDate' => 'required|date',
|
||||
'scheduledTime' => 'required',
|
||||
'messageType' => 'required|in:text,template',
|
||||
'messageContent'=> 'required_if:messageType,text|nullable|string',
|
||||
'templateId' => 'required_if:messageType,template|nullable|integer',
|
||||
];
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$template = $this->templateId ? WhatsappMessageTemplate::find($this->templateId) : null;
|
||||
|
||||
WhatsappScheduledMessage::create([
|
||||
'phone_number' => preg_replace('/\D/', '', $this->phoneNumber),
|
||||
'message_type' => $this->messageType,
|
||||
'message_content' => $this->messageContent,
|
||||
'template_id' => $this->templateId,
|
||||
'template_name' => $template?->template_name,
|
||||
'template_language' => $template?->language_code,
|
||||
'scheduled_date' => $this->scheduledDate,
|
||||
'scheduled_time' => $this->scheduledTime,
|
||||
'status' => 'pending',
|
||||
'created_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
$this->alert('success', 'Mensaje programado correctamente.');
|
||||
$this->resetForm();
|
||||
}
|
||||
|
||||
public function cancel(int $id): void
|
||||
{
|
||||
WhatsappScheduledMessage::findOrFail($id)->update(['status' => 'cancelled']);
|
||||
$this->alert('info', 'Mensaje cancelado.');
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
WhatsappScheduledMessage::findOrFail($id)->delete();
|
||||
$this->alert('success', 'Registro eliminado.');
|
||||
}
|
||||
|
||||
public function resetForm(): void
|
||||
{
|
||||
$this->showForm = false;
|
||||
$this->phoneNumber = '';
|
||||
$this->messageType = 'text';
|
||||
$this->messageContent = '';
|
||||
$this->templateId = null;
|
||||
$this->scheduledDate = '';
|
||||
$this->scheduledTime = '';
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$messages = WhatsappScheduledMessage::when($this->search, fn($q) =>
|
||||
$q->where('phone_number', 'like', "%{$this->search}%")
|
||||
->orWhere('message_content', 'like', "%{$this->search}%"))
|
||||
->when($this->filterStatus, fn($q) => $q->where('status', $this->filterStatus))
|
||||
->orderByDesc('scheduled_date')
|
||||
->orderByDesc('scheduled_time')
|
||||
->paginate(20);
|
||||
|
||||
$templates = WhatsappMessageTemplate::where('status', 'approved')->get();
|
||||
|
||||
return view('livewire.whatsapp.show-programados', compact('messages', 'templates'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user