<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Evento;
use App\DataTables\EventoDataTable;
use App\DataTables\EventoDataTableEditor;
use Illuminate\Support\Facades\Auth;
use App\DataTables\EventoFormDataTable;


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

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

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

    public function show(Request $request)
    {
      // dd($request->all());
        $evento = Evento::find($request->get('evento_id'));
        return view('evento.show', [
          'evento' => $evento , 
          'galleriaItems' => is_array(json_decode($evento->galleria, true)) ? json_decode($evento->galleria, true) : []]);
    }

    public function gallery_edit(Request $request)
    {
      $evento = Evento::find($request->get('evento_id'));

      return view('evento.gallery.edit', [
          'evento' => $evento,
          'galleria' => json_decode($evento->galleria, true),
      ]);
    }

    public function gallery_upload(Request $request)
    {
      $evento = Evento::find($request->get('evento_id'));
      $galleria = json_decode($evento->galleria);
      if($galleria == null) $galleria = [];
      $nome = $request->file('file')->getClientOriginalName();
      $galleria[] = [
        'nome' => $nome,
        'path' => $request->file('file')->store( $evento->id . '/gallery', 'eventi')
      ];
      $evento->galleria = json_encode($galleria);
      $evento->save();
      return redirect()->route('evento.gallery.edit', ['evento_id' => $evento->id])->with('success', 'Foto aggiunta con successo');
    }

    public function edit(Request $request , EventoFormDataTable $dataTable)
    {
      $evento = Evento::find($request->get('evento_id'));
      $dataTable->evento_id = $evento->id;
      return $dataTable->render('evento.edit', compact('evento'));
    }

}
