<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\PrintJob;
use App\DataTables\PrintJobDataTable;
use App\DataTables\PrintJobDataTableEditor;
use Illuminate\Support\Facades\Auth;

class PrintJobController extends Controller
{
    public static $permission_group = "Code di stampa";
    public static $permissions = [
        'view-print-job' => 'Vedi',
        'create-print-job' => 'Crea',
        'edit-print-job' => 'Modifica',
        'delete-print-job' => 'Elimina',
    ];
    
    public static function middleware(): array
    {
        return [
            new Middleware('permission:view-print-job', only: ['index']),
            new Middleware('permission:create-print-job|edit-print-job|delete-print-job', only: ['store', 'update', 'destroy']),
        ];
    }
    
    public function index(PrintJobDataTable $dataTable)
    {
        return $dataTable->render('print_job.index');
    }
    public function store(PrintJobDataTableEditor $editor)
    {
        $request = request();
        $input = $request->all();
        if($request->has('action')){
            switch($input['action']){
                case 'create':
                    if(!Auth::user()->can('create-print-job')) return;
                    break;
                case 'edit':
                    if(!Auth::user()->can('edit-print-job')) return;
                    break;
                case 'remove':
                    if(!Auth::user()->can('delete-print-job')) return;
                    break;
            }
        }
        return $editor->process($request);
    }
    public function show(Request $request)
    {
        $print_job = PrintJob::find($request->get('print_job_id'));
        return view('print_job.show', compact('print_job'));
    }
}
