<?php

namespace App\Http\Controllers;

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

class GroupController extends Controller
{

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

    public function index(Category $category)
    {

        $groups = Group::where('category_id', '=', $category->id)->orderBy('position')->get();
        return view('groups.index',compact('groups', 'category'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Category $category)
    {
        return view('groups.create', compact('category'));
    }

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

        $input = $request->all();

        $input["name"] = $input["name"] == null ? '' : $input["name"];
        $input["category_id"] = $category->id;
        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;

        }

        Group::create($input);

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

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

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

    /**
     * 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, Group $group)
    {
        $request->validate($this->rules);

        $input = $request->all();
        $input["name"] = $input["name"] == null ? '' : $input["name"];

        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;

        }

        $group->update($input);

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category, Group $group)
    {
        $group->delete();

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