<?php

namespace App\Http\Controllers;

use App\formularios;
use App\modulos;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class formularioController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        $modulo = modulos::find($id);
        return view('admin.modulos.formularios.agregarFormulario', [
            'modulo'=>$modulo,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, $id)
    {
        if( empty($request['nombre']) == null || Auth::user()->role=='admin') {
            formularios::create([
                'nombre' => $request['nombre'],
                'Descripcion' => $request['Descripcion'],
                'modulo_id' => $request['modulo']
            ]);
            return redirect('modulo/'.$id);
            
        }
        return redirect('error');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $modulos = modulos::all();
        $formulario = formularios::find($id);
        return view('admin.modulos.formularios.Formulario', [
            'formulario'=>$formulario,
            'modulos'=> $modulos
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if(Auth::user()->role=='admin'){
            $formulario = formularios::find($id);
            $formulario->nombre =$request->nombre;
            $formulario->Descripcion =$request->Descripcion;
            $formulario->modulo_id =$request->modulo;
            $formulario->update();
        }
        return redirect('Formulario/'.$id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        formularios::destroy($id);
    }
}
