<?php

namespace App\Http\Controllers;

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

class RolesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $roles=DB::table('roles')->get();
        $status_codes=DB::table('employment_status')->get();
        return view('Configurations.RolesConfig.index',compact(['roles','status_codes']));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'role'=>'required',
            'role_code'=>'required',
            
        ]);
        DB::table('roles')
        ->insertGetId([
            'role_code' => $request['role_code'],
            'role' => $request['role'],
            'employment_status_code' => $request['employmentstatus_code'],
            'description' => $request['description'],
            'created_at' => DB::raw('NOW()'),
        ]);

    return redirect('/roles')->with('Success','Role added successfully');
    }

    /**
     * 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)
    {
        $role= DB::table('roles')->where('id', $id)->first();
        $status_codes=DB::table('employment_status')->get();
        $selectedemploymentstatus_code= DB::table('employment_status')->select('employment_status_code')->where('id',$id)->get();
        return view('Configurations.RolesConfig.edit',compact(['role','status_codes','selectedemploymentstatus_code']));
    }

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

        DB::table('roles')
            ->where('id', $id)
            ->update([
                'role_code' => $request['role_code'],
                'role' => $request['role'],
                'employment_status_code' => $request['employmentstatus_code'],
                'description' => $request['description'],   
                ]);

        return redirect('/roles')->with('Success','Role updated successfully');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(DB::table('roles')->where('id',$id)->exists()){
            DB::table('roles')->where('id', $id)->delete();
            return redirect('/roles')->with('success', 'Role has been deleted Successfully');
        }else{
            return redirect('/roles')->with('error', 'Role does not exist');
 
        }       
    }
      
}
