<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\CategoriePost;
use App\Tag;
use App\Post;
use Toastr;

class BlogController extends Controller
{

   public function blog(Request $request)
   {
      $posts = Post::latest()->approved()->published()->paginate(6);
      $this->vider_session($request);

      return view('front.blog.index', compact('posts'));
   }




   public function categorieBlog(Request $request, $categorie_id, $slug)
   {
      $posts = CategoriePost::findOrFail($categorie_id)->posts()->paginate(4);
      $this->vider_session($request);


      return view('front.blog.index', compact('posts'));
   }

   public function postByCategory($slug)
   {
      //  $category = CategoriePost::where('slug',$slug)->first();
      //  $posts = $category->posts()->get();

       $category = CategoriePost::where('slug',$slug)->first();
       $posts = $category->posts()->approved()->published()->get();
       return view('front.blog.category',compact('category','posts'));
   }





   public function tagBlog($slug)
   {

      // $posts = Tag::findOrFail($tag_id)->posts()->paginate(4);
      // $this->vider_session($request);


      // return view('front.blog.index', compact('posts'));
      $tag = Tag::where('slug',$slug)->first();
      $posts = $tag->posts()->approved()->published()->get();
      return view('front.blog.tag',compact('tag','posts'));
   }



   public function search(Request $request)
   {
      // if ($request->keywords != "") {
      //    $posts = Post::where('title', 'like', '%' . $request->keywords . '%')
      //       ->orWhere('description', 'like', '%' . $request->keywords . '%')
      //       ->paginate(100);
      //    $request->session()->put('search', $request->keywords);

      //    return view('front.blog.index', compact('posts'));
      // } else {
      //    return redirect()->route('blog');
      // }
          $query = $request->input('query');
          $posts = Post::where('title','LIKE',"%$query%")->approved()->published()->get();
          return view('front.blog.search',compact('posts','query'));
   }


   public function vider_session($request)
   {
      $request->session()->put('search', '');
   }




   public function showBlog($blog_id, $slug)
   {
      $post = Post::findOrFail($blog_id);
      return view('front.blog.single', compact('post'));
   }





   public function NextPost($blog_id)
   {

      // get next post id
      $id_next = Post::where('id', '>', $blog_id)->min('id');
      if ($id_next == null) {
         return redirect()
            ->route('showBlog', [$blog_id, Post::findOrFail($blog_id)->slug]);
      }

      $post = Post::findOrFail($id_next);
      return view('front.blog.single', compact('post'));
   }



   public function PrevPost($blog_id)
   {

      // get previous post id
      $id_previous = Post::where('id', '<', $blog_id)->max('id');
      if ($id_previous == null) {
         return redirect()
            ->route('showBlog', [$blog_id, Post::findOrFail($blog_id)->slug]);
      }

      $post =  Post::findOrFail($id_previous);
      return view('front.blog.single', compact('post'));
   }
}
