LAR_Maintenance/app/Livewire/MaintenanceHistory/DataTable.php
2024-08-08 10:33:21 +02:00

75 lines
1.8 KiB
PHP

<?php
namespace App\Livewire\MaintenanceHistory;
use App\Models\MaintenanceHistory;
use SteelAnts\DataTable\Livewire\DataTableComponent;
use Illuminate\Database\Eloquent\Builder;
class DataTable extends DataTableComponent
{
public string $type = "planned";
public $listeners = [
'maintenanceHistoryAdded' => '$refresh',
'closeModal' => '$refresh',
];
public function query(): Builder
{
$query = MaintenanceHistory::query();
if ($this->type == "planned") {
$query->whereNull('finished_at');
} elseif ($this->type == "history") {
$query->whereNotNull('finished_at');
}
return $query;
}
public function headers(): array
{
$headers = [
'maintenance.name' => 'maintenance.name',
'start_at' => 'start_at',
];
if ($this->type == "planned") {
$headers['guestor.name'] = 'guestor';
} else {
$headers['finished_at'] = 'finished_at';
}
return $headers;
}
public function remove($maintenancehistory_id)
{
MaintenanceHistory::find($maintenancehistory_id)->delete();
}
public function renderColumnMaintenanceName($val, $row)
{
if ($this->type == "planned") {
$ret = '<a href="' . route('maintenance.planned.detail', $row['id']) . '">' . e($val) . '</a>';
} else {
$ret = '<a href="' . route('maintenance.history.detail', $row['id']) . '">' . e($val) . '</a>';
}
return $ret;
}
public function actions($item)
{
return [
[
'type' => "livewire",
'action' => "remove",
'text' => "remove",
'parameters' => $item['id']
]
];
}
}