<?php

namespace App\Http\Controllers;

use App\Models\BlogImage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class BlogImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return BlogImage::all();
    }

    /**
     * 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)
    {

        $save_name = time() . '.jpg';
        $saveImgPath = 'blog/banner/' . $save_name;

        BlogImage::insert([
            'blogId' => $request->blogId,
            'saveName' => $save_name,
            'fileName' => $request->fileName,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        if ($request->bannerImage) {
            $image_64 = $request->bannerImage;
            $replace = substr($image_64, 0, strpos($image_64, ',') + 1);
            $image = str_replace($replace, '', $image_64);
            $image = str_replace(' ', '+', $image);
            Storage::disk('local')->put($saveImgPath, base64_decode($image));
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\BlogImage  $blogImage
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        return BlogImage::where('id', '=', $request->blogId)->first();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\BlogImage  $blogImage
     * @return \Illuminate\Http\Response
     */
    public function edit(BlogImage $blogImage)
    {
        //
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\BlogImage  $blogImage
     * @return \Illuminate\Http\Response
     */
    public function destroy(BlogImage $blogImage)
    {
        //
    }
}
