<?php

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

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

    // public function calculateBobotKriteria()
    // {
    //     $criterias = Criteria::all();

    //     $totalKriteria = count($criterias);

    //     $bobotKriteria = [];

    //     foreach ($criterias as $key => $criteria) {
    //         $sum = 0;
    //         // Iterasi untuk setiap nilai prioritas pada kriteria
    //         for ($i = $key; $i < $totalKriteria; $i++) {
    //             // Hitung nilai SMARTER
    //             $sum += 1 / ($i + 1);
    //         }
    //         // Hitung bobot kriteria menggunakan rumus SMARTER ROC
    //         $bobot = $sum / $totalKriteria;

    //         $bobotKriteria[$criteria->nama] = $bobot;

    //         $criteria->update(['bobot' => $bobot]);
    //         $criteria->save();
    // }

    //     return redirect()->route('kriteria.index')->with('success', 'Bobot kriteria berhasil dihitung dan disimpan.');
    // }

    public function index(Request $request)
    {
        if ($request->has('search')) {
            $criterias = Criteria::where('nama', 'LIKE', '%'.$request->search.'%')->paginate(5);
        } else {
            $criterias = Criteria::paginate(5);
        }

        return view('kriteria.kriteria', compact('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('nama', 'prioritas', 'bobot'), [
            'nama' => 'required|string',
            'prioritas' => 'required|integer',
            'bobot' => 'nullable|numeric',
        ]);

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

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

        return redirect()->route('kriteria.index')->with('success', 'Data 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)
    {
        $kriteria = Criteria::findOrFail($id);
        return view('kriteria.edit', compact('kriteria'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        // $kriteria = Criteria::findorfail($id);
        // $kriteria->update($request->all());

        // return redirect('kriteria')->with('success', 'Data Kriteria Berhasil Diperbarui!');

        $validatedData = $request->validate([
            'nama' => 'required|string',
            'prioritas' => 'required|integer',
            'bobot' => 'nullable|numeric',
        ]);

        $kriteria = Criteria::findOrFail($id);
        $kriteria->update($validatedData);

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

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

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

}
