<?php

namespace App\Http\Controllers\Admin\Blog;

use App\Helper\admin\ImageUpload;
use App\Http\Controllers\Controller;
use App\Models\Blog;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class BlogController extends Controller
{
    public  function  index(){
        $blogs=Blog::orderBy('created_at', 'DESC')->get();
        return view('admin.pages.blogs.blog.index',compact('blogs'));
    }
    public  function add(){
        return view('admin.pages.blogs.blog.add');
    }
    public function save(Request $request)
    {
        $request->validate([
            'image' => 'required',
            'title' => 'required',
            'description' => 'required',
            'username' => 'required',

        ]);

        if ($request->hasFile('image')){
            $file = $request->file('image');
            $path = "upload/images/blogimages/";
            $filename = ImageUpload::saveImage($path, $file);
            $image_name = $path.$filename;
        }else{
            $image_name = '';
        }

        $blogs = new Blog();
        $blogs->title = $request->get('title');

        $blogs->blog_slug= Str::replace(' ', '-', strtolower(Str::slug($request->get('title'))));
        $blogs->image = $image_name;
        $blogs->description = $request->get('description');
        $blogs->username = $request->get('username');
        $blogs->save();


        return redirect()->route('admin::blog')->with('success','Blog Add Successfully');

    }

    public function edit($id){
        $blog=Blog::find($id);
        return view('admin.pages.blogs.blog.edit',compact('blog'));
    }


    public function update(Request $request,$id)
    {
        $request->validate([
            'title' => 'required',
            'description' => 'required',
            'username' => 'required',
        ]);

        $blogs = Blog::find($id);
        $old = $blogs->image;
        if ($request->hasFile('image')){
            $file = $request->file('image');
            $path = "upload/images/blogimages/";
            $filename = ImageUpload::updateImage($old, $path, $file);
            $image_name = $path.$filename;
        }else{
            $image_name = $old;
        }

        $blogs->title = $request->get('title');
        $blogs->blog_slug= Str::replace(' ', '-', strtolower(Str::slug($request->get('title'))));
        $blogs->image = $image_name;
        $blogs->description = $request->get('description');
        $blogs->username = $request->get('username');
        $blogs->save();
        return redirect()->route('admin::blog')->with('success','Blog Add Successfully');
    }

    public function delete($id)
    {
        $data = Blog::where('id',$id)->first();
        if (!empty($data)) {
            $oldImage = $data->image;
            if (!empty($oldImage)) {
                @unlink(public_path() . '/' . $oldImage);
            }
            $data->delete();
            return response()->json(['status' => 200]);
        }else{
            return response()->json(['status' => 400]);
        }
    }
    public function status(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        if($status=='Active'){
            Blog::where('id',$id)->update([
                'status' => 'Inactive',
            ]);
            $st='Inactive';
            $html='<a href="javascript:void(0);" class="btn btn-warning btn-sm" onclick="active_inactive_blog('.$id.','.$st.')" ><span class="fa fa-ban"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
        else{
            Blog::where('id',$id)->update([
                'status' => 'Active',
            ]);
            $st='Active';
            $html = '<a href="javascript:void(0);" class="btn btn-success btn-sm" onclick="active_inactive_blog('.$id.','.$st.')"><span class="fa fa-check"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
    }

}
