<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProfileRequest;
use App\Models\Profile;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;


class RegisterProfileController extends Controller
{
    public function create()
    {
        $profile = Profile::where('user_id', Auth::id())->first() ?? new Profile();
        return view('userprofile.profile', ['profile' => $profile]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */

    public function store(ProfileRequest $request)
    {
        $user = Auth::user();

        if ($request->isMethod('POST')) {
            $profile = Profile::where('user_id', Auth::id())->first();

            if ($profile) {
                $profile->fill($request->except('_token', 'submit'));
                $profile->update();
            }
            if (!$profile) {
                $profile = new Profile($request->all());
                $profile->save();
            }
        }


        return redirect()->route('home')->with(['message' => 'Профиль создан']);

    }

    public static function createView()
    {
        return self::create();
    }

}
