<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Categoriacontabile;
use App\DataTables\CategoriacontabileDataTable;
use App\DataTables\CategoriacontabileDataTableEditor;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

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