<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Ordine;
use App\DataTables\OrdineDataTable;
use App\DataTables\OrdineDataTableEditor;
use Illuminate\Support\Facades\Auth;

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

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

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

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

    public function show(Request $request)
    {
        $ordine = Ordine::find($request->get('ordine_id'));
        return view('ordine.show', compact('ordine'));
    }

    public function show_modal(Request $request)
    {
        $ordine = Ordine::find($request->get('ordine_id'));
        return view('ordine.show_modal', compact('ordine'));
    }
}

