<?php

namespace App\Http\Controllers;
use App\ProductoLaboratorio;

use Illuminate\Http\Request;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('producto_laboratorios.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=ProductoLaboratorio::where($where)->first();
        
        if($verificarRepetido){
            $arrResponse = [
                'message' => 'El Laboratorio ya existe'
            ];
            return redirect()->route('productos.laboratorios')->with($arrResponse);
            dd();
        }
        $laboratorio = new ProductoLaboratorio();
        $laboratorio->create($request->all());
        $arrResponse = [
            'message' => 'Laboratorio guardado correctamente'
        ];
        return redirect()->route('productos.laboratorios')->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(ProductoLaboratorio $object)
    {
        //
        return view('producto_laboratorios.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, ProductoLaboratorio $object)
    {
        //
        $object->nombre = $request['nombre'];
        $object->update();
        $arrResponse = [
            'message' => 'Laboratorio actualizada correctamente'
        ];
        return redirect()->route('productos.laboratorios')->with($arrResponse);
    }

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