<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\karyawan;

use Illuminate\Support\Facades\Session;

class KaryawanController extends Controller
{
    public function __construct()
    {

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $karyawans= karyawan::all();
        return view('karyawans.tampilkaryawan', compact('karyawans'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('karyawans.tambahkaryawan');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'nama' => 'required',
            'pekerjaan' => 'required',
            'notlp' => 'required',
        ]);

        $karyawans = new karyawan();
        $karyawans->nama = $request->nama;
        $karyawans->pekerjaan = $request->pekerjaan;
        $karyawans->notlp = $request->notlp;
        $karyawans->save();
        if($karyawans->save()){
            Session::flash('pesan_sukses','Berhasil menambahkan data');
        }
        else{
            Session::flash('pesan_gagal','Gagal menambahkan data');
        }
        return redirect()->route('karyawan.index');
    }

    /**
     * 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)
    {
        $karyawans = karyawan::findOrFail($id);
        return view('karyawans.editkaryawan', compact('karyawans'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'nama' => 'required',
            'pekerjaan' => 'required',
            'notlp' => 'required',
        ]);

        $karyawans = karyawan::findOrFail($id);
        $karyawans->nama = $request->pekerjaan;
        $karyawans->pekerjaan = $request->nama;
        $karyawans->notlp = $request->notlp;
        $karyawans->save();
        if($karyawans->save()){
            Session::flash('pesan_sukses','Berhasil menyimpan data');
        }
        else{
            Session::flash('pesan_gagal','Gagal menyimpan data');
        }
        return redirect()->route('karyawan.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $karyawans = karyawan::findOrFail($id);
        $karyawans->delete();
        return redirect()->route('karyawan.index')->with('pesan_sukses','Berhasil menghapus data');
    }
}
