<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Ekskul;

class EkskulController extends Controller
{
    public function index()
    {
        $ekskuls = Ekskul::orderBy('id', 'desc')->get();
        return view('admin.ekskul.index', compact('ekskuls'));
    }


    public function create()
    {
        return view('admin.ekskul.create');
    }

    public function store(Request $request)
    {
        try {
            $request->validate([
                'nama' => 'required',
                'foto' => 'required|image|max:5120'
            ]);

            $path = $request->file('foto')->store('ekskul', 'public');

            Ekskul::create([
                'nama' => $request->nama,
                'foto' => $path
            ]);

            return redirect()->route('admin.ekskul.index')
                ->with('success', 'Ekstrakurikuler berhasil ditambahkan!');
        } catch (\Throwable $e) {
            dd($e->getMessage());
        }
    }


    public function destroy($id)
    {
        $data = Ekskul::findOrFail($id);
        $data->delete();

        return back()->with('success', 'Data berhasil dihapus');
    }
}
