<?php

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

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

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

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

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

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