<?php

namespace App\Http\Controllers;
use App\ProductoLinea;

use Illuminate\Http\Request;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('producto_lineas.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=ProductoLinea::where($where)->first();
        
        if($verificarRepetido){
            $arrResponse = [
                'message' => 'La Línea ya existe'
            ];
            return redirect()->route('productos.lineas')->with($arrResponse);
            dd();
        }
        $laboratorio = new ProductoLinea();
        $laboratorio->create($request->all());
        $arrResponse = [
            'message' => 'Línea guardado correctamente'
        ];
        return redirect()->route('productos.lineas')->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(ProductoLinea $linea)
    {
        //
        return view('producto_lineas.editar',compact('linea'));
    }

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

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