<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Dispositivo;
use App\DataTables\StampanteDataTable;
use App\DataTables\StampanteDataTableEditor;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;


class StampanteController extends Controller
{
    public static $permission_group = "Stampanti";
    public static $permissions = [
        'view-stampante' => 'Vedi',
        'create-stampante' => 'Crea',
        'edit-stampante' => 'Modifica',
        'delete-stampante' => 'Elimina',
    ];
    
    public static function middleware(): array
    {
        return [
            new Middleware('permission:view-stampante', only: ['index']),
            new Middleware('permission:create-stampante|edit-stampante|delete-stampante', only: ['store', 'update', 'destroy']),
        ];
    }
    
    public function index(StampanteDataTable $dataTable)
    {
        $dataTable->attivita_id = Session::get('attivita_attuale');
        return $dataTable->render('stampante.index');
    }
    public function store(StampanteDataTableEditor $editor)
    {
        $request = request();
        $input = $request->all();
        if($request->has('action')){
            switch($input['action']){
                case 'create':
                    if(!Auth::user()->can('create-stampante')) return;
                    break;
                case 'edit':
                    if(!Auth::user()->can('edit-stampante')) return;
                    break;
                case 'remove':
                    if(!Auth::user()->can('delete-stampante')) return;
                    break;
            }
        }
        return $editor->process($request);
    }
}
