<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Gira;
use App\Apm;
use App\PlanTrabajo;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
                        
        //Linea de GL
        $idGL=Auth::id();
        $giras=Gira::with('region')->get();
        $glInfo=Apm::where('id_user',$idGL)->first();
        $lineaGL=$glInfo->id_linea;

        $apms=Apm::where('id_linea',$lineaGL)
                    ->where('id_user','!=',$idGL)
                    ->get();
        return view('distribucion_territorios.nuevo',compact('apms','giras'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $verificarAsignacion=PlanTrabajo::where('id_apm',$request->id_apm)
                    ->where('id_gira',$request->id_gira)
                    ->first();
        if($verificarAsignacion){
            $arrResponse = [
                'message' => 'La asignación ya existe'
            ];
            return redirect()->route('territorio.index')->with($arrResponse);
        }else{
            $asignacionPlan = new PlanTrabajo();
            $asignacionPlan->id_apm = $request->id_apm;
            $asignacionPlan->id_codigo_postal = 0;
            $asignacionPlan->id_gira = $request->id_gira;
            $asignacionPlan->id_user = Auth::id();
            $asignacionPlan->save();
            $arrResponse = [
                'message' => 'Asignación correctamente'
            ];
            return redirect()->route('territorio.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($id)
    {
        //Linea de GL
        $planTrabajo = PlanTrabajo::where('id',$id)->first();
        $giraPlan=Gira::where('id',$planTrabajo->id_gira)->with('region')->first();
        $idGL=Auth::id();
        $giras=Gira::with('region')->get();
        $glInfo=Apm::where('id_user',$idGL)->first();
        $lineaGL=$glInfo->id_linea;

        $apms=Apm::where('id_linea',$lineaGL)
                    ->where('id_user','!=',$idGL)
                    ->get();
        return view('distribucion_territorios.editar',compact('apms','giras','planTrabajo','giraPlan'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, PlanTrabajo $territorio)
    {
        //

        $territorio->id_apm = $request->id_apm;
        $territorio->id_codigo_postal = 0;
        $territorio->id_gira = $request->id_gira;
        $territorio->id_user = Auth::id();
        $territorio->update();
        $arrResponse = [
            'message' => 'Asignación actualizada correctamente'
        ];
        return redirect()->route('territorio.index')->with($arrResponse);
    }

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

        $gira['data']=Gira::with('region')
                ->get();
        return response($gira);
    }
}
