<?php

namespace App\Http\Controllers;

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

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

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

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

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

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

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

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