<?php

namespace App\Http\Controllers;

use App\Models\TravelClass;
use App\Models\TravelProgram;
use App\Models\TravelProgramDraft;
use App\Repositories\TravelProgramRepository;
use Input;

class TravelProgramController extends Controller
{

    protected $travelProgramRepo;

    public function __construct(TravelProgramRepository $travelProgramRepo)
    {
        $this->middleware('admin');
        $this->travelProgramRepo = $travelProgramRepo;
    }

    public function index($step = false)
    {
        $data = [
            'travel_programs' => TravelProgram::all()->sortByDesc("id"),
            'step' => $step
        ];
        return view('travel.program.index', $data);
    }

    public function detail($id)
    {
        if($id == "new") {
            $program = new TravelProgram();
            $id = 'new';

        }else{
            $program = TravelProgram::findOrFail($id);
            $id = $program->id;
        }
        $data = [
            'program' => $program,
            'id' => $id,
        ];
        return view('travel.program.detail', $data);

    }

    public function store($id)
    {
        $data = Input::all();
        $program = $this->travelProgramRepo->update($data);
        \Session()->flash('alert-save', '1');
        return redirect(route('travel_program_detail', [$program->id]));

    }

    /*
     * PROGRAM CLASSES
    */

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function classUpdate(){
        $data = Input::all();
        $this->travelProgramRepo->updateClass($data);
        \Session()->flash('alert-save', '1');
        return redirect(route('travel_program_detail', [$data['program_id']]));
    }
    public function classDelete($id){
        $travel_class = TravelClass::findOrFail($id);
        $pId = $travel_class->program_id;
        if(count($travel_class->travel_program_drafts)){
            \Session()->flash('alert-error', 'Eintrag wird bei den Vorlagen verwendet');
            return redirect(route('travel_program_detail', [$pId]));
        }
        $travel_class->delete();
        \Session()->flash('alert-success', 'Programm Kategorie gelöscht');
        return redirect(route('travel_program_detail', [$pId]));
    }


    /*
     * PROGRAM DRAFTS
    */

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function draftUpdate(){
        $data = Input::all();
        $this->travelProgramRepo->updateDraft($data);
        \Session()->flash('alert-save', '1');
        return redirect(route('travel_program_detail', [$data['travel_program_id']]));
    }
    public function draftDelete($id){
        $travel_program_draft = TravelProgramDraft::findOrFail($id);
        $pId = $travel_program_draft->travel_program_id;
        $travel_program_draft->delete();
        \Session()->flash('alert-success', 'Programm Vorlage gelöscht');
        return redirect(route('travel_program_detail', [$pId]));
    }

}


