<?php

namespace App\Http\Controllers;

use App\Category;
use Illuminate\Http\Request;
use File;

class CategoryController extends Controller
{

    protected $rules = [
        'name' => ['required']
    ];

    public function index()
    {
        $categories = Category::orderBy('position')->get(); //->paginate(50);

        return view('categories.index',compact('categories'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('categories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate($this->rules);

        $input = $request->all();
        $input["position"] = $input["position"] != '' ? $input["position"] : 0;
        if(request()->image)
        {

            $file = request()->image;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/categories"))
                File::makeDirectory(public_path()."/files/categories");

            $path = public_path('files/categories');

            request()->image->move($path, $filename);

            $input["image"] = $filename;

        }
        Category::create($input);

        return redirect()->route('categories.index')
                        ->with('success','Category created successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        return view('categories.show',compact('category'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category)
    {
        return view('categories.edit',compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        $request->validate($this->rules);

        $input = $request->all();
        $input["position"] = $input["position"] != '' ? $input["position"] : 0;
        if(request()->image)
        {

            $file = request()->image;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/categories"))
                File::makeDirectory(public_path()."/files/categories");

            $path = public_path('files/categories');

            request()->image->move($path, $filename);

            $input["image"] = $filename;

        }
        $category->update($input);

        return redirect()->route('categories.index')
                        ->with('success','Category updated successfully');
    }

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

        $category->delete();

        return redirect()->route('categories.index')
                        ->with('success','Category deleted successfully');
    }
}
