<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Validator;
use App\Comment;
use Str;
use Auth;

class CommentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $result = Auth::check();

        if($result){
            $comments = Comment::latest()->get();
            return view('backend.comments',compact(['comments']));
        }
        else{
            return redirect('/');
        }
        
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
        //validating data
        $rules = [
            'name' => 'required',
            'email' => 'required',
            'subject' => 'required',
            'message' => 'required',
        ];

        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['errors'=>"Make sure that you have filled all the fields."]);
        }

        else{
            //getting the data
            $data_save = [
                'full_name' => $request->name,
                'email' => $request->email,
                'subject' => $request->subject,
                'message' => $request->message,
                'slug' => Str::slug($request->email),
            ];

            //creating data now
            $data = Comment::create($data_save);

            if($data){
                return response()->json(['success'=>"Message Received Successfully."]);
            }
            else{
                return response()->json(['errors'=>"Unable to Submit the message."]);
            }
        }

    }

    /**
     * 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)
    {
        //
        $data = Comment::findOrFail($id);
        return response()->json(['result'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $data_delete = Comment::findOrFail($id);

        $data = $data_delete->delete();

        if($data){
            return response()->json(['success'=>"Comment has been Deleted"]);
        }
        else{
            return response()->json(['errors'=>"Unable to Delete Comment"]);
        }
    }

    public function previewed(Request $request){
        $id = $request->comment;
        
        Comment::whereId($id)->update(['status'=>'checked']);

        return response()->json(['success'=>"Checked"]);
    }
}
