<?php

namespace App\Http\Controllers;

use App\Event;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;


class EventController extends Controller
{
    public function __construct()
    {
        $this->middleware([ "auth" ]);

        $this->middleware([ "check.auth:event.list" ])->only("index");
        $this->middleware([ "check.auth:event.show" ])->only("show");
        $this->middleware([ "check.auth:event.create" ])->only("create", "store");
        $this->middleware([ "check.auth:event.edit" ])->only("edit", "update");
        $this->middleware([ "check.auth:event.delete" ])->only("delete");
    }

    /**
     * Display a listing of the resource..
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $events = Event::query()->paginate($request->input("limit", 20));

        //returns the function with events index page and a parameter of events.
        //also Response::detect checks screen size to determine if user is on a computer or mobile.
        return Response::detect("events.index", [ "events" => $events]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        //returns "create event" blade
        return Response::detect("events.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function store(Request $request)
    {
        $requestBody = $request->validate([
            "name" => "required|unique:events|max:255",
            "description" => "required|max:255",
            "date" => "required"
        ]);

        //creates a new Event model with the given parameter
        $event = new Event($requestBody);

        $saved = $event->save();

        if(!$saved){
            return Response::detect("events.store");
        }else{
            $event = Event::query()->paginate($request->input("limit", 20));
            return Response::detect("events.index", ['events' => $event]);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param Event $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Event $id)
    {
        return Response::detect("events.show", [ "event" => $id ]);
    }

    /**
     * Show the form for editing the specified resource.-
     *
     * @param  int  $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $event = Event::find($id);
        return Response::detect("events.edit", [ "event" => $event ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();

        $event = Event::find($id);
        $event->update($data);
        $saved = $event->save();

        if(!$saved){
            return Response::detect("events.update", [ "event" => $event]);
        }else{
            $event = Event::query()->paginate($request->input("limit", 20));
            return Response::detect("events.index", ['events' => $event]);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Event $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function destroy($id)
    {
        $event = Event::find($id);
        $event->delete();
        return redirect()->route("events.index");
    }


    public function search(Request $request){
        if($request->ajax()){
            $output = "<tr>".
                "<th>Event Navn</th>".
                "<th>Event Beskrivelse</th>".
                "<th>Event Dato</th>".
                "<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/pencil.svg\" alt=\"Update\"></th>".
                "<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/trashcan.svg\" alt=\"Delete\"></th>".
                "</tr>";
            $events = DB::table('events')->where('name', 'LIKE',$request->search.'%')
                ->orWhere('date','LIKE', $request->search.'%')
                ->get();

            if(count($events) !== 0){
                foreach ($events as $key => $event){
                    $output.='<tr>'.
                        '<td>' . $event->name . '</td>'.
                        '<td>' . $event->description .'</td>'.
                        '<td>' . $event->date .'</td>'.
                        '<td><a href="'. route("events.edit", [ "event" => $event->id ]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></td>'.
                        '<td><form method="post" action="' .route("events.destroy", [ "event" => $event->id ]). '" class="w-100 nostyle">'.
                        csrf_field().
                        method_field("delete").

                        '<button class="w-100 nostyle" onclick="return confirm(\'Are you sure you want to delete?\');" type="submit"><img class="w-100 cursor-pointer" src="'. asset('/images/icons/trashcan-dark.svg') . '" alt="Delete"></button>'.
                        '</form>'.
                        '</td>'.
                        '</tr>';
                }
            }
            return Response($output);
        }
    }



}