<?php

namespace App\Http\Controllers;
use App\Departamento;
use App\Pais;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class DepartamentosController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $departamentos=Departamento::with('pais')
                ->get();
        return view('departamentos.lista',compact('departamentos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $paises=Pais::all();
        return view('departamentos.nuevo',compact('paises'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $where = [
            'descripcion_departamento' => $request->descripcion,
            'id_pais' => $request->id_pais
        ];
        $verificarRepetido=Departamento::where($where)->first();
        if($verificarRepetido){
            $arrResponse = [
                'message' => 'El Departamento ya existe'
            ];
            return redirect()->route('departamento.index')->with($arrResponse);
            dd();
        }
        $departamento = new Departamento();
        $departamento->descripcion_departamento = $request->descripcion;
        $departamento->id_pais = $request->id_pais;
        $departamento->id_user = Auth::id();
        $departamento->save();
        $arrResponse = [
            'message' => 'Departamento guardado correctamente'
        ];
        return redirect()->route('departamento.index')->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(Departamento $departamento)
    {
        //
        $paises=Pais::all();
        return view('departamentos.editar',compact('paises','departamento'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Departamento $departamento)
    {
        //
        $departamento->descripcion_departamento = $request->descripcion;
        $departamento->id_pais = $request->id_pais;
        $departamento->id_user = Auth::id();
        $departamento->update();
        $arrResponse = [
            'message' => 'Departamento actualizado correctamente'
        ];
        return redirect()->route('departamento.index')->with($arrResponse);
    }

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