<?php

namespace App\Http\Controllers;
use App\ProductoMarca;

use Illuminate\Http\Request;

class ProductoMarcaController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $info=ProductoMarca::all();
        return view('producto_marcas.lista',compact('info'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $where = [
            'nombre' => $request->nombre
        ];
        $verificarRepetido=ProductoMarca::where($where)->first();
        
        if($verificarRepetido){
            $arrResponse = [
                'message' => 'La Marca ya existe'
            ];
            return redirect()->route('productos.marcas')->with($arrResponse);
            dd();
        }
        $laboratorio = new ProductoMarca();
        $laboratorio->create($request->all());
        $arrResponse = [
            'message' => 'Marca guardado correctamente'
        ];
        return redirect()->route('productos.marcas')->with($arrResponse);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(ProductoMarca $object)
    {
        //
        return view('producto_marcas.editar',compact('object'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ProductoMarca $object)
    {
        //
        $object->nombre = $request['nombre'];
        $object->update();
        $arrResponse = [
            'message' => 'Marca actualizada correctamente'
        ];
        return redirect()->route('productos.marcas')->with($arrResponse);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        ProductoMarca::destroy($id);
        $arrResponse = [
            'message' => 'Marca eliminada correctamente'
        ];
        return redirect()->route('productos.marcas')->with($arrResponse);
    }
}
