<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\PayFrequency;

class PayfrequencyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $payFrequency = PayFrequency::all();

        return view('Config.PayfrequencyConfig.index', compact('payFrequency'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'payfrequency'=>'required',
            'shortcode'   =>'required',
            
          ]);
          $payFrequency = new PayFrequency([
            'pay_frequency' => $request->get('payfrequency'),
            'short_code'=> $request->get('shortcode'),
            'description'=> $request->get('payfrequencyDescription')
          ]);
          $payFrequency->save();
          return redirect('/payfrequency')->with('success', 'Pay Frequency has been added');
    }

    /**
     * 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)
    {
        $payFrequency = PayFrequency::find($id);

        return view('Config.PayfrequencyConfig.edit', compact('payFrequency'));
    }

    /**
     * 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([
            'payfrequency'=>'required',
            'shortcode'   => 'required',
          ]);
    
          $payFrequency = PayFrequency::find($id);
          $payFrequency->pay_frequency = $request->get('payfrequency');
          $payFrequency->short_code = $request->get('shortcode');
          $payFrequency->description = $request->get('payfrequencyDescription');
          $payFrequency->save();
    
          return redirect('/payfrequency')->with('success', 'Pay Frequency has been added');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $payFrequency = PayFrequency::find($id);
        $payFrequency->delete();
   
        return redirect('/payfrequency')->with('success', 'Pay Frequency has been deleted Successfully');
    }
}
