148 lines
3.6 KiB
PHP
148 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Gejala;
|
|
use Illuminate\Http\Request;
|
|
use RealRashid\SweetAlert\Facades\Alert;
|
|
|
|
class AdminGejalaController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$data = [
|
|
'title' => 'Manajemen Gejala',
|
|
'gejala' => Gejala::get(),
|
|
'content' => 'admin/gejala/index'
|
|
];
|
|
|
|
return view('admin.layouts.wrapper', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$latestGejala = Gejala::orderBy('id', 'desc')->first();
|
|
$newId = ($latestGejala ? intval(substr($latestGejala->kode_gejala, 1)) : 0) + 1;
|
|
$newKodeGejala = "G" . $newId;
|
|
|
|
$data = [
|
|
'title' => 'Tambah Gejala',
|
|
'newKodeGejala' => $newKodeGejala, // Mengirim kode gejala baru ke view
|
|
'content' => 'admin/gejala/create'
|
|
];
|
|
|
|
return view('admin.layouts.wrapper', $data);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'nilai_cf' => 'required|in:0.2,0.4,0.6,0.8,1',
|
|
]);
|
|
|
|
$latestGejala = Gejala::orderBy('id', 'desc')->first();
|
|
|
|
$newId = ($latestGejala ? intval(substr($latestGejala->kode_gejala, 1)) : 0) + 1;
|
|
// dd($newId);
|
|
$data['kode_gejala'] = 'G' . $newId;
|
|
|
|
Gejala::create($data);
|
|
|
|
Alert::success('Sukses', 'Data Telah ditambahkan');
|
|
return redirect('/admin/gejala');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
// $gejala = Gejala::where('id', $id)->pluck('name')->get();
|
|
// dd($gejala);
|
|
|
|
$data = [
|
|
'title' => 'Edit Gejala',
|
|
'gejala' => Gejala::find($id),
|
|
'content' => 'admin/gejala/create'
|
|
];
|
|
return view('admin.layouts.wrapper', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$gejala = Gejala::find($id);
|
|
$data = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'nilai_cf' => 'required|in:0.2,0.4,0.6,0.8,1',
|
|
]);
|
|
|
|
$gejala->update($data);
|
|
|
|
Alert::success('Sukses', 'Data Telah diubah');
|
|
return redirect('/admin/gejala');
|
|
}
|
|
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$gejala = Gejala::find($id);
|
|
$gejala->delete();
|
|
|
|
Alert::success('Sukses', 'Data Telah dihapus');
|
|
return redirect('/admin/gejala');
|
|
}
|
|
|
|
public function getLatestGejala()
|
|
{
|
|
$latestGejala = Gejala::orderBy('id', 'desc')->first();
|
|
return response()->json(['kode_gejala' => $latestGejala->kode_gejala]);
|
|
}
|
|
|
|
}
|