<?php

namespace App\Http\Controllers;

use App\Models\Edukasi;
use Illuminate\Http\Request;

class EdukasiController extends Controller
{
    // =========================
    // ADMIN - LIST DATA
    // =========================
    
    public function index(Request $request)
{
    $query = Edukasi::query();

    // SEARCH
    if ($request->search) {

        $query->where('judul', 'like', '%' . $request->search . '%')
              ->orWhere('isi', 'like', '%' . $request->search . '%');

    }

    // jumlah data per halaman
    $perPage = $request->perPage ?? 5;

    // pagination
    $edukasis = $query->latest()
        ->paginate($perPage)
        ->withQueryString();

    return view('admin.edukasi.index', compact(
        'edukasis',
        'perPage'
    ));
}
    // =========================
    // ADMIN - FORM CREATE
    // =========================
    public function create()
    {
        return view('admin.edukasi.create');
    }

    // =========================
    // ADMIN - SIMPAN DATA
    // =========================
    public function store(Request $request)
    {
        // VALIDASI
        $request->validate([
            'judul' => 'required|string|max:255',
            'isi'   => 'required',
        ]);

        // SIMPAN DATA
        $data = [
            'judul' => $request->judul,
            'isi'   => $request->isi,
        ];

            $data = $request->all();

        if ($request->hasFile('gambar')) {
            $file = $request->file('gambar');
            $namaFile = time() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('uploads'), $namaFile);

            $data['gambar'] = $namaFile;
        }

        Edukasi::create($data);

        return redirect()->route('edukasi.index')
            ->with('success', 'Data edukasi berhasil ditambahkan');
    }

    // =========================
    // ADMIN - FORM EDIT
    // =========================
    public function edit($id)
    {
        $edukasi = Edukasi::findOrFail($id);
        return view('admin.edukasi.edit', compact('edukasi'));
    }

    // =========================
    // ADMIN - UPDATE DATA
    // =========================
    public function update(Request $request, $id)
    {
        // VALIDASI
        $request->validate([
            'judul' => 'required|string|max:255',
            'isi'   => 'required',
        ]);

        // UPDATE
        $edukasi = Edukasi::findOrFail($id);
        $data = [
            'judul' => $request->judul,
            'isi'   => $request->isi,
        ];

                $edukasi = Edukasi::findOrFail($id);
        $data = $request->all();

        if ($request->hasFile('gambar')) {
            $file = $request->file('gambar');
            $namaFile = time() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('uploads'), $namaFile);

            $data['gambar'] = $namaFile;
        }

        $edukasi->update($data);


        return redirect()->route('edukasi.index')
            ->with('success', 'Data edukasi berhasil diupdate');
    }

    // =========================
    // ADMIN - DELETE DATA
    // =========================
    public function destroy($id)
    {
        Edukasi::destroy($id);

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



    public function show($id)
{
    $edukasi = Edukasi::findOrFail($id);
    return view('admin.edukasi.show', compact('edukasi'));
}

    // =========================
    // USER - TAMPILKAN EDUKASI
    // =========================


public function userIndex(Request $request)
{
    $query = Edukasi::query();

    // 🔍 SEARCH
    if ($request->search) {
        $query->where('judul', 'like', '%' . $request->search . '%')
              ->orWhere('isi', 'like', '%' . $request->search . '%');
    }

    $edukasis = $query->latest()->get();

    return view('user.edukasi', compact('edukasis'));
}

    public function showUser($id)
{
    $edukasi = Edukasi::findOrFail($id);
    return view('user.edukasi_detail', compact('edukasi'));
}
}