<?php

namespace App\Http\Controllers\Admin;

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

class BlogController extends Controller
{
    //

    public function blog(){
        $allblogs = Blog::all();
        return view('admin.pages.blog.blog',compact('allblogs'));
    }

    public  function add(){
        return view('admin.pages.blog.add_blog');
    }


    public function save(Request $request){

        $request->validate([
            'image' => 'required',
            'title' => 'required',
            'content' => 'required',
            'username' => 'required',

        ]);

        if ($request->hasFile('image')){
            $file = $request->file('image');
            $path = "upload/blog/blogimage/";
            $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->content = $request->get('content');
        $blogs->username = $request->get('username');
        
        $blogs->save();


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

    }


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


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


        $blogs = Blog::find($id);
        $old = $blogs->image;
        if ($request->hasFile('image')){
            $file = $request->file('image');
            $path = "upload/blog/blogimage/";
            $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->content = $request->get('content');
        $blogs->username = $request->get('username');
        $blogs->save();
        return redirect()->route('admin::blog_page')->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]);
        }
    }



}
