<?php

namespace App\Http\Controllers;

use App\Models\Tavolo;
use App\DataTables\TavoloDataTable;
use App\DataTables\TavoloDataTableEditor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class TavoloController extends Controller
{
    public static $permission_group = "Tavolo";
    public static $permissions = [
        'view-tavolo' => 'Vedi',
        'create-tavolo' => 'Crea',
        'edit-tavolo' => 'Modifica',
        'delete-tavolo' => 'Elimina',
    ];
    
    public static function middleware(): array
    {
        return [
            new Middleware('permission:view-tavolo', only: ['index']),
            new Middleware('permission:create-tavolo|edit-tavolo|delete-tavolo', only: ['store', 'update', 'destroy']),
        ];
    }

    public function index(TavoloDataTable $dataTable)
    {
        $dataTable->attivita_id = session()->get('attivita_attuale');
        return $dataTable->render('tavolo.index');
    }
    public function store(TavoloDataTableEditor $editor)
    {
        $request = request();
        $input = $request->all();
        if($request->has('action')){
            switch($input['action']){
                case 'create':
                    if(!Auth::user()->can('create-tavolo')) return;
                    break;
                case 'edit':
                    if(!Auth::user()->can('edit-tavolo')) return;
                    break;
                case 'delete':
                    if(!Auth::user()->can('delete-tavolo')) return;
                    break;
            }
        }
        return $editor->process($request);
    }
}
