<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

Route::get('/', function () {
    return view('welcome');
});
Route::get('/uts', function () {
    return view('uts.index');
});
Route::post('/proses', function () {
    return view('uts.proses');
});
Route::get('/uas', function () {
    return view('uas.index');
});

Route::get('/create', function () {
    return view('uas.create');
});

Route::get('/data', function () {
    return view('uas.data');
});

Route::post('/simpan', function (Request $request) {

    DB::table('pendaftaran')->insert([
        'nama' => $request->nama,
        'tempat_lahir' => $request->tempat_lahir,
        'tanggal_lahir' => $request->tanggal_lahir,
    ]);

    return redirect('/uas');
});

Route::get('/edit', function () {
    $id = request('id');

    $d = DB::table('pendaftaran')
        ->where('id', $id)
        ->first();
 
        if (!$d) {
            abort(404);
        }
    return view ('uas.edit', compact('d'));
});

Route::post('/update', function (Request $request) {

    DB::table('pendaftaran')
        ->where('id', $request->id)
        ->update([
            'nama' => $request->nama,
            'tempat_lahir' => $request->tempat_lahir,
            'tanggal_lahir' => $request->tanggal_lahir,
        ]);

    return redirect('/uas');
});

Route::get('/hapus', function () {
    $id = request('id');

    if ($id) {
        DB::table('pendaftaran')
            ->where('id', $id)
            ->delete();
    }

    return redirect('/uas');
});