<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Redirect;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = Category::all();
        return view('categories.index',compact('list'));
    }

    /**
     * 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)
    {

       // dd($request);
        $validation = Validator::make($request->all(),[
            'name' => 'required',
             ///'photo' => 'required|mimes:jpeg,jpg,gif,png|max:2048',
        
        ]);
        if($validation->fails()){
            $errors = $validation->errors()->first();
            return redirect()->back()->with('errors',$validation->errors());
        }
        
        $obj = new Category();
        $obj->name = $request->name;
        if ($request->photo) {
            $id_photo = $request->file('photo');
            $name = mt_rand(100000, 999999) . time() . '.' . $id_photo->getClientOriginalExtension();
            $destinationPath = public_path('assets/uploads');
            $id_photo->move($destinationPath, $name);
            $obj->image = asset('assets/uploads')."/".$name;
        }
        
        
        if($obj->save())
        {
            return redirect('category')->with('success','Cateogry has been successfully added.');
        }
        else
        {
            return redirect('category')->with('failure','Something went wrong,Try again later');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\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\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        $validation = Validator::make($request->all(),[
            'name' => 'required',
             ///'photo' => 'required|mimes:jpeg,jpg,gif,png|max:2048',
        
        ]);
        if($validation->fails()){
            $errors = $validation->errors()->first();
            return redirect()->back()->with('errors',$validation->errors());
        }
        
        $obj = Category::find($category->id);

        $obj->name = $request->name;
        if ($request->photo) {
            $id_photo = $request->file('photo');
            $name = mt_rand(100000, 999999) . time() . '.' . $id_photo->getClientOriginalExtension();
            $destinationPath = public_path('assets/uploads');
            $id_photo->move($destinationPath, $name);
            $obj->image = asset('assets/uploads')."/".$name;
        }
     
        
        if($obj->save())
        {
            return redirect('category')->with('success','Cateogry has been successfully added.');
        }
        else
        {
            return redirect('category')->with('failure','Something went wrong,Try again later');
        }
    }

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