<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\Blog;
use Session;
use Spatie\Permission\Models\Permission;

class BlogController extends Controller
{
    function __construct()
    {
         $this->middleware('permission:blog-list|blog-create|blog-edit|blog-delete', ['only' => ['index','show']]);
         $this->middleware('permission:blog-create', ['only' => ['create','store']]);
         $this->middleware('permission:blog-edit', ['only' => ['edit','update']]);
         $this->middleware('permission:blog-delete', ['only' => ['destroy']]);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $blogs = Blog::orderBy('id', 'desc')->paginate(10);
        $activeBlogCount = Blog::orderBy('id','desc')->where('status',true)->count();
        $inactiveBlogCount = Blog::orderBy('id','desc')->where('status',false)->count();

        return view('admin.blog.index',compact('blogs','activeBlogCount','inactiveBlogCount'));
    }

    public function activeList()
    {
        $allBlogCount = Blog::orderBy('id','desc')->count();
        $blogs = Blog::orderBy('id','desc')->where('status',true)->paginate(10);
        $inactiveBlogCount = Blog::orderBy('id','desc')->where('status',false)->count();

        return view('admin.blog.activeList',compact('blogs','allBlogCount','inactiveBlogCount'));
    }

    public function inActiveList()
    {
        $allBlogCount = Blog::orderBy('id','desc')->count();
        $activeBlogCount = Blog::orderBy('id','desc')->where('status',true)->count();
        $blogs = Blog::orderBy('id','desc')->where('status',false)->paginate(10);

        return view('admin.blog.inActiveList',compact('blogs','allBlogCount','activeBlogCount'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

       $request->validate([
            'title'=> 'required',
            'post'=> 'required',
            'photo'=> 'required',
        ]);

        $Blog = new Blog();
        $Blog->title = $request->title;
        $Blog->post = $request->post;
        $Blog->solid_post = strip_tags($request->post);

        if($request->hasFile('photo')) {
            $image = $request->file('photo');
            $imageName = time().'_'.$image->getClientOriginalName();
            $image->move(public_path('uploads/blog_img'), $imageName);

            $Blog->photo = $imageName;
        }

        $Blog->save();
        Toastr::success('Blog created successfull.', '', ["progressbar" => true]);
        return redirect()->route('blog.index');
    }

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


   /* public function getBlog($id){

        $allBlog = Blog::find($id);
        return response()->json([
         'allBlog' => $allBlog,
        ]);

     }
*/
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   
        $request->validate([
            'title'=> 'required',
            'post'=> 'required',
        ]);

        $Blog = Blog::where('id',$id)->first();

        $Blog->title = $request->title;
        $Blog->post = $request->post;
        $Blog->solid_post = strip_tags($request->post);

        if($request->hasFile('photo')) {
            $image = $request->file('photo');
            $imageName = time().'_'.$image->getClientOriginalName();
            $image->move(public_path('uploads/blog_img'), $imageName);


            $old = Blog::find($id);
            if (file_exists(public_path('uploads/blog_img/'.$old->photo))) {
                unlink(public_path('uploads/blog_img/'.$old->photo));
            }

            $Blog->photo = $imageName;
        }

        $Blog->save();
        Toastr::success('Blog updated successfull.', '', ["progressbar" => true]);
        return redirect()->route('blog.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $old = Blog::find($id);
        if (file_exists(public_path('uploads/blog_img/'.$old->photo))) {
            if($old->photo != ''){
                unlink(public_path('uploads/blog_img/'.$old->photo));
            }
        }

        Blog::where('id',$id)->delete();
        Toastr::success('Blog deleted successfull.', '', ["progressbar" => true]);
        return redirect(route('blog.index'));
    }

    //To active status...
    public function blogActive($id)
    {
        Blog::where('id', $id)->update(['status' => true]);
        Toastr::success('Blog activated successfull.', '', ["progressbar" => true]);
        return redirect()->back();
    }
    
    //To inactive status...
    public function blogInactive($id)
    {
        Blog::where('id', $id)->update(['status' => false]);
        Toastr::success('Blog in-activated successfull.', '', ["progressbar" => true]);
        return redirect()->back();
    }



}
