<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Allergene;
use App\DataTables\AllergeneDataTable;
use App\DataTables\AllergeneDataTableEditor;
use Illuminate\Support\Facades\Auth;

class AllergeneController extends Controller
{
    public static $permission_group = "Allergeni";
    public static $permissions = [
        'view-allergene' => 'Vedi',
        'create-allergene' => 'Crea',
        'edit-allergene' => 'Modifica',
        'delete-allergene' => 'Elimina',
    ];

    public static function middleware(): array
    {
        return [
            new Middleware('permission:view-allergene', only: ['index']),
            new Middleware('permission:create-allergene|edit-allergene|delete-allergene', only: ['store', 'update', 'destroy']),
        ];
    }

    public function index(AllergeneDataTable $dataTable){
        return $dataTable->render('allergene.index');
    }

    public function store(AllergeneDataTableEditor $editor)
    {
        $request = request();
        $input = $request->all();
    
        if($request->has('action')){
          switch($input['action']){
            case 'create':
              if(!Auth::user()->can('create-user')) return;
              break;
    
              case 'edit':
                if(!Auth::user()->can('edit-user')) return;
                break;
    
                case 'remove':
                  if(!Auth::user()->can('remove-user')) return;
                  break;
                }
              }
              return $editor->process($request);
    }
}
