<?php

namespace App\Http\Controllers;

use App\Models\Bobot;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class BobotController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $numtab = 1;
        $bobot = Bobot::latest()->get();
        return view('server-side.pages.manajemen-data.bobot.data', compact(
            ['numtab', 'bobot']
        ));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $bobotId = (new Bobot())->generateId();
        return view('server-side.pages.manajemen-data.bobot.create', compact('bobotId'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'bobot' => ['required'],
            'nilai' => ['required', 'numeric'],
        ]);
        Bobot::create($request->all());
        return Redirect::route('bobot.index')->with('message', 'Berhasil menambah data bobot baru');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Bobot  $bobot
     * @return \Illuminate\Http\Response
     */
    public function show(Bobot $bobot)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Bobot  $bobot
     * @return \Illuminate\Http\Response
     */
    public function edit(Bobot $bobot)
    {
        return view('server-side.pages.manajemen-data.bobot.edit', compact('bobot'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Bobot  $bobot
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Bobot $bobot)
    {
        $request->validate([
            'bobot' => ['required'],
            'nilai' => ['required', 'numeric'],
        ]);
        Bobot::find($bobot->bobotId)->update($request->all());
        return Redirect::route('bobot.index')->with('message', 'Berhasil melakukan perubahan pada data bobot yang dipilih');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Bobot  $bobot
     * @return \Illuminate\Http\Response
     */
    public function destroy(Bobot $bobot)
    {
        $bobot->delete();
        return Redirect::route('bobot.index')->with('message', 'Berhasil menghapus data bobot yang dipilih');
    }
}
