<?php

namespace App\Http\Controllers\BackendController;  // Namespace yang benar

use App\Http\Controllers\Controller;  // Pastikan Anda mengimpor Controller dengan benar
use App\Models\Biaya;
use Illuminate\Http\Request;

class BiayaController extends Controller
{
public function index(Request $request)
{
    $query = Biaya::query();

    if ($request->filled('search')) {
        $search = $request->input('search');
        $query->where(function($q) use ($search) {
            $q->where('kategori', 'like', "%{$search}%")
              ->orWhere('jurusan', 'like', "%{$search}%")
              ->orWhere('biaya_awal', 'like', "%{$search}%")
              ->orWhere('biaya_spp', 'like', "%{$search}%")
              ->orWhere('deskripsi', 'like', "%{$search}%");
        });
    }

    $biayas = $query->paginate(10);

    return view('admin.biaya.index', compact('biayas'));
}

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

    public function store(Request $request)
    {
        $request->validate([
            'kategori' => 'required|string|max:100',
            'jurusan' => 'required|string|max:100',
            'biaya_awal' => 'required|numeric|min:0',
            'biaya_spp' => 'required|numeric|min:0',
            'deskripsi' => 'nullable|string',
        ]);
    
        Biaya::create($request->only('kategori', 'jurusan', 'biaya_awal', 'biaya_spp', 'deskripsi'));
    
        return redirect()->route('biaya.index')->with('message', 'Biaya berhasil ditambahkan.')->with('alert-type', 'success');
    }

    public function edit($id)
    {
        $biaya = Biaya::findOrFail($id);
        return view('admin.biaya.edit', compact('biaya'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'kategori' => 'required|string|max:100',
            'jurusan' => 'required|string|max:100',
            'biaya_awal' => 'required|numeric|min:0',
            'biaya_spp' => 'required|numeric|min:0',
            'deskripsi' => 'nullable|string',
        ]);
    
        $biaya = Biaya::findOrFail($id);
        $biaya->update($request->only('kategori', 'jurusan', 'biaya_awal', 'biaya_spp', 'deskripsi'));
    
        return redirect()->route('biaya.index')
            ->with('message', 'Data biaya berhasil diperbarui.')
            ->with('alert-type', 'success');
    }

    public function destroy($id)
    {
        $biaya = Biaya::findOrFail($id);
        $biaya->delete();

        return redirect()->route('biaya.index')->with('message', 'Biaya berhasil dihapus.')->with('alert-type', 'success');
    }
}
