<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreItemRequest;
use App\Models\Category;
use App\Models\Picture;
use App\Models\Item;
use App\Models\Subcategory;
use Illuminate\Support\Facades\Input;

class UsersItemController extends Controller
{
    public function index()
    {
        return view('itemregister.index');
    }

    public function create()
    {
        $categories = Category::all();
        $subcategories = Subcategory::all();

        return view('userprofile.index', [
                'newItem' => true,
                'categories' => $categories,
                'subcategories' => $subcategories,
            ]
        );

    }

    public function store(StoreItemRequest $request)
    {
        if ($request->isMethod('POST')) {
            $item = new Item($request->all());
            $item->pictures = !empty(Input::file()); // не правильно нужно через Model Picture
            $item->save();

            Picture::fromRequest($item);

            return redirect()->back()->with(['message' => 'Новый объект создан']);
        }
    }
}