<?php

namespace App\Http\Controllers;

use App\Duration;
use Illuminate\Http\Request;

class DurationController 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()
    {
        $Durations = Duration::all();
        return view('Duration.index',compact('Durations'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('Duration.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',
            'amount'=>'max:255',
            'narration'=>'max:255',
        ]);
         Duration::create($request->all());
        //dd($request);
        
        return redirect(route('Duration.index'))->with('Message','Saved Successfully');
    }

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

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

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

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

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