gps_system/app/Livewire/CommandCenter.php
sackey 6b878bb0a0
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
Initial commit
2025-09-12 16:19:56 +00:00

166 lines
5.1 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Command;
use Livewire\Component;
use Livewire\WithPagination;
class CommandCenter extends Component
{
use WithPagination;
public $filters = [
'search' => '',
'status' => '',
'type' => '',
'device_id' => '',
'date_range' => '',
];
public $showSendCommandModal = false;
public $showDetailsModal = false;
public $selectedCommand = null;
public $commandForm = [
'device_id' => '',
'type' => '',
'custom_command' => '',
'description' => '',
'duration' => '',
'expires_at' => '',
];
protected $layout = 'layouts.app';
public function getStatsProperty()
{
$totalCommands = Command::count();
$successfulCommands = Command::where('status', 'acknowledged')->count();
$pendingCommands = Command::whereIn('status', ['pending', 'sent'])->count();
$failedCommands = Command::where('status', 'failed')->count();
return [
'total_commands' => $totalCommands,
'successful_commands' => $successfulCommands,
'pending_commands' => $pendingCommands,
'failed_commands' => $failedCommands,
];
}
public function render()
{
$commands = Command::query()
->with('user')
->when($this->filters['search'], function ($query) {
$query->where(function ($q) {
$q->where('type', 'like', '%' . $this->filters['search'] . '%')
->orWhere('device_id', 'like', '%' . $this->filters['search'] . '%')
->orWhere('description', 'like', '%' . $this->filters['search'] . '%');
});
})
->when($this->filters['status'], function ($query) {
$query->where('status', $this->filters['status']);
})
->when($this->filters['type'], function ($query) {
$query->where('type', $this->filters['type']);
})
->when($this->filters['device_id'], function ($query) {
$query->where('device_id', 'like', '%' . $this->filters['device_id'] . '%');
})
->when($this->filters['date_range'], function ($query) {
match($this->filters['date_range']) {
'today' => $query->whereDate('created_at', today()),
'week' => $query->where('created_at', '>=', now()->subWeek()),
'month' => $query->where('created_at', '>=', now()->subMonth()),
default => null,
};
})
->orderBy('created_at', 'desc')
->paginate(15);
return view('livewire.command-center', [
'commands' => $commands,
]);
}
public function sendCommand()
{
$this->validate([
'commandForm.device_id' => 'required|string',
'commandForm.type' => 'required|string',
]);
$parameters = [];
if ($this->commandForm['duration']) {
$parameters['duration'] = $this->commandForm['duration'];
}
Command::create([
'device_id' => $this->commandForm['device_id'],
'type' => $this->commandForm['type'],
'description' => $this->commandForm['description'],
'parameters' => !empty($parameters) ? $parameters : null,
'status' => 'pending',
'expires_at' => $this->commandForm['expires_at'] ? \Carbon\Carbon::parse($this->commandForm['expires_at']) : null,
'user_id' => auth()->id(),
]);
$this->closeSendCommandModal();
$this->reset('commandForm');
session()->flash('message', 'Command sent successfully.');
}
public function viewCommand($commandId)
{
$this->selectedCommand = Command::with('user')->find($commandId);
$this->showDetailsModal = true;
}
public function cancelCommand($commandId)
{
Command::find($commandId)->update(['status' => 'cancelled']);
session()->flash('message', 'Command cancelled successfully.');
}
public function retryCommand($commandId)
{
Command::find($commandId)->update(['status' => 'pending']);
session()->flash('message', 'Command retry initiated.');
}
public function closeSendCommandModal()
{
$this->showSendCommandModal = false;
$this->reset('commandForm');
}
// Handle legacy method name (backward compatibility)
public function showSendcommandModal($deviceId = null)
{
if ($deviceId) {
$this->commandForm['device_id'] = $deviceId;
}
$this->showSendCommandModal = true;
}
// Handle legacy method name (backward compatibility)
public function showCommanddetailsModal($commandId = null)
{
if ($commandId) {
$this->viewCommand($commandId);
}
}
public function closeDetailsModal()
{
$this->showDetailsModal = false;
$this->selectedCommand = null;
}
public function updatingFilters()
{
$this->resetPage();
}
}