<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Session;

class ModalController extends Controller {
    public function modalEdit() {
        return view('edit');
    }

    public function modalEditPost(Request $request) {
        $user = Auth::user();
        $avatarName = "";
        $userBio = "";
        $request->validate([
            'bio' => 'nullable|string|max:255',
            'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        if ($request->has('bio')) {
            $userBio = $request->bio;
            $user->bio = $userBio;
        }

        if ($request->hasFile('image')) {
            $extension = $request->image->extension();

            $avatarName = $user->id.'_avatar'.time().'.'.$extension;

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

            $user->avatar = $avatarName;
        }

        $user->save();

        return back()
            ->with('success','You have successfully edited your bio.');
    }
}
