<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Crud;

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

            $cruds = DB::table('cruds')->get();
            
        return view('index',  ['cruds' => $cruds]);
    }
    
    public function sport()    {

            $cruds = DB::table('cruds')->where('type', 'sport')->get();
            
        return view('index',  ['cruds' => $cruds]);
    }

    public function it()    {

            $cruds = DB::table('cruds')->where('type', 'IT')->get();
            
        return view('index',  ['cruds' => $cruds]);
    }

    public function festive()    {

            $cruds = DB::table('cruds')->where('type', 'festive')->get();
            
        return view('index',  ['cruds' => $cruds]);
    }


     /*
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $crud = new Crud([
          'title' => $request->get('title'),
          'post' => $request->get('post'),
          'date' =>$request->get('date'),
          'type' =>$request->get('type'),
          'last_modifit' => auth()->user()->name 
        ]);

        $crud->save();
        return redirect('/fsirevents');
    }

    /**
     * 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)
    {
        $crud = Crud::find($id);
        
        return view('edit', compact('crud','id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $crud = Crud::find($id);
        $crud->title = $request->get('title');
        $crud->post = $request->get('post');
        $crud->date = $request->get('date');
        $crud->type = $request->get('type');
        $crud->last_modifit = auth()->user()->name;
        $crud->save();
        return redirect('/fsirevents');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $crud = Crud::find($id);
      	$crud->delete();

      	return redirect('/fsirevents');
    }
}