<?php

namespace App\Http\Controllers;

use App\Designation;
use Illuminate\Http\Request;

class DesignationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this->middleware('auth'); //Ordinady
        $this->middleware('role:developer');
    }

    public function index()
    {
        $Designations = Designation::all();
        return view('Designation.index',compact('Designations'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request-> validate([
            'title'=>'required|max:255',
            'CL'=>'numeric',
            'BP'=>'numeric',
            'advance'=>'numeric',
            'narration'=>'max:255',
        ]);

        Designation::create($request->all());
        return redirect(route('Designation.index'))->with('Message','Saved Successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Designation  $designation
     * @return \Illuminate\Http\Response
     */
    public function show(Designation $designation)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Designation  $designation
     * @return \Illuminate\Http\Response
     */
    public function edit(Designation $Designation)
    {
        return view('Designation.edit',compact('Designation')); 
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Designation  $designation
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Designation $Designation)
    {
        $request-> validate([
            'title'=>'required|max:255',
            'CL'=>'numeric',
            'BP'=>'numeric',
            'advance'=>'numeric',
            'narration'=>'max:255',            
            
        ]);

        $Designation->update([
            'title'=>$request->title,
            'CL'=>$request->CL,
            'BP'=>$request->BP,
            'advance'=>$request->advance,
            'narration'=>$request->narration,
        ]);
        return redirect(route('Designation.index'))->with('Message','Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Designation  $designation
     * @return \Illuminate\Http\Response
     */
    public function destroy(Designation $Designation)
    {
        $Designation->delete();
        return redirect(route('Designation.index'))->with('Message','Deleted Successfully');
    }

    public static function DesignationListForCombo($Designation_id)
    {
                    $Designations=Designation::all();
                    //$Categories=['Items'=>$Items,'Selected'=>$category_id];
                    return view('Designation.combo',compact('Designations','Designation_id'));
    }
}
