<?php

namespace App\Http\Controllers;
use App\Models\Criteria;
use App\Models\SubCriteria;
use Illuminate\Http\Request;
use RealRashid\SweetAlert\Facades\Alert;
use Illuminate\Support\Facades\Validator;

class SubCriteriaController extends Controller
{
    /**
     * Display a listing of the resource.
     */

    public function index(Request $request)
    {
        if ($request->has('search')) {
            $subcriterias = SubCriteria::where('nama', 'LIKE', '%'.$request->search.'%')
                                        ->orWhere('deskripsi', 'LIKE', '%'.$request->search.'%')
                                        ->paginate(10);
        } else {
            $subcriterias = SubCriteria::paginate(10);
        }

        $criterias = Criteria::all();
        return view('subkriteria.subkriteria', compact('subcriterias', 'criterias'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->only('criteria_id','nama', 'deskripsi', 'prioritas','bobot'), [
            'criteria_id' => 'required',
            'nama' => 'required',
            'deskripsi' => 'required',
            'prioritas' => 'required',
            'bobot' => 'nullable|numeric',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        SubCriteria::create([
            'criteria_id' =>$request->criteria_id,
            'nama' => $request->nama,
            'deskripsi' => $request->deskripsi,
            'prioritas' => $request->prioritas,
            'bobot' => $request->bobot,
        ]);

        return redirect()->route('subkriteria.index')->with('success', 'Data Sub Kriteria Berhasil Ditambah!');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $subkriteria = SubCriteria::findOrFail($id);
        return view('subkriteria.edit', compact('subkriteria'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $validatedData = $request->validate([
            'criteria_id' => 'required',
            'nama' => 'required',
            'deskripsi' => 'required',
            'prioritas' => 'required',
            'bobot' => 'nullable|numeric',
        ]);

        $subkriteria = SubCriteria::findOrFail($id);
        $subkriteria->update($validatedData);

        return redirect()->route('subkriteria.index')->with('success', 'Data Sub Kriteria Berhasil Diperbarui!');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $subkriteria = SubCriteria::findorfail($id);
        $subkriteria->delete();

        return back()->with('info', 'Data Sub Kriteria Berhasil Dihapus!');
    }
}
