<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

class ImageUploadController extends Controller {
    //source https://www.itsolutionstuff.com/post/laravel-7-image-upload-exampleexample.html

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Reponse
     */
    public function imageUpload() {
        return view('edit');
    }

    /**
     * Display a listing of the resource
     *
     * @return \Illuminate\Http\Reponse
     */
    public function imageUploadPost(Request $request) {
        $request->validate([
            'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'bio' => 'nullable|string|max:255',
        ]);

        $user = Auth::user();

        $avatarName = $user->id.'_avatar'.time().'.'.request()->image->getClientOriginalExtension();

        $userBio = $request->bio;

        $request->image->move(public_path('/uploads/avatars'), $avatarName);

        $user->avatar = $avatarName;
        $user->bio = $userBio;
        $user->save();

        return back()
            ->with('success','You have successfully uploaded an image.')
            ->with('image', $avatarName)
            ->with('bio', $userBio);
    }
}
