<?php
namespace App\Livewire\Maintenance;

use App\Models\Maintenance;
use SteelAnts\DataTable\Livewire\DataTableComponent;
use Illuminate\Database\Eloquent\Builder;

class DataTable extends DataTableComponent
{
    public $listeners = [
        'maintenanceAdded' => '$refresh',
        'closeModal' => '$refresh',
    ];

    public function query(): Builder
    {
        return Maintenance::query();
    }

    public function headers(): array
    {
        return [
            'name' => 'name',
			'description' => 'description',
			'schedule' => 'schedule',
        ];
    }

    public function remove($maintenance_id){
        Maintenance::find($maintenance_id)->delete();
    }

    public function actions($item)
    {
        return [
            [
                'type' => "livewire",
                'action' => "edit",
                'text' => "edit",
                'parameters' => $item['id']
            ],
            [
                'type' => "livewire",
                'action' => "remove",
                'text' => "remove",
                'parameters' => $item['id']
            ]
        ];
    }

    public function edit($maintenance_id)
    {
        $this->dispatch('openModal', 'maintenance.form', __('boilerplate::maintenances.edit'), ['model' => $maintenance_id]);
    }
}