<?php

namespace App\Http\Controllers;

use App\Feedbacks;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;


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

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

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $feedback = Feedbacks::query()->orderBy('created_at', 'desc')->paginate($request->input("limit", 20));

        return Response::detect("feedbacks.index", [ "feedback" => $feedback ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return Response::detect("feedbacks.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $requestBody = $request->validate([
            "message" => "required",
            "suggestion_form" => "required|max:255"
        ]);

        $feedbacks = new Feedbacks($requestBody);
        $feedbacks->save();

        return redirect()->route("root.index");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        return Response::detect("feedback.show", [ "feedback" => $id]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $feedback = Feedbacks::find($id);

        return Response::detect("feedbacks.edit", [
            "feedback" => $feedback
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param int $id
     * @return void
     */
    public function update(Request $request, $id)
    {
        $data = $request->validate([
            "time" => "required"
        ]);

        $feedback = Feedbacks::find($id);

        $feedback->update($data);

        $feedback->save();

        return Response::detect("feedbacks.edit", [
            "feedback" => $feedback
        ]);
    }

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

        return redirect()->route("feedbacks.index");
    }



    public function search(Request $request){
        if($request->ajax()){
            $output = "<tr>".
                "<th style='width: 6em;'>Dato</th>".
                "<th>Feedback Besked</th>".
                "<th style='width: 5em;'>Ris el. Ros</th>".
                "<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/trashcan.svg\" alt=\"Delete\"></th>".
            "</tr>";

            //$value = Feedbacks::query('feedbacks')->select('suggestion_form ')->dump();
            //$value = DB::table('feedbacks')->select('suggestion_form ')->dump();
            if($request->option === "Ris" || $request->option === "Ros")
                $feedbacks = Feedbacks::query()->orderBy('created_at', 'desc')->where('suggestion_form', '=', $request->option)
                    ->get();
            else
                $feedbacks = Feedbacks::all();

            if(count($feedbacks) !== 0){
                foreach ($feedbacks as $key => $feedback){
                    $output.='<tr>'.
                        '<td>' . date('d-m-Y', strtotime($feedback->created_at)) . '</td>'.
                        '<td>' . $feedback->message . '</td>'.
                        '<td>' . $feedback->suggestion_form . '</td>'.
                        '<td><form method="post" action="' .route("feedbacks.destroy", [ "feedback" => $feedback ]). '" 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);
        }
    }


}
