<?php

namespace App\Http\Controllers;

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

class JobTitleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $jobtitles=DB::table('job_titles')->get();
        $paycodes=DB::table('pay_frequencies')->get();
        $rolecodes=DB::table('roles')->get();
        return view('Configurations.JobtitleConfig.index',compact(['jobtitles','paycodes','rolecodes']));
    }

    /**
     * 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([
            'job_title_code'=>'required',
            'job_title'=>'required',
            'rolecode'=>'required',
            'job_paygrade_code'=>'required',
            
        ]);
        DB::table('job_titles')
        ->insertGetId([
            'job_title_code' => $request['job_title_code'],
            'job_title' => $request['job_title'],
            'role_code'=>$request['rolecode'],
            'job_paygrade_code' => $request['job_paygrade_code'],
            'description' => $request['description'],
            'minimum_experience' => $request['minimum_experience'],
            'created_at' => DB::raw('NOW()'),
        ]);

        return redirect('/jobtitle')->with('Success','Job Title 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)
    {
        $jobtitles = DB::table('job_titles')->where('id', $id)->first();       
        $pay_codes=DB::table('pay_frequencies')->get();       
        $rolecodes=DB::table('roles')->get();
        $selectedrole= DB::table('roles')->select('role_code')->where('id',$id)->get();
        $selectedpaycode= DB::table('pay_frequencies')->select('payfrequency_code')->where('id',$id)->get();
        
        return view('Configurations.JobtitleConfig.edit',compact(['jobtitles','rolecodes','pay_codes','selectedrole','selectedpaycode']));
    }

    /**
     * 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([
            'job_title_code'=>'required',
            'job_title'=>'required',
            'rolecode'=>'required',
            'job_paygrade_code'=>'required',
        ]);

        DB::table('job_titles')
            ->where('id', $id)
            ->update([
                'job_title_code' => $request['job_title_code'],
                'job_title' => $request['job_title'],
                'role_code'=>$request['rolecode'],
                'job_paygrade_code' => $request['job_paygrade_code'],
                'description' => $request['description'],
                'minimum_experience' => $request['minimum_experience'],    
                ]);
        return redirect('/jobtitle')->with('Success','Job Title updated successfully');
    }

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