<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $this->validate($request,[
            'name' => 'required',
            'price' => 'required',
            'file' => 'required|image'
        ]);
        $product = new Product();
        $file = $request->file('file');
        $name = time().'_'.$file->getClientOriginalName();
        $file->move(public_path().'/images/',$name);
        $product->file = $name;
        $product->name = $request->name;
        $product->prix = $request->price;
        $product->save();
        return redirect()->route('products.index')->with(['success'=>'Produit Ajouté']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $product = Product::find($id);
        return view('admin/products/show',compact('product'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $product = Product::find($id);
        return view('admin/products/edit',compact('product'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $this->validate($request,[
            'name' => 'required',
            'price' => 'required',
        ]);
        $product = Product::find($id);
        //$file = $request->file('file');
        //$name = time().'_'.$file->getClientOriginalName();
        //$file->move(public_path().'/images/',$name);
        //$product->file = $name;
        $product->name = $request->name;
        $product->prix = $request->price;
        $product->save();
        return redirect()->route('products.index')->with(['success'=>'Produit Ajouté']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $product = Product::find($id)->delete();
        return redirect()->route('products.index')->with(['success'=>'Produit Ajouté']);
    }
}
